简体   繁体   中英

Converting EDT/EST to UTC

The code below seems to convert to BST instead. Am I doing something silly?

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;
    }
}

The above code prints [13:00:00.000, 22:15:00.000] . According to this link, it's an hour off should be [12:00:00.000, 21:15:00.000]

You've only provided a time of day. What day are you interested in? That will affect the mapping to UTC. If you mean today , you should specify that explicitly. It looks like you really want to parse to a LocalTime , then construct an appropriate LocalDateTime by joining that with some appropriate LocalDate (which will depend on what you're trying to achieve).

My guess is that it's actually doing it for January 1st 1970 - at which point the UTC offset of New York was -5, not -4. You can verify that by logging startTime and endTime in full.

Also for simplicity, I'd strongly recommend calling DateTimeZone.forId just twice, at the start of the method instead of every time you need a zone.

You should also consider what you want to happen if the local date/time is ambiguous, or potentially skipped due to DST transitions. If you pick a date which doesn't include any transitions, this will never happen of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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