Java How to convert UTC milliseconds into UTC Date

mmingfeilam
mmingfeilam used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(utcMilliSecondsValue)
Date date = calendar .getTime();



How do you know 1446159600 represents Thu, 29 Oct 2015 23:00:00 ?

I believe it represents Sat Jan 17 18:42:39 GMT 1970

Author

Commented:
Is this UTC date?
Top Expert 2015

Commented:
yes
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Jim CakalicSenior Engineer

Commented:
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
Top Expert 2015

Commented:
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 CakalicSenior Engineer

Commented:
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

Senior Engineer
Commented:
I apologize, you wanted a specific date format also. So that would be:
        Date date = new Date(1446159600L * 1000L);
        //  Thu, 29 Oct 2015 23:00:00 GMT
        SimpleDateFormat gmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
        gmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(gmt.format(date));

Open in new window

Which finally produces:
Thu, 29 Oct 2015 23:00:00 GMT

Author

Commented:
Jim Cakalic: I want the output to be a Date and not string
Jim CakalicSenior Engineer

Commented:
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);

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial