Monday, December 17, 2012

Java.util.Calendar - milliseconds since Jan 1, 1970

The dates you print from Calendar are local to your timezone, whereas the epoch is defined to be midnight of 1970-01-01 in UTC. So if you live in a timezone west of UTC, then your date will show up as 1969-12-31, even though (in UTC) it's still 1970-01-01.

        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(1970, 0, 1, 0, 0, 0);
        System.out.println(cal.getTimeInMillis());

http://stackoverflow.com/questions/263376/java-util-calendar-milliseconds-since-jan-1-1970

No comments:

Post a Comment