Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat format string with 00 causes Unparsaeble date

Tags:

java

date

The following code causes an "Unparseable date" exception when executed. Is there any way I can tell SimpleDateFormat 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);
    }
}

}

like image 353
miara Avatar asked Jun 01 '26 23:06

miara


1 Answers

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");
like image 135
BalusC Avatar answered Jun 03 '26 13:06

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!