繁体   English   中英

将EDT / EST转换为UTC

[英]Converting EDT/EST to UTC

下面的代码似乎改为转换为BST。 我在做傻事吗?

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] 根据链接,应有一个小时的休假时间: [12:00:00.000, 21:15:00.000]

提供了一天中的时间。 您对哪一天感兴趣? 这将影响到UTC的映射。 如果您今天的意思是,则应明确指定。 它看起来像你真的想解析到LocalTime ,然后构建一个合适的LocalDateTime通过加入与一些适当的LocalDate (这将取决于你想要什么来实现)。

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

同样为简单起见,强烈建议在方法开始时两次调用DateTimeZone.forId ,而不是每次需要区域时调用一次。

如果本地日期/时间模棱两可,或者由于DST转换而可能被跳过,则还应考虑要发生的情况。 如果您选择的日期不包含任何过渡,则当然不会发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM