Avatar of Michael Lam
Michael Lam
 asked on

Java How to convert UTC milliseconds into UTC Date

I have an int 1446159600 which is UTC/GMT date Thu, 29 Oct 2015 23:00:00 GMT.  I tried to do conversion to the actual UTC date, but couldn't get it to work with Calendar, SimpleDateFormat, and Timezone classes.  Can someone help me?
Java

Avatar of undefined
Last Comment
Jim Cakalic

8/22/2022 - Mon
SOLUTION
gurpsbassi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michael Lam

ASKER
Is this UTC date?
gurpsbassi

yes
Jim Cakalic

Date is a fairly simple wrapper on a millisecond value. It does not embody the concept of a timezone. That is layered on by what is interpreting the millisecond value into human readable form, typically a DateFormat, though Date.toString() uses a different method. Both are still dependent on something other than Date providing the TimeZone which will usually be the default JVM TimeZone unless provided.

So this:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1446159600)
Date date = calendar .getTime();

Open in new window

doesn't give you a Date object representing GMT time. It's still just a wrapper on a millisecond value. If you do
System.out.println(date.toString());

Open in new window

what is printed will be in the JVM default TimeZone. For me, that's CST so I get:
Sat Jan 17 11:42:39 CST 1970

The output is exactly the same if I do the much simpler
Date date = new Date(1446159600);

Open in new window

What you want to do is set the TimeZone on the DateFormat to get the desired interpretation:
DateFormat gmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
gmt.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(gmt.format(date));

Open in new window

Using this the output is
January 17, 1970 5:42:39 PM GMT

This kind of confusion is the reason the JodaTime API was adopted as a JSR in Java8.

Regards,
Jim
Your help has saved me hundreds of hours of internet surfing.
fblack61
gurpsbassi

Problem is the author has posted in the question :
 
I have an int 1446159600 which is UTC/GMT date Thu, 29 Oct 2015 23:00:00 GMT

Expected result is Thu, 29 Oct 2015 23:00:00.
Jim Cakalic

Per the javadoc for Date:

Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

So epoch is GMT to begin with and milliseconds is a delta from that. The question then becomes one of interpretation into a TimeZone after the milliseconds have been added to the epoch.

The int value provided in the question appears to actually be the number of seconds, not milliseconds, from the epoch. Given that change, this code will produce the intended result:
        Date date = new Date(1446159600L * 1000L);
        DateFormat gmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        gmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(gmt.format(date));

Open in new window

ASKER CERTIFIED SOLUTION
Jim Cakalic

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michael Lam

ASKER
Jim Cakalic: I want the output to be a Date and not string
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jim Cakalic

To reiterate, the Date object simply represents the number of milliseconds that have elapsed since midnight 1/1/1970 GMT. If you want the value you have as a Date object then multiply it by 1000 (because your value is in seconds) and use the result as the argument to the Date constructor:

Date date = new Date(1446159600L * 1000L);