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");
精彩评论