SimpleDateFormat format string with 00 reasons Date mismatch

The following code throws an "Imperfect Date" exception when it is executed. Can I say that SimpleDateFormat date string should end at 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);
    }
}

}

+3
source share
1 answer

Use single quotation marks to represent literal text.

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

See also:


Update : after a quick test this did not work. I suspect a jawadoc has revealed a corner case. In the end, you can just leave them.

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

Alternatively, you can also use Sto represent them in milliseconds. This will not affect the final result, since in any case it is equal to zero.

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
+4
source

Source: https://habr.com/ru/post/1766244/


All Articles