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?
6 Answers
// 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());
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.
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
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.
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
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