为什么在这种情况下日期会被错误地解释

时间:2016-01-13 04:40:10

标签: java

我有一组rss feed,我使用rssmix将它们组合在一起。

我面临日期问题,日期与实际新闻提供商不匹配以及通过rssmix获得的日期

实际新闻提供者提供的日期是

2016年1月13日,上午9点12分

通过rssmix我得到这个日期

2016-01-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();
    }

}

请您告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您希望GMT(UTC)中的时间转换为亚洲/加尔各答。时差为+5.30小时。因此Wed, 13 Jan 2016 02:30:22 +0000将为2016-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));