Reputation: 137
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?
Upvotes: 0
Views: 3681
Reputation: 79550
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).
java.time
, the modern APIimport 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.
Upvotes: 0
Reputation: 8129
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)
Upvotes: 2
Reputation: 8283
new Date(1317322560000l)
outputs
Thu Sep 29 20:56:00 CEST 2011
same as your calendar. Why should it be 18:56
?
Upvotes: 0