Reputation: 2626
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?
Upvotes: 3
Views: 1020
Reputation: 3512
If you are not providing any date portion by default it will be assigned the default Epoche standard date(01 Jan 1970 00:00 Hours
is called Epoche standard time
and it is taken as standard to measure all the time related calculations as base time).
The getTime()
method of java.util.Date class returns the total number of milliseconds between the given date(including time also)
and Epoche standard( 01 Jan 1970 00:00 Hours)
.
Though you are not using getTime()
method so it is irrelevant here.
So simply you are getting that time.
And according to GMT it is right.
I thinks it will help you.
Upvotes: 0
Reputation: 28991
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.
Upvotes: 1
Reputation: 997
Take day light savings also into consideration. This should give you what you expect
sdf.setTimeZone(TimeZone.getTimeZone("EST5EDT"));
Upvotes: 0