首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将EDT/EST转换为UTC

将EDT/EST转换为UTC
EN

Stack Overflow用户
提问于 2013-06-06 20:11:16
回答 1查看 760关注 0票数 1

下面的代码似乎转换成了BST。我是不是做了什么傻事?

代码语言:javascript
复制
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class Test {
    @Test
    public void testTimeInterval(){
        String DAY_SHIFT="08:00-17:15";
        System.out.println(toInterval(DAY_SHIFT, "America/New_York", "UTC"));
    }

    public static final String TIME_FORMAT = "HH:mm";
    public static List<LocalTime> toInterval(String str, String sourceTZ, String destTZ) {
        if (!StringUtils.isBlank(str)){
            final DateTimeFormatter timeFormat = DateTimeFormat.forPattern(TIME_FORMAT).withZone(DateTimeZone.forID(sourceTZ));
            String[] tokens = str.split("-");
            if (tokens!=null && tokens.length==2){
                try{
                    long start = timeFormat.parseMillis(tokens[0]);
                    long end = timeFormat.parseMillis(tokens[1]);
                    DateTime startTime = new DateTime(start, DateTimeZone.forID(sourceTZ)).toDateTime(DateTimeZone.forID(destTZ));
                    DateTime endTime = new DateTime(end, DateTimeZone.forID(sourceTZ)).toDateTime(DateTimeZone.forID(destTZ));
                    return Arrays.asList(new LocalTime(startTime, DateTimeZone.forID(destTZ)),
                            new LocalTime(endTime, DateTimeZone.forID(destTZ)));
                }
                catch (IllegalArgumentException e){
                    e.printStackTrace();
                }
            }
        };
        return null;
    }
}

上面的代码打印[13:00:00.000, 22:15:00.000]。根据this链接,这是一个小时的休息,应该是[12:00:00.000, 21:15:00.000]

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-06 20:15:33

您只提供了一天中的某个时间。你对哪一天感兴趣?这将影响到UTC的映射。如果你指的是今天,你应该明确指定。看起来您确实想要解析成一个LocalTime,然后通过将其与一些适当的LocalDate连接来构造一个适当的LocalDateTime (这将取决于您试图实现的目标)。

我的猜测是,它实际上是在1970年1月1日这样做-当时纽约的UTC偏移量是-5,而不是-4。您可以通过完整记录startTimeendTime来验证这一点。

另外,为了简单起见,我强烈建议在方法开始时调用DateTimeZone.forId两次,而不是每次需要一个专区时都调用。

如果本地日期/时间不明确,或者由于DST转换而可能被跳过,您还应该考虑希望发生的情况。如果你选择一个不包含任何过渡的日期,当然这永远不会发生。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16961761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档