Date value wrongly formatted

2020-04-13 05: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 written the code to do that but when I format the date its always giving me a date more than 1 day for the specified value, some times it's adding 5:30.

Below is the code for that:

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

The output for the above is

    ---date----Wed Aug 24 08:30:03 IST 2011  

Can you please let me know whats the issue here. Is there a problem in the pattern that I am using in the SimplaDateFormat class or is there a problem with the code. I have been scratching my head on this for a long time now.

回答1:

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));


回答2:

Can you please let me know whats the issue here.

Sure. You're effectively calling date.toString(), which doesn't know anything about the SimpleDateFormat which was used to parse the original text value. A Date is just an instant in time. It has no notion of a per-instance format. Additionally, it doesn't know about a time zone. You've given a value in PDT, which was then parsed... and when you print it, it's using the system local time zone (IST). That's what Date.toString always does.

If you want to format a Date in a particular way, using a particular format in a particular time zone, call DateFormat.format.



回答3:

Because you are not formatting a date. Look at the example

  public static void main(String[] args){
    Locale currentLocale = Locale.US;
    DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
    Date date = null;
    Date today;
    try {
      today = new Date();
      String result = f.format(today);
      System.out.println("Locale: " + currentLocale.toString());
      System.out.println("Result: " + result);
      date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
    } catch (ParseException e) {
      e.printStackTrace();  
    }
    System.out.println("---date----" + f.format(date));
  }

will output

Locale: en_US
Result: Tue Sep 25 19:12:38 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011

Now, you have a bit modified code

  public static void main(String[] args){
    Locale currentLocale = Locale.US;
    DateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
    DateFormat f2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zz yyyy", currentLocale);
    Date date = null;
    Date today;
    try {
      today = new Date();
      String result = f.format(today);
      System.out.println("Locale: " + currentLocale.toString());
      System.out.println("Result: " + result);
      date = f.parse("Tue Aug 23 20:00:03 PDT 2011");
      System.out.println("---date----" + f.format(date));
      System.out.println("---date----" + f2.format(date));

    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

which outputs to

Locale: en_US
Result: Tue Sep 25 20:42:10 EEST 2012
---date----Tue Aug 23 20:00:03 PDT 2011
---date----Wed Aug 24 06:00:03 EEST 2011  

seems that SimpleDateFormat don't care about timezone even if 'z' pattern is specified. It is setting the timezone when it parses the input. That's how I can describe that a strange behavior. Then use of 'z' pattern seems obsolete and lead to unpredictable results.

so setting the TimeZone will fix the issue

f2.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));