3

I am trying to convert a Date to String and then back again to Date. However I found out that the final date is different from the original date, what gives?

    //1975-06-20
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 1975);
    cal.set(Calendar.MONTH, 5);
    cal.set(Calendar.DAY_OF_MONTH, 20);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    System.out.println(cal);

    Date originalDate = cal.getTime();
    System.out.println("Date 1: " + originalDate.toString());

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date date = sdf.parse(originalDate.toString());
    System.out.println("Date 2: " + date.toString());

The output from the above code is:

cal: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Singapore",offset=28800000,dstSavings=0,useDaylight=false,transitions=9,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1975,MONTH=5,WEEK_OF_YEAR=26,WEEK_OF_MONTH=5,DAY_OF_MONTH=20,DAY_OF_YEAR=179,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=0,HOUR_OF_DAY=16,MINUTE=0,SECOND=0,MILLISECOND=333,ZONE_OFFSET=28800000,DST_OFFSET=0]
Date 1: Fri Jun 20 12:00:00 SGT 1975
Date 2: Fri Jun 20 11:30:00 SGT 1975
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • 2
    [Calendar](http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html) is locale aware where as Date is not. See [this post for more info](http://stackoverflow.com/questions/2697549/how-to-make-date-locale-independent) – Brad Jun 27 '12 at 07:58
  • 5
    There is no DateFormat.format(String, String) method. Your code doesn't compile. And what's the point in formatting the toString() representation of a Date? Why do you parse the toString() of originalDate rather than parsing the formatted date? – JB Nizet Jun 27 '12 at 07:59
  • @Brad, [it isn't?](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Date.java#Date.toString%28%29) – d1e Jun 27 '12 at 08:00
  • 1
    Your call to `DateFormat.format()` does not match the Java API: http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html – WhyNotHugo Jun 27 '12 at 08:01
  • 1
    @JB Nizet and @Hugo, you are correct. The first method is an utility class created by my team. It uses `SimpleDateFormat` too. I have removed it from the question to avoid confusion. But my question is still valid though.. – Rosdi Kasim Jun 27 '12 at 08:07

2 Answers2

13

Probably because of the timezone change in Singapore on 1982 (+ 30 minutes).

http://www.timeanddate.com/worldclock/timezone.html?n=236&syear=1980

The SimpleDateFormat take the SGT as UTC+8 when parsing the date, and convert it to UTC+7.5, which is the SGT before 1982. Hence the date is off by 30 minutes.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • This explains the 30 mins difference, but doesn't explain why the "hours" in the original time is showing as `12` when they have been set with `cal.set(Calendar.HOUR, 0)`. @Rosdi, is this a typo in your question? – Brad Jun 27 '12 at 08:40
  • @Brad: Since `HOUR` is hour of morning/afternoon (12-hour cycle), and it's currently afternoon in Singapore (which affect the `.getInstance()`), it is set to 12 PM when the code sets the `HOUR` to 0. `HOUR_OF_DAY` will set the hour for 24 hours in a day. – nhahtdh Jun 27 '12 at 08:46
0

are you sure you pasted the code right?

Date date = DateFormat.format(originalDate.toString(), "EEE MMM dd HH:mm:ss zzz yyyy");

seems problematic: there is no method with the signature Date DateFormat.format(String, String) as far as I know

Gergely Szilagyi
  • 3,813
  • 1
  • 14
  • 12
  • I have removed that part. It is an utility class created by my team. Sorry about that. But my question still valid. – Rosdi Kasim Jun 27 '12 at 08:13