3

The code below:

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class FooMain {
    private static final DateFormat DF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");

    public static void main(String args[]) {
        System.out.println(DF.format(new Date(0)));
    }
}

prints out:

1970-01-01T01:00Z

Should'nt it have been 1970-01-01T00:00Z instead? I understand that Unix Epoch time is always unambiguous and we don't have to worry about timezones, but here's my timezone in case it matters:

$ cat /etc/timezone 
Europe/Madrid    
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • 2
    From the documentation of `java.util.Date`: "[...]represent the specified number of milliseconds since [...] January 1, 1970, 00:00:00 **GMT**." and Spain's time zone is GMT+1 so that output is correct. – Marco Forberg Jun 04 '13 at 14:49
  • 1
    Also, don't confuse `java.sql.Date` with `java.util.Date`. The former extends the latter and is supposed to contain info about the date only. – Luiggi Mendoza Jun 04 '13 at 14:53

3 Answers3

7

new Date(0) does correspond to January 1, 1970, 00:00:00 GMT. The issue is that, by default, DateFormat will print the date in your system timezone. Set the timezone on your formatter to GMT:

DF.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(DF.format(new Date(0))); // outputs: 1970-01-01T00:00Z
DannyMo
  • 11,344
  • 4
  • 31
  • 37
  • 1
    My bad. Copied the snippet from: http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date and thought that "Z" corresponds to Zulu timezone (which it does, but it is hardcoded so one has to set the timezone as per your answer). – Marcus Junius Brutus Jun 04 '13 at 15:02
  • No worries. I completely missed that the format had a hard-coded `'Z'` on it, as well =p – DannyMo Jun 04 '13 at 15:04
1

You have to .setTimeZone() your SimpleDateFormat; by default, the time zone is the system time zone:

final SimpleDateFormat fmt 
    = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));

System.out.println(fmt.format(new Date(0)));
fge
  • 119,121
  • 33
  • 254
  • 329
1

java.time

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat etc.) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using java.time, the modern API:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        // Recommended
        Instant epoch = Instant.EPOCH;
        System.out.println(epoch);

        // Alternatively,
        epoch = Instant.ofEpochMilli(0);
        System.out.println(epoch);
    }
}

Output:

1970-01-01T00:00:00Z
1970-01-01T00:00:00Z

Learn more about java.time, 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