Date/Calendar confusion

Discussion in 'Java' started by Ulrich Scholz, Sep 6, 2012.

  1. Dear all,

    have a look at the function below (Java 5). The first result is 0 as expected. But why is the second one different?

    Thanks, Ulrich


    private static void testDate() throws ParseException
    {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    TimeZone timeZone = TimeZone.getTimeZone("GMT");
    timeZone.setRawOffset(0); // get GMT time zone for sure
    dateFormat.setTimeZone(timeZone);

    Calendar calendar1 = Calendar.getInstance(timeZone, Locale.US);
    Date date1 = DATE_FORMAT.parse("1970-01-01T00:00:00.000");
    calendar1.setTime(date1);
    System.out.println(calendar1.getTimeInMillis()); // is 0

    Calendar calendar2 = Calendar.getInstance(timeZone, Locale.US);
    Date date2 = DATE_FORMAT.parse("0000-00-00T00:00:00.000");
    calendar2.setTime(date2);

    // adjust for the epoch 01.01.1970
    //
    calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR) + 1970);
    calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH) + 1);
    calendar2.set(Calendar.DAY_OF_MONTH, calendar2.get(Calendar.DAY_OF_MONTH) + 1);

    System.out.println(calendar2.getTimeInMillis()); // should be 0 but is -124335907200000
    }
    Ulrich Scholz, Sep 6, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shevek
    Replies:
    3
    Views:
    6,013
    Shevek
    Jun 23, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,230
    Michael Borgwardt
    May 30, 2004
  3. Shevek
    Replies:
    0
    Views:
    375
    Shevek
    Jun 23, 2004
  4. Replies:
    0
    Views:
    192
  5. Ulrich Scholz

    Date/Calendar confusion

    Ulrich Scholz, Sep 6, 2012, in forum: Java
    Replies:
    8
    Views:
    457
Loading...

Share This Page