Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date time parsing in Java

Given this code...

 String date = "11:00 AM";

 SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");

 sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern")); // --> should be GMT-4

 TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

 Date parse = sdf.parse(date); // -- gives Thu Jan 01 16:00:00 GMT 1970

Why does date parsing give me 16:00 and not 15:00?

like image 681
Sergiu Avatar asked Aug 06 '26 04:08

Sergiu


1 Answers

You cannot use java.util.Date to store time only. It will assign the 1970 year, which in some cases is incorrect. I would recommend to use org.joda.time.LocalTime for it.

like image 175
kan Avatar answered Aug 07 '26 19:08

kan



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!