时间下载无法转换为GMT时间

时间:2017-02-19 10:38:58

标签: java datetime

我的下载文件概念如下: 如果适配器在早上7点重新启动,则 首次下载将从00:00:00开始至早上7点。 5分钟后会再次下载,下载时间也是6.58am到7.05am。 Timezome会有所不同,如亚洲/新加坡,亚洲/香港等。 但出于某种目的,这个时区需要转换为GMT。

以下是我的编码

private String TIME_ZONE_ID = "America/New_York";
private final String TIME_ZONE_ID_TRKD_DEFAULT = "Etc/GMT";
public void run() {
    // TODO Auto-generated method stub
    if (sessionFactory.fileServer == null)
        throw new NullPointerException("sessionfactory.fileServer is null");
    // hsding@debug
    out.println("d> 2.1");

    while (true && isStarted) {
        try {
            if (!isConnected) {
                out.println("d> 2.2");
                openTransport(); // Connect
            }// end if
        } catch (Exception e) {
            e.printStackTrace();
            out.println(expMsg + " run():" + e.getMessage());
        }

        try {
            boolean isValid = getTokenValidity();
            if (isConnected) {
                // boolean isValid = getTokenValidity();

                out.println("Is Token Valid = [" + isValid + "]");
                if (isValid) {
                    try {
                        for (int i = 0; i < garrNewsFilterCriteria.length; i++) {
                            giCurrSubscribeSeqNo = i;

                            // add in keep track of last download time. hsding@debug@2016041x
                            if (garrLastNewsSubscribeStartTime[i] == null) {
                                out.println("garrLastNewsSubscribeStartTime= " + garrLastNewsSubscribeStartTime[i]);
                                garrLastNewsSubscribeStartTime[i] = getStartDateTime(true);
                            } else {
                                out.println("garrLastNewsSubscribeStartTime2= " + garrLastNewsSubscribeStartTime[i]);
                                garrLastNewsSubscribeStartTime[i] = getStartDateTime();
                            }



                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(SLEEP_TIME);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    out.println("Token not valid!!!");
                    isConnected = false;
                }
            } else
                isConnected = false;

        } catch (Exception e) {
            e.printStackTrace();
            // TODO Auto-generated catch block
            out.println(expMsg + " run():" + e.getMessage());
        }
    }
}
private XMLGregorianCalendar getStartDateTime() {
    return getStartDateTime(false);
}

private XMLGregorianCalendar getStartDateTime(boolean startFromZeroHour) {
    XMLGregorianCalendar xmlCal = null;
    try {
        xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
    }

    // hsding@debug@20160419. enhance on the retrieve time FROM and TO.
    // Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Canada/Eastern"));

    Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"));
    out.println("D> now 0=" + now);
    if (startFromZeroHour) {
        // ??????
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
    } else {
        now.set(Calendar.SECOND, 0); // pre offset the second to zero for easy the polling job in future.
        int fallback_prev_N_second = (int) (SLEEP_TIME * 2) / 1000 * -1;
        now.add(Calendar.SECOND, fallback_prev_N_second);
    }

    out.println("D> now 1=" + now);
    now.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_ID_TRKD_DEFAULT));
    // hsding@debug@2016041x
    out.println("D> now 2=" + now);

    xmlCal.setDay(now.get(Calendar.DAY_OF_MONTH));
    xmlCal.setMonth(now.get(Calendar.MONTH) + 1);
    xmlCal.setYear(now.get(Calendar.YEAR));
    xmlCal.setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));

    out.println("D> xmlCal=" + xmlCal);

}

private XMLGregorianCalendar getEndDateTime() {
    XMLGregorianCalendar xmlCal = null;
    int iDay = 0;
    int iMonth = 0;
    try {
        xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
    }
    now.get(Calendar.MINUTE), now.get(Calendar.SECOND));

    // out.println("News Retrieve [End Time] =" + xmlCal.toGregorianCalendar().getTime());

    // hsding@debug@20160419. enhance on the retrieve time FROM and TO.
    // Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Canada/Eastern"));

    Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"));
    out.println("D> End time =" + now);
    now.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_ID_TRKD_DEFAULT));
    out.println("D> End time2 =" + now);
    xmlCal.setDay(now.get(Calendar.DAY_OF_MONTH));
    // xmlCal.setDay(12);
    // We do a +1 below because XMLGregorianCalendar goes from 1 to 12
    // while Calendar.MONTH goes from 0 to 11 !!!
    xmlCal.setMonth(now.get(Calendar.MONTH) + 1);
    // xmlCal.setMonth(1);
    xmlCal.setYear(now.get(Calendar.YEAR));

    xmlCal.setTime(now.get(Calendar.HOUR), now.get(Calendar.MINUTE), 59);
    out.println("D> End time xmlcal  =" + xmlCal);
    return xmlCal;
}
 private void getNewsHeaderFromReuters(String sParam01, String sParam02, XMLGregorianCalendar xmlParam03) throws Exception {

try{
        RetrieveHeadlineMLRequest1 headlineRequest = new RetrieveHeadlineMLRequest1();

        NewsRequest newsRequest = new NewsRequest();

        Filter filter = new Filter();

        MetaDataConstraint constraint = new MetaDataConstraint();



        newsRequest.setStartTime(xmlParam03);

        newsRequest.setEndTime(getEndDateTime());


        out.println("News Retrieve [START Time]=" + newsRequest.getStartTime());
        out.println("News Retrieve [END Time]=" + newsRequest.getEndTime());

        headlineRequest.setHeadlineMLRequest(newsRequest);
        RetrieveHeadlineMLResponse1 headlineResponse;

        int i = 0;

        try {
            String sNewsID = "";
            boolean isProcess = true;

            headlineResponse = newsPort.retrieveHeadlineML1(headlineRequest);
            out.println("SOAP request :  " + messageDumper.nextMessage());
            out.println("SOAP response :  " + messageDumper.nextMessage());

    } catch (Exception e) {
        e.printStackTrace();
        out.println(expMsg + " getNewsHeaderFromReuters()" + e.getMessage());
        isConnected = false;
        throw new Exception(e.getMessage());
    }
}

我用例子来表示我的问题: 当适配器在新加坡时间的2017-02-18 7am重新启动时,转换为GMT后的开始时间为2017-02-18 00:00:00,但结束时间为2017-02-17 11:50:34。 这是错误的..

当适配器在2017-02-18上午8点之后重新启动时,新加坡时间,转换为GMT后开始时间为2017-02-18 00:00:00,但结束时间为2017-02-18 00:05: 34(最多转换为GMT的时间)。但这是正确的..

如果我没有转换为GMT,所有时间都是正确的,但我不想这个答案,因为它会影响一些数据,所以它需要先转换为GMT。

请告知我的编码错误...

谢谢

此致 沙龙

0 个答案:

没有答案