Sun, 16 Nov 2008 03:53:12 GMT+8
请教:该如何写SimpleDateFormat,最好timezone通用一点的。试过EEE, dd MMM yyyy HH:mm:ss zzz不行。How?

解决方案 »

  1.   

    .要是SimpleDateFormat 弄不了 可以用正则。
      

  2.   


       SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
       sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
       String s = sdf.format(new Date());
       System.out.println(s);
      

  3.   

    我不要format,我要parse。
    将Sun, 16 Nov 2008 03:53:12 GMT+8解析为Date 
      

  4.   


              String dateString = "Sun, 16 Nov 2008 03:53:12 GMT+8";   
              String pattern = "EEE,dd MMM yyyy HH:mm:ss z";   
            
              SimpleDateFormat formatter = new SimpleDateFormat(pattern,Locale.US);   
              try   {   
                      Date date = formatter.parse(dateString);   
                      System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));  
              }   
              catch   (Exception   e)   {   
                      throw   new   RuntimeException(e.getMessage());   
              }
      

  5.   

    String dateString = "Sun, 16 Nov 2008 03:53:12 GMT +8";
      

  6.   

     String dateString = "Sun, 16 Nov 2008 03:53:12 GMT+8";   
              String pattern = "EEE,dd MMM yyyy HH:mm:ss z";   
            
              SimpleDateFormat formatter = new SimpleDateFormat(pattern,Locale.US);   
      

  7.   


    Locale   locale   =   new   Locale("en","US");        
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format,locale);你最大的问题正在这里 这里好像跟不同国家的习惯 有关系;
    "EEE,dd MMM yyyy HH:mm:ss z"; 
    是美国人的习惯, 所以要这么写哦
      

  8.   


             SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
     format.setTimeZone(TimeZone.getTimeZone("GMT-9"));
     System.out.println(format.format(format.parse("Sun, 16 Nov 2008 03:53:12 GMT")));
      

  9.   

       SimpleDateFormat sdf= new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
         sdf.setTimeZone(TimeZone.getTimeZone("GMT-9"));
        Date d = sdf.parse("Sun, 16 Nov 2008 03:53:12 GMT");//转换成Date
     System.out.println(sdf.format(sdf.parse("Sun, 16 Nov 2008 03:53:12 GMT")))
      

  10.   

    不是转换不了,也不是Locale的原因,是你的字符串Sun, 16 Nov 2008 03:53:12 GMT+8 有问题,
    应该是Sun, 16 Nov 2008 03:53:12 GMT+8:00 ,你再转换试一下
      

  11.   

    就是这个Sun, 16 Nov 2008 03:53:12 GMT+8,不是我写的。  这是新浪博客的http响应头的信息,我要解析过来。
    还有,我不只解析它一个,我还要通用性。
    求高手出手相救
      

  12.   

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
    String s = sdf.format(new Date());
    System.out.println(s);
      

  13.   

    说实话,我也不是很懂,感觉分解字符串太麻烦了,不过你可以参考SimpleDateFormat的实现 public Date parse(String text, ParsePosition pos)
        {
            int start = pos.index;
            int oldStart = start;
    int textLength = text.length();        boolean[] ambiguousYear = {false};        calendar.clear(); // Clears all the time fields        for (int i = 0; i < compiledPattern.length; ) {
                int tag = compiledPattern[i] >>> 8;
        int count = compiledPattern[i++] & 0xff;
        if (count == 255) {
    count = compiledPattern[i++] << 16;
    count |= compiledPattern[i++];
        }     switch (tag) {
        case TAG_QUOTE_ASCII_CHAR:
    if (start >= textLength || text.charAt(start) != (char)count) {
        pos.index = oldStart;
        pos.errorIndex = start;
        return null;
    }
    start++;
    break;     case TAG_QUOTE_CHARS:
    while (count-- > 0) {
        if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
    pos.index = oldStart;
    pos.errorIndex = start;
    return null;
        }
        start++;
    }
    break;     default:
    // Peek the next pattern to determine if we need to
    // obey the number of pattern letters for
    // parsing. It's required when parsing contiguous
    // digit text (e.g., "20010704") with a pattern which
    // has no delimiters between fields, like "yyyyMMdd".
    boolean obeyCount = false;
    if (i < compiledPattern.length) {
        int nextTag = compiledPattern[i] >>> 8;
        if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
    obeyCount = true;
        }
    }
    start = subParse(text, start, tag, count, obeyCount,
     ambiguousYear, pos);
    if (start < 0) {
        pos.index = oldStart;
        return null;
    }
        }
    }        // At this point the fields of Calendar have been set.  Calendar
            // will fill in default values for missing fields when the time
            // is computed.        pos.index = start;        // This part is a problem:  When we call parsedDate.after, we compute the time.
            // Take the date April 3 2004 at 2:30 am.  When this is first set up, the year
            // will be wrong if we're parsing a 2-digit year pattern.  It will be 1904.
            // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day.  2:30 am
            // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
            // on that day.  It is therefore parsed out to fields as 3:30 am.  Then we
            // add 100 years, and get April 3 2004 at 3:30 am.  Note that April 3 2004 is
            // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
            /*
            Date parsedDate = calendar.getTime();
            if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
                calendar.add(Calendar.YEAR, 100);
                parsedDate = calendar.getTime();
            }
            */
            // Because of the above condition, save off the fields in case we need to readjust.
            // The procedure we use here is not particularly efficient, but there is no other
            // way to do this given the API restrictions present in Calendar.  We minimize
            // inefficiency by only performing this computation when it might apply, that is,
            // when the two-digit year is equal to the start year, and thus might fall at the
            // front or the back of the default century.  This only works because we adjust
            // the year correctly to start with in other cases -- see subParse().
            Date parsedDate;
            try {
                if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
                {
                    // We need a copy of the fields, and we need to avoid triggering a call to
                    // complete(), which will recalculate the fields.  Since we can't access
                    // the fields[] array in Calendar, we clone the entire object.  This will
                    // stop working if Calendar.clone() is ever rewritten to call complete().
                    Calendar savedCalendar = (Calendar)calendar.clone();
                    parsedDate = calendar.getTime();
                    if (parsedDate.before(defaultCenturyStart))
                    {
                        // We can't use add here because that does a complete() first.
                        savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
                        parsedDate = savedCalendar.getTime();
                    }
                }
                else parsedDate = calendar.getTime();
            }
            // An IllegalArgumentException will be thrown by Calendar.getTime()
            // if any fields are out of range, e.g., MONTH == 17.
            catch (IllegalArgumentException e) {
                pos.errorIndex = start;
                pos.index = oldStart;
                return null;
            }        return parsedDate;
        }
      

  14.   

    对于这样的回答很多很多,要是这么简单的话,我就不用问了。
    看看老外的回答: Convert it to the more standard GMT+08:00 before parsing. 
    这个建议很好,我觉得。我英语表达很不好,但人家都能看懂。
    对于这个帖子,我的意思表达得够准确了。我想你们是瞟了两眼,然后从哪复制一个大概的答案,
    很不负责任的回答!
    话说回来,还是非常非常感谢认真回答的朋友。