我有一个表示日期的字符串。

String date_s = "2011-01-18 00:00:00.0";

我想把它转换成一个日期,并以YYYY-MM-DD格式输出。

2011-01-18

我怎样才能做到这一点呢?


好吧,根据我在下面找到的答案,以下是我尝试过的一些方法:

String date_s = " 2011-01-18 00:00:00.0"; 
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); 
Date date = dt.parse(date_s); 
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));

但是它输出02011-00-1而不是所需的2011-01-18。我做错了什么?


当前回答

   String str = "2000-12-12";
   Date dt = null;
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    try 
    {
         dt = formatter.parse(str);
    }
    catch (Exception e)
    {
    }

    JOptionPane.showMessageDialog(null, formatter.format(dt));

其他回答

使用LocalDateTime#parse()(如果字符串碰巧包含时区部分,则使用ZonedDateTime#parse())将某个模式下的string解析为LocalDateTime。

String oldstring = "2011-01-18 00:00:00.0";
LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));

然后使用localdatetime# format()(或zoneddatetime# format())将LocalDateTime格式化为特定模式的字符串。

String newstring = datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(newstring); // 2011-01-18

或者,如果您还没有使用Java 8,可以使用SimpleDateFormat#parse()将特定模式下的String解析为Date。

String oldstring = "2011-01-18 00:00:00.0";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

然后使用SimpleDateFormat#format()将日期格式化为特定模式的字符串。

String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18

参见:

Java字符串到日期的转换


更新:根据你的失败的尝试,你添加到这个问题后的答案张贴;模式是区分大小写的。仔细阅读java.text.SimpleDateFormat javadoc中各个部分代表什么。举个例子,M代表月,M代表分钟。此外,年份是四位数yyyy,而不是五位数yyyyy。仔细看看我上面发布的代码片段。

   String str = "2000-12-12";
   Date dt = null;
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    try 
    {
         dt = formatter.parse(str);
    }
    catch (Exception e)
    {
    }

    JOptionPane.showMessageDialog(null, formatter.format(dt));
private SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy");

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if(value instanceof Date) {
        value = dataFormat.format(value);
    }
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
};
try
 {
    String date_s = "2011-01-18 00:00:00.0";
    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date tempDate=simpledateformat.parse(date_s);
    SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");           
    System.out.println("Output date is = "+outputDateFormat.format(tempDate));
  } catch (ParseException ex) 
  {
        System.out.println("Parse Exception");
  }

你错了: SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");

第一: 应该是 新SimpleDateFormat(“yyyy-mm-dd”); //yyyy 4而不是5 这个显示02011,但是yyyy显示2011

第二: 像这样更改代码 新SimpleDateFormat(“yyyy-MM-dd”);

希望能帮到你