Does parsing SimpleDateFormatter behave differently?

I'm trying to figure out how dates in java are parsed and I still can't figure out what I need to pass as parameters to get consistent output no matter what timezone I'm 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 this doesn't make any sense:

On the first date, I miss the 11:45 am with an offset of +0200 and that gives me IST, which is 10:45 am Irish standard time.

Can someone explain to me what is going on there and how he comes to this conclusion? The time is at 11:45, whether it is 11:45 UTC or 11:45 local time (to a country that has an offset of +0200). From what I can see, it seems to me that the time is local time.

Another question:
If I am in Ireland, that is +0000 in winter and +0100 in summer. Will the value passed for "Z" change depending on which date I am requesting? Or shouldn't you know it automatically depending on the date passed over time?

I really don't know what's going on.

+3


source to share


2 answers


Z

in your template, the format is the absolute offset from UTC. When you speak 11:45:00 +0200

, it is equal to the time represented 09:45:00 UTC

; that is, 2 hours earlier than 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 (for example Europe/Dublin

) instead of an absolute offset, Java will automatically handle daylight saving time rules based on the part of the date you provide.

+1


source


Yes, the time elapsed at this time is local time. Speaking 2012-03-28 11:45:00 +0200

, you are actually saying "11:45 am in the time zone, which has +02: 00 against UTC". The time is then of course displayed in your local time (which is +01: 00 against UTC due to DST, so it subtracts one hour from the elapsed time).



Date methods respect daylight saving time. Try changing your local system date and you will see the return time change as well. Or try CDT, CST, MDT, MST, and similar time strings supported by the Date class - they all know their daylight saving time.

0


source