Resubmitting: Issue with converting the dates between different timzones

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

Resubmitting: Issue with converting the dates between different timzones

jeetmarwah
NOTE: Submitting again after subscribing to Joda.

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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Resubmitting: Issue with converting the dates between different timzones

jodastephen
On 2 April 2012 18:48, jeetmarwah <[hidden email]> wrote:

> 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.

You are confusing things by converting to and from java.util.Date.

The toString of java.util.Date always prints in the default time zone
of the JDK - as per TimeZone.getDefault(). NO amount of tweaking will
change that.

All the other operations you perform are simply changing the Joda
DateTime object. Once you call toDate() you convert back to
java.util.Date and the toString is thus unchanged.

Try printing the toString of the DateTime object and you will see the
correct result.

Also, you should avoid the deprecated "Etc/GMT" and use just "GMT" or
"UTC" instead

Stephen

------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...