Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse Timezone with Single Digit Offset? "Wed Dec 31 00:00:00 GMT-8 1969"

I have this date:

"Wed Dec 31 00:00:00 GMT-8 1969"

But it cannot be parsed with this SimpleDateFormat:

new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");

How do I specify the -8 in the format string? I have tried Z and X after reading the SDF docs, but to no avail. What should I use?

like image 731
David Williams Avatar asked Nov 01 '25 15:11

David Williams


1 Answers

If you are using java 8, you can try DateTimeFormatter with a pattern of "EEE MMM d HH:mm:ss O yyyy" likes:

    String date = "Wed Dec 31 00:00:00 GMT-8 1969";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss O yyyy");
    LocalDateTime dateTime = LocalDateTime.parse(date, formatter);

O represent localized zone-offset and can format zone offset like GMT+8, GMT+08:00, UTC-08:00. You can see Offset O in DateTimeFormatter for more details.

like image 84
Wilson Avatar answered Nov 03 '25 05:11

Wilson



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!