下面的代码似乎改为转换为 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]