Java中日期格式的问题

7

我对这段代码有问题。

public static void main(String[] args) throws IOException, ParseException { 
    String strDate = "2011-01-12 07:50:00";
    DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd H:M:S");
    Date date = formatter.parse(strDate);
    System.out.println("Date="+date);
}

输出结果为:
日期=2015年2月12日 星期四 07:01:00 EET
您做错了什么吗?
4个回答

11

大写的M代表月份中的天数。小写的m代表小时中的分钟数。同时,您需要小写的s,因为大写的S表示毫秒。

话虽如此,您可能更接近需要的是这个:

yyyy-MM-dd HH:mm:ss

3

你的格式不正确,请参考SimpleDateFormat

你应该使用以下格式:

String strDate = "2011-01-12 07:50:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

1

0
根据你的问题,你需要检查以下语句: DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd H:M:S"); 其中,"yyyy-MM-dd HH:mm:ss" 是 SimpleDateFormate。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接