如何将2013-03-13T20:59:31 + 0000日期字符串解析为Date


问题内容

如何将此格式日期字符串 2013-03-13T20:59:31 + 0000 解析为Date对象?

我正在尝试这种方式,但是它不起作用。

DateFormat df = new SimpleDateFormat("YYYY-MM-DDThh:mm:ssTZD");
Date result =  df.parse(time);

问题答案:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");

年是小写的y。输入中与日期无关的任何字符(如“ T”中的字母)都应用2013-03-13T20:59:31+0000引起来''

有关定义的模式字母的列表,请参见文档

解析检查给定日期是否为您指定的格式。要在检查后以特定格式打印日期,请参见以下内容:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
Date result;
try {
    result = df.parse("2013-03-13T20:59:31+0000");
    System.out.println("date:"+result); //prints date in current locale
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(sdf.format(result)); //prints date in the format sdf
}