Date value wrongly formatted

前端 未结 3 1324
悲哀的现实
悲哀的现实 2021-01-21 12:03

I am trying to convert a String DateTime value which is present in a flat file as a Date object after parsing the flat file in my code.

I have

3条回答
  •  借酒劲吻你
    2021-01-21 12:57

    Your system timezone is different. The output is showing IST - or Indian Standard Time, which is an 12.5h difference from PDT. The code is properly parsing the given date which is PDT (UTC -7) and printing out IST (UTC +5h30).

    Java stores Dates as UTC dates. So when you parse the PDT date, Java will convert it to UTC and store it internally as a UTC timestamp. When you print, if you do not specify the timezone, it will default to the system timezone, which in your case would appear to be IST.

    To specify an exact timezone, specify it in the SimpleDateFormat:

    DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy");
    f.setTimeZone(TimeZone.getTimeZone("PDT"));
    Date date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
    System.out.println("---date----" + f.format(date));
    

提交回复
热议问题