public class LocalZoneUtils {
private final static DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final static DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
*
* @Description 根据时区+timestamp转换成系统时间
* @Date 17:48 2019/12/5
* @Param [timeZone, createTime]
* @return java.lang.String
**/
public static Date convertTargetDate(String timeZone, String dateStr) throws ParseException {
Objects.requireNonNull(dateStr, "parseZonedDateTimeViaDate dateStr must not be null.");
return convertTargetTimeZoneDate(null, timeZone, DEFAULT_DATE_FORMAT.parse(dateStr));
}
/**
* 转换为系统时间
*
* @param timeZone
* @param dateStr
* @return
*/
public static Date convertLocalDate(String timeZone, String dateStr) throws ParseException {
Objects.requireNonNull(timeZone, "parseZonedDateTimeViaDate timeZone must not be null.");
Objects.requireNonNull(dateStr, "parseZonedDateTimeViaDate dateStr must not be null.");
return convertTargetTimeZoneDate(timeZone, null, DEFAULT_DATE_FORMAT.parse(dateStr));
}
public static Date convertTargetTimeZoneDate(String sourceZoneId, String targetZoneId, Date sourceDate)
throws ParseException {
if (sourceDate == null) {
return null;
}
ZoneId sourceZone;
if (sourceZoneId == null) {
sourceZone = ZoneId.systemDefault();
} else {
sourceZone = ZoneId.of(sourceZoneId);
}
ZoneId targetZone;
if (targetZoneId == null) {
targetZone = ZoneId.systemDefault();
} else {
targetZone = ZoneId.of(targetZoneId);
}
ZonedDateTime sourceZonedDateTime = ZonedDateTime.ofInstant(sourceDate.toInstant(), sourceZone);
ZonedDateTime targetZonedDateTime = sourceZonedDateTime.withZoneSameInstant(targetZone);
//处理重叠问题
long hours = Duration.between(targetZonedDateTime.withEarlierOffsetAtOverlap(),
targetZonedDateTime.withLaterOffsetAtOverlap()).toHours();
targetZonedDateTime = targetZonedDateTime.plusHours(hours);
return Date.from(targetZonedDateTime.toLocalDateTime().atZone(sourceZone).toInstant());
}
public static void main(String[] args) throws ParseException {
//预计不在夏令时 2016-03-13 01:59:59 Date date1 = convertTargetTimeZoneDate(null, "America/New_York", DEFAULT_DATE_FORMAT.parse("2016-03-13 14:59:59")); System.out.println(DateUtil.format(date1, "yyyy-MM-dd HH:mm:ss")); //预计在夏令时 2016-03-13 03:00:00 Date date2 = convertTargetTimeZoneDate(null, "America/New_York", DEFAULT_DATE_FORMAT.parse("2016-03-13 15:00:00")); System.out.println(DateUtil.format(date2, "yyyy-MM-dd HH:mm:ss")); //预计在夏令时 2016-11-06 02:59:59 Date date3 = convertTargetTimeZoneDate(null, "America/New_York", DEFAULT_DATE_FORMAT.parse("2016-11-06 14:59:59")); System.out.println(DateUtil.format(date3, "yyyy-MM-dd HH:mm:ss")); //预计不在夏令时2016-11-06 02:00:00 Date date4 = convertTargetTimeZoneDate(null, "America/New_York", DEFAULT_DATE_FORMAT.parse("2016-11-06 15:00:00")); System.out.println(DateUtil.format(date4, "yyyy-MM-dd HH:mm:ss")); //预计不在夏令时 2016-03-13 01:59:59 Date date5 = convertTargetTimeZoneDate("America/New_York", null, DEFAULT_DATE_FORMAT.parse("2016-03-13 01:59:59")); System.out.println(DateUtil.format(date5, "yyyy-MM-dd HH:mm:ss")); //预计在夏令时 2016-03-13 03:00:00 Date date6 = convertTargetTimeZoneDate("America/New_York", null, DEFAULT_DATE_FORMAT.parse("2016-03-13 03:00:00")); System.out.println(DateUtil.format(date6, "yyyy-MM-dd HH:mm:ss"));
}
参考文献: