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?
LVL 1
mmingfeilamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
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
0
mmingfeilamAuthor Commented:
Is this UTC date?
0
gurpsbassiCommented:
yes
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Jim CakalicSenior Developer/ArchitectCommented:
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
0
gurpsbassiCommented:
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.
0
Jim CakalicSenior Developer/ArchitectCommented:
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

0
Jim CakalicSenior Developer/ArchitectCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmingfeilamAuthor Commented:
Jim Cakalic: I want the output to be a Date and not string
0
Jim CakalicSenior Developer/ArchitectCommented:
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);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.