开发者

SimpleDateFormat format string with 00 causes Unparsaeble date

开发者 https://www.devze.com 2023-01-17 03:07 出处:网络
The following code causes an \"Unparseable date\" exception when executed. Is there any way I can tell Sim开发者_JAVA百科pleDateFormatthe date string must ends with 00?

The following code causes an "Unparseable date" exception when executed. Is there any way I can tell Sim开发者_JAVA百科pleDateFormat the date string must ends with 00?

public class Main {
public static void main(String[] args) {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss00");
    try {
        Date date = format.parse("2009030916301600");
    } catch (ParseException ex) {
        System.out.println("Exception" + ex);
    }
}

}


Use singlequotes to represent literal text.

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss'00'");

See also:

  • java.text.SimpleDateFormat javadoc

Update: it didn't work here after a quick test. I suspect an uncovered corner case in javadoc. After all, you can just leave them away.

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

Alternatively, you can also use S to represent them as milliseconds. It won't affect the final result since it's zero anyway.

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
0

精彩评论

暂无评论...
验证码 换一张
取 消