2016-01-13 113 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