Issue with converting the dates between different timzones

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Issue with converting the dates between different timzones

jeetmarwah
This post has NOT been accepted by the mailing list yet.
Hi,

I have a simple use case to convert a date from local to GMT and vice versa. Here is the snippet of my code and its output.

public static Date convertLocalToGMT(Date localDate)
    {
        DateTime dateTime = new DateTime(localDate);
        System.out.println("Current Local Date :" + dateTime.toDate());
        System.out.println("Current Local Date In Millis :" + dateTime.toDate().getTime());
       
        DateTime gmt = dateTime.withZoneRetainFields(DateTimeZone.forID("Etc/GMT"));
        System.out.println("GMT Date :" + gmt.toDate());
        System.out.println("GMT Date In Millis :" + gmt.toDate().getTime());
       
        DateTime gmt1 = dateTime.withZone(DateTimeZone.forID("Etc/GMT"));
        System.out.println("GMT Date :" + gmt1.toDate());
        System.out.println("GMT Date In Millis :" + gmt1.toDate().getTime());

        DateTime gmt2 = dateTime.toDateTime(DateTimeZone.forID("Etc/GMT"));
        System.out.println("GMT Date :" + gmt2.toDate());
        System.out.println("GMT Date In Millis :" + gmt2.toDate().getTime());

        /*
        long dm = localDate.getTime();
        System.out.println();
        long gmtm = gmt.getMillis();
        System.out.println();
        */
        Date d = gmt.toDate();
        System.out.println();
       

       
        return d;
    }



The output of this is as follows:

Current Local Date :Fri Mar 30 11:26:10 PDT 2012
Current Local Date In Millis :1333131970483
GMT Date :Fri Mar 30 04:26:10 PDT 2012
GMT Date In Millis :1333106770483
GMT Date :Fri Mar 30 11:26:10 PDT 2012
GMT Date In Millis :1333131970483
GMT Date :Fri Mar 30 11:26:10 PDT 2012
GMT Date In Millis :1333131970483


Here is my main method to invoke the api:

    public static void main(String[] args)
    {
        convertLocalToGMT(new Date());
    }


What I don't understand is why the conversion is not happening from local to GMT.

Please advise.

Thanks,
Jeet
Loading...