0

I have a problem with dates.

I'm sure these milliseconds 1317322560000, represent the date of Thu Sep 29 18:56:00 GMT+02:00 2011 in Italy.

But using the Calendar class the date is Thu Sep 29 20:56:00 GMT+02:00. I think this happens because, the summer schedule is in effect.

how can I convert milliseconds into the corresponding date right?

3 Answers3

2

System.currentTimeMillis() returns "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC." (i.e. GMT).

So the "Date" 1317322560000 you have, is 29/09/2011:18:56:00 GMT. In Italy, on 29th Sep the offset from GMT is +2 hours (because of "summer time" or technically speaking DST = Daylight Saving Time). From 30/10/2011:03:00:00 (next sunday by the way), in Italy they will be in "winter time" (no DST), so the offset will be +1).

So you correctly get Thu Sep 29 20:56:00 CEST 2011 (18:56:00 + 2 hours offset in Italy's time zone). Please check this code that shows all this stuff (it is Groovy).

import java.text.DateFormat
import java.util.TimeZone

println Locale.getDefault()
Date d = new Date(1317322560000)
println d
Locale.setDefault(new Locale("it", "IT"))
println Locale.getDefault()

DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
println df.getTimeZone().getOffset(1317322560000) + " => +2h offset in 'summer time' (DST on)"

df.setTimeZone(TimeZone.getTimeZone("GMT+00:00"))
println df.format(d)
df.setTimeZone(TimeZone.getTimeZone("GMT+01:00"))
println df.format(d)
df.setTimeZone(TimeZone.getTimeZone("GMT+02:00"))
println df.format(d)
df.setTimeZone(TimeZone.getTimeZone("Europe/Rome"))
println df.format(d)
println "---"

Date winterDate = new Date(1321382560000)
println winterDate
println df.getTimeZone().getOffset(1321382560000) + " => +1h offset in 'winter time' (DST off)"

The result of this:

es_ES
Thu Sep 29 20:56:00 CEST 2011
it_IT
7200000 => +2h offset in 'summer time' (DST on)
giovedì 29 settembre 2011 18.56.00 GMT+00:00
giovedì 29 settembre 2011 19.56.00 GMT+01:00
giovedì 29 settembre 2011 20.56.00 GMT+02:00
giovedì 29 settembre 2011 20.56.00 CEST
---
Tue Nov 15 19:42:40 CET 2011
3600000 => +1h offset in 'winter time' (DST off)
jalopaba
  • 8,039
  • 2
  • 44
  • 57
  • You can check also http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – jalopaba Oct 28 '11 at 11:24
  • It's ok, but if I want to create a calendar from this dataformat and when I request the date, the result is 20.56 – Gioacchino Del Prete Oct 28 '11 at 11:46
  • It will display 18:56 if the timezone is GMT+00:00, 19:56 if TZ is GMT+01:00 and 20:56 if TZ is GMT+02:00, which is your case. There is no way you get a different result if your timezone is GMT+2. So I guess that when you got "18:56" you were running the code with the timezone set to GMT+0. – jalopaba Oct 29 '11 at 08:07
0

It is indeed Thu Sep 29 20:56:00 GMT+02:00. i.e. the corresponding date-time in UTC is Thu Sep 29 18:56:00Z where Z stands for Zulu and is the timezone designator for zero-timezone offset (+00:00 hours offset or the Etc/UTC timezone).

Solution using java.time, the modern API

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.ofEpochMilli(1317322560000L);

        // In Rome
        ZonedDateTime zdtRome = instant.atZone(ZoneId.of("Europe/Rome"));
        System.out.println(zdtRome);

        // In UTC
        ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc);

        // Custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd uuuu HH:mm:ssXXX", Locale.ENGLISH);
        System.out.println(zdtRome.format(dtf));
        System.out.println(zdtUtc.format(dtf));
    }
}

Output:

2011-09-29T20:56+02:00[Europe/Rome]
2011-09-29T18:56Z[Etc/UTC]
Thu Sep 29 2011 20:56:00+02:00
Thu Sep 29 2011 18:56:00Z

Learn more about the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

new Date(1317322560000l)

outputs

Thu Sep 29 20:56:00 CEST 2011

same as your calendar. Why should it be 18:56?

kgautron
  • 7,915
  • 9
  • 39
  • 60