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?
-
What Java version? 7? 8? – Tomas Oct 30 '15 at 07:32
-
2It seems the timestamp is in seconds, java works in milliseconds. Keep that in mind. – aalku Oct 30 '15 at 09:41
-
Similar: [*How to convert a UTC timestamp to local day, hour, minute in Java?*](https://stackoverflow.com/q/3843080/642706) – Basil Bourque May 26 '19 at 20:52
6 Answers
to;dr
Instant.ofEpochSecond ( 1_446_159_600L )
2015-10-29T23:00:00Z
Count From Epoch
You appear to have an integer count-from-epoch counting whole seconds with an epoch of Unix time, the first moment of 1970 in UTC.
The old date-time classes you reference are based on milliseconds. So multiply by 1,000.
java.time
Even better, avoid those old classes altogether. The new java.time framework in Java 8 and later is a vast improvement, supplanting those old classes. See Tutorial. These new classes have a resolution of nanoseconds, or 9 decimal places in a fraction of a second.
The Instant
class (a moment on the timeline in UTC) even has a handy ofEpochSecond
method, so no need to multiply by a thousand.
long wholeSecondsFromEpochInUtc = 1_446_159_600L;
Instant instant = Instant.ofEpochSecond ( wholeSecondsFromEpochInUtc );
That solves your Question in a one-liner.
Formatting
We can go further. Apply a time zone to that Instant to get a ZonedDateTime
.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );
String output = zdt.format ( formatter );
Dump to console.
System.out.println ( "instant (UTC): " + instant );
System.out.println ( "zdt: " + zdt );
System.out.println ( "output: " + output );
instant (UTC): 2015-10-29T23:00:00Z
zdt: 2015-10-29T19:00-04:00[America/Montreal]
output: jeudi 29 octobre 2015 19 h 00 EDT

- 303,325
- 100
- 852
- 1,154
// print the time in local time zone
Date date = new Date(1446159600);
System.out.println(date);
// print UTC time
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(utcTimeZone);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(utcTimeZone);
calendar.setTimeInMillis(date.getTime());
System.out.println(simpleDateFormat.format(calendar.getTime()));
//print in local time zone
//Because getTime method actully call new Date(long millsecond);
/* public final Date getTime() {
return new Date(getTimeInMillis());
}*/
System.out.println(calendar.getTime());

- 47
- 2
The timestamp seems to be in seconds while most java uses milliseconds so we have to multiply by 1000.
long timestampMilliseconds = 1446159600*1000L;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String stringDate = simpleDateFormat.format(new Date(timestampMilliseconds));
System.out.println(stringDate); // Thu, 29 Oct 2015 23:00:00 GMT
The L after 1000 is so that the multiplication is done as long values and the number does not overflow integer max value. You can use ((long)1446159600)*1000
, 1446159600L*1000
or whatever to get the same effect. You can use TimeUnit.SECONDS.toMillis(1446159600)
too.

- 2,860
- 2
- 23
- 44
Actually 1446159600 milliseconds is "Sat Jan 17 23:12:39 IST 1970". IST being my local time.
But, you can get the date associated using a Calendar object like follows.
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(1446159600 );
System.out.println(cal.getTime());
And, the output in this happens to be "Sat Jan 17 23:12:39 IST 1970"
Also, if you want to convert the milliseconds to a date, you can use http://currentmillis.com/ It gives the date associated with the given millisecs.

- 563
- 1
- 6
- 25
-
That's interesting, if I go to http://www.epochconverter.com/, and put in this number, I get 10/29/2015. Does this mean unix epoch time is different than UTC time in milliseconds? – user270811 Oct 30 '15 at 07:47
-
It looks like 'epochconverter' takes seconds instead of milliseconds. That's the problem :D – Durga Swaroop Oct 30 '15 at 08:57
-
-
-
sorry, I am trying to avoid local timezone, UTC time only as Date and not as string. – user270811 Oct 31 '15 at 04:22
-
@user270811 Then may be you can try converting the date to an Instant. Instant always shows the time in UTC. You can refer to the question i asked on this. http://stackoverflow.com/questions/33435491/toinstant-in-calendar-is-showing-in-gmt-instead-of-local-time/33444673#33444673 – Durga Swaroop Oct 31 '15 at 19:32
Maybe I'm misunderstanding the problem, but this creates a Date from the current time in milliseconds:
public class DateTime
{
public static void main (String[] args)
{
long time = System.currentTimeMillis ();
System.out.println (time);
Date date = new Date (time);
System.out.println (date);
}
}
which outputs:
1446191239738
Fri Oct 30 18:47:19 AEDT 2015

- 1,125
- 9
- 23
-
-
@user270811 but it's just a long integer, you already said it was a UTC date. – dmolony Oct 30 '15 at 07:59
-
this might help - http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – dmolony Nov 03 '15 at 06:25
Check this code out:
// Make a format for showing the date
DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);
// Make a format for showing the time
DateFormat timeFormat = new SimpleDateFormat("hh:mm a", Locale.FRANCE);
// Get the current time in UTC (in milliseconds since 01 Jan 1970)
Long currentUTCDate = Calendar.getInstance().getTimeInMillis();
// Convert the current time from UTC to normal date
Date currentDate = new Date(currentUTCDate);
// Take the date only from currentDate object using the dateFormat we made before
// and pass it to currentDateOnly String
String currentDateOnly = dateFormat.format(currentDate);
// Do the same with the time
String currentTimeOnly = timeFormat.format(currentDate);
In this code we can distinguish three main different classes:
1. SimpleDateFormat()
, which returns an object of type DateFormat
2. Calendar
, whcih one of it methods is getTimeInMillis()
3. Date
, which makes an object that can hold every detail about the time you gave it
Now:
. The getTimeInMillis()
returns a number of type Long, which holds the current number of milliseconds since 01 Jan 1970.
We use this type of Time to make it easier for making calculations with dates.
. In Date
class, we have a constructor that takes our Long number from getTimeInMillis()
as a parameter, and returns for us an object of type Date
that holds every detail of the Time we gave it in the constructor.
. Now we don't need all the details that our object currentDate
holds, so we make a filter to take only date and time from the currentDate
object, and to accomplish that we use objects of type DateFormat
.
. Making an object of type DateFormat
, we use SimpeDateFormat()
constructor and we pass to it a parameter
as a String
of a specific pattern
that we want to take from our currentDate
object, for example ("dd MMM, yyy"), which is in our code:
DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyy", Locale.FRANCE);
. Finally we take our currentDate
object as a paremeter
to our dateFormat.format()
method, which will return to us a String of a filtered Date of the same pattern we gave it, which is in our example "dd MMM, yyy"
, and the result will look something like:
08 Sep, 2018

- 43
- 7
-
-
These terrible classes were supplanted years ago by the modern *java.time* classes. Suggesting their use in 2018 is poor advice. – Basil Bourque Sep 08 '18 at 00:27
-
-
Well, I apologize for giving useless informations. Thanks for passing by – BlackGraper Sep 08 '18 at 00:35