JDK1.8 時區轉換(包含夏令時)

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"));

    }

參考文獻:

https://blog.csdn.net/u011165335/article/details/76636296/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章