2016-01-13 80 views
0

我有一組rss提要,我用rssmix將它們組合在一起。爲什麼在這種情況下錯誤地解釋日期

我面臨着日期的問題,日期被錯配通過rssmix

由實際的新聞提供商提供的日期是

2016年1月13日獲得的實際新聞提供商和日期,上午09時12

凡通過rssmix我得到這個日期

2016年1月13日02:30

我用所有日期的轉換,請看這是我的完整程序

import java.net.URL; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 
import java.util.TimeZone; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.apache.log4j.Logger; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

public class Test { 
    private final static Logger logger = Logger.getLogger(Test.class); 
    private static Test instance = null; 
    private static DocumentBuilder builder = null; 
    private static final long DAY = 43200000; 

    // private static final long DAY = 30400000; 

    public Test() { 
    } 

    public static Test getInstance() { 
     if (instance == null) 
      instance = new Test(); 
     return instance; 
    } 

    public static DocumentBuilder getDocumentBuilderInstance() 
      throws ParserConfigurationException { 
     if (builder == null) 
      builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     return builder; 
    } 

    private static SimpleDateFormat in_newsupdater = new SimpleDateFormat(
      "E,dd MMM yy HH:mm:ss", Locale.ENGLISH); 
    private static SimpleDateFormat out_newsupdater = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm", Locale.ENGLISH); 


    public static void main(String args[]) { 

     try { 
      final JSONArray latestnews = new JSONArray(); 
      builder = getDocumentBuilderInstance(); 
      final URL url = new URL("http://www.rssmix.com/u/8171434/rss.xml"); 
      final Document doc = builder.parse(url.openStream()); 
      final NodeList items = doc.getElementsByTagName("item"); 
      for (int i = 0; i < items.getLength(); i++) { 
       final JSONObject jsonobj_allnews = new JSONObject(); 
       final Element item = (Element) items.item(i); 
       String title = getValue(item, "title"); 

       String link = getValue(item, "link"); 
       String pub_date = getValue(item, "pubDate"); 

       System.out.println("recievied" + pub_date); 

       pub_date = convertdate(pub_date); 

      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
      e.getMessage(); 
     } catch (Throwable e) { 
      e.getMessage(); 
      e.printStackTrace(); 
     } finally { 
     } 
    } 

    public static String convertdate(final String recivieddate) 
      throws ParseException { 

     Date date = in_newsupdater.parse(recivieddate); 
     in_newsupdater.setTimeZone(TimeZone.getTimeZone("GMT")); 
     out_newsupdater.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); 
     return out_newsupdater.format(date); 
    } 

    public static String getValue(final Element parent, final String nodeName) { 
     return parent.getElementsByTagName(nodeName).item(0).getFirstChild() 
       .getNodeValue(); 
    } 

} 

可否請你讓我知道如何解決這個問題。

+0

很抱歉,如果我錯了,我m到處輸出爲2016年1月13日02:30,但如果因爲它應該是2016年1月13日09:12知道(小時和分鐘正確) – Kiran

+0

這並不像所有想到的任何時區或區域設置問題。您的原始發佈時間與從服務器讀取時間戳的時間有沒有可能存在差距?這可能解釋了這種差異。 –

+0

在我的linux服務器中,當我檢查時間顯示正確的IST時間。 m下面是我的服務器的輸出是正確的 – Kiran

回答

0

您希望格林尼治標準時間(UTC)的時間轉換爲亞洲/加爾各答的權利。時差是+5.30小時。所以Wed, 13 Jan 2016 02:30:22 +00002016-01-13 08:00

SimpleDateFormat in_newsupdater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); 
    SimpleDateFormat out_newsupdater = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 

    Date date = in_newsupdater.parse("Wed, 13 Jan 2016 02:30:22 +0000"); 
    out_newsupdater.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); 
    System.out.println(out_newsupdater.format(date)); 
+0

對不起,如果我混淆你,實際數據給出rss提供商是Wed,13 Jan 2016 02:30:22 +0000,我無法直接使用2016年1月13日,09.12 AM(例如SGX Nifty表示積極的開局; IIP,TCS令人失望\t 2016年1月13日星期三02:30: 22 +0000) – Kiran

+0

so'Wed,13 Jan 2016 02:30:22 +0000'應該轉換爲'2016-01-13 09:12'? –

+0

是的,..... – Kiran

相關問題