java date format - GMT 0700 (PDT)

前端 未结 2 1253
感动是毒
感动是毒 2020-12-11 05:49

This is the date format that I need to deal with

Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)

But I don\'t get what the last two parts are. Is t

相关标签:
2条回答
  • 2020-12-11 06:30

    No, it is not fixed. It is a TimeZone. You can match it with Z in the date format.

    To be more precise, in SimpleDateFormat formats :

    • Z matches the -0700 part.
    • GMT is fixed. Escape it with some quotes.
    • z matches the PDT part. (PDT = Pacific Daylight Time).
    • The parenthesis around PDT are fixed. Escape them with parenthesis.

    You can parse your date with the following format :

    EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'
    

    Another remark : Wed Aug contains the day and month in English so you must use an english locale with your SimpleDateFormat or the translation will fail.

    new SimpleDateFormat("*format*", Locale.ENGLISH);
    
    0 讨论(0)
  • 2020-12-11 06:48

    Here is the Javadoc:

    http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

    For this example: Wed Aug 21 2013 00:00:00 GMT-0700 (PDT), you'd want this format:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class JavaDate {
    
      public static void main (String[] args) throws Exception {
    
        String s= "Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)";
        SimpleDateFormat sdf = 
          new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('Z')'");
        Date d = sdf.parse (s);
        System.out.println ("Date=" + d + "...");
      }
    }
    

    EXAMPLE OUTPUT: Date=Tue Aug 20 23:00:00 PDT 2013...

    Thanx to Arnaud Denoyelle above for his edits!

    0 讨论(0)
提交回复
热议问题