2015-11-01 77 views
-1

我正在阅读使用DOM API的rss订阅源。 由于这种格式如何将日期转换为所需格式

Sun, 01 Nov 2015 06:27:09 +0000 

我需要日期转换成这种格式pubdate的iamreciving日期的一部分

2015-11-01 06:27 

这就是我想

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 


public class Test { 
    public static void main(String[] args) throws ParseException { 
     String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000"); 
      System.out.println(today); 
    } 
    public static String convertdate(String recivieddate) throws ParseException { 
     SimpleDateFormat in = new SimpleDateFormat("E,dd MM yy HH:mm:ss"); 
     Date date = in.parse(recivieddate); 

     SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
     String newdate = out.format(date); 

     return newdate.toString(); 
    } 
} 

但除了我得到是

Exception in thread "main" java.text.ParseException: Unparseable date: "Sun, 01 Nov 2015 06:27:09 +0000" 
    at java.text.DateFormat.parse(Unknown Source) 
    at Test.convertdate(Test.java:13) 
    at Test.main(Test.java:8) 

回答

2

您应该使用E,dd MMM yy HH:mm:ss解析

与此

import java.text.ParseException; 
    import java.text.SimpleDateFormat; 
    import java.util.Date; 

public class test { 
     public static void main(String[] args) throws ParseException { 
      String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000"); 
       System.out.println(today); 
     } 
     public static String convertdate(String recivieddate) throws ParseException { 
      SimpleDateFormat in = new SimpleDateFormat("E,dd MMM yy HH:mm:ss"); 
      Date date = in.parse(recivieddate); 

      SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
      String newdate = out.format(date); 

      return newdate.toString(); 
     } 
    } 

输出 2015年11月1日06:27

+0

非常感谢。 – Kiran

+0

@Kiran欢迎你 – SpringLearner

+0

@基兰,如果这个答案解决了你的问题,你应该接受它。 – JonasCz

0

试试这个试试...

package com.paytm.pg.common; 

     import java.text.ParseException; 
     import java.text.SimpleDateFormat; 
     import java.util.Date; 


     public class Test { 
      public static void main(String[] args) throws ParseException { 
       String today = convertdate("Sun, 01 Nov 2015 06:27:09 +0000"); 
        System.out.println(today); 
      } 
      public static String convertdate(String recivieddate) throws ParseException { 
       SimpleDateFormat in = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss"); 
       Date date = in.parse(recivieddate); 

       SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
       String newdate = out.format(date); 

       return newdate.toString(); 
      } 
     } 
+1

“HH:MM:SS”? M和S在这里是错误的。 – Henry

+0

感谢亨利指出! –

0

如果您不介意使用库,您可以在不指定格式的情况下解析日期。

import org.pojava.datetime.DateTime; 
import java.util.TimeZone; 

public static void main(String[] args) { 
    TimeZone utc=TimeZone.getTimeZone("UTC"); 
    String formatted=DateTime.parse("Sun, 01 Nov 2015 06:27:09 +0000") 
     .toString("yyyy-MM-dd HH:mm", utc)); 
    // 2015-11-01 06:27 
} 
相关问题