Reputation:
I am trying to get my head around how dates in java are getting parsed, and I still cant seem to get around to what I need to pass in as parameters to get a consistent output no matter what time zone I am in.
I wrote this test class:
import java.util.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
public class TimeTest {
public static void main(String [] args) throws Exception
{
String dateString = "2012-03-28 11:45:00 +0200";
String dateString1 = "2012-03-28 11:45:00 +0000";
Timestamp timestamp= null;
Timestamp timestamp1= null;
DateFormat planningDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date date = planningDateFormat.parse(dateString);
Date date1 = planningDateFormat.parse(dateString1);
timestamp = new Timestamp(date.getTime());
timestamp1 = new Timestamp(date1.getTime());
System.out.println("Time value passed in was: " + dateString);
System.out.println("Date value after parse: " + date);
System.out.println("Time stamp value is: " + timestamp);
System.out.println("Time value passed in was: " + dateString1);
System.out.println("Date value after parse: " + date1);
System.out.println("Time stamp value is: " + timestamp1);
}
}
which gives the following output:
Time value passed in was: 2012-03-28 11:45:00 +0200
Date value after parse: Wed Mar 28 10:45:00 IST 2012
Time stamp value is: 2012-03-28 10:45:00.0
Time value passed in was: 2012-03-28 11:45:00 +0000
Date value after parse: Wed Mar 28 12:45:00 IST 2012
Time stamp value is: 2012-03-28 12:45:00.0
Now let me explain why non of this makes any sense:
In the first date passes in I pass in the time 11:45 with a +0200 offset, and it gives me the time IST which is Irish Standard Time of 10:45.
Can anyone explain to me what is going on there and how its coming to this conclusion? The time passes in of 11:45, does that represent 11:45 UTC or 11:45 local time (to the country which has an offset of +0200). From what I see it seems to me that the time represents the local time.
Another question is:
If I'm in Ireland, which is +0000 in the winter and +0100 in the summer. Would the value being passed in for "Z" change depending on what date I'm querying? Or shouldn't java know this automatically depending on the date passes in with the time?
I really don't know what's going on.
Upvotes: 3
Views: 169
Reputation: 2425
The Z
in your format pattern represents an absolute offset from UTC. When you say 11:45:00 +0200
, that is equal to the time represented by 09:45:00 UTC
; i.e., 2 hours ahead of what the time is at UTC. Since IST is UTC+1 we have:
11:45:00 +0200
==09:45:00 UTC
==10:45:00 IST
If you use a timezone alias (e.g., Europe/Dublin
) instead of an absolute offset, Java will automatically handle daylight savings time rules depending on the date part you provide.
Upvotes: 1
Reputation: 38444
Yes, the time passed in is local time. By saying 2012-03-28 11:45:00 +0200
you actually say "11:45 in a timezone which has +02:00 against UTC". The time is then, of course, shown in your local time (which is +01:00 against UTC because of the DST, so it subtracted one hour from the passed time).
The Date methods do respect Daylight saving time. Try changing your local system date and you'll see that the returned time also changes. Or try inputing CDT, CST, MDT, MST and similar timezome strings supported by Date class - they all know their Daylight saving time.
Upvotes: 0