package com.fdkankan.common.util;

import cn.hutool.core.date.DateUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.TimeZone;
import lombok.SneakyThrows;

public class DateExtUtil extends DateUtil {

    public static final String dateStyle = "yyyy-MM-dd HH:mm:ss";
    public static final String dateStyle2 = "HH:mm:ss";
    public static final String dateStyle3 = "HH:mm";
    public static final String dateStyle4 = "yyyy-MM-dd";
    public static final String dateStyle5 = "yyyy-MM";
    public static final String dateStyle6 = "yyyyMMdd";
    public static final String dateStyle7 = "yyMMddHHmm";
    public static final String dateStyle8 = "yyyy-MM-dd HH:mm";
    public static final String dateStyle9 = "yyyy.MM.dd";
    public static final String dateStyle10 = "yyyyMM";
    public static final String dateStyle11 = "yyyyMMddHHmmssSSS";

    public static Date hoursCalculate(Date beginDate, int hours) {
        return timesCalculate(beginDate, hours, GregorianCalendar.HOUR);
    }

    public static Date timesCalculate(Date beginDate, int times, int type) {
        /*
         * GregorianCalendar类的add(int field,int amount)方法表示年月日加减.
         * field参数表示年,月,周,日等. amount参数表示要加减的数量.
         */
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(beginDate);
        gc.add(type, times);
        return gc.getTime();
    }

    @SneakyThrows
    public static void main(String[] args) {
//        DateExtUtil.testInstant();
        DateExtUtil.testTransferBetweenZones();








    }

    //同一时刻，不同的时区的时间展现形式不一样，但是时刻一样，也就是时间戳是一样的

    public static void testOldDate(){
        Date currDate = new Date();
        //国际统一时间字符串格式  Wed Apr 27 16:17:30 CST 2022    CST （china standar time）
        System.out.println(currDate.toString());
        //本地时间字符串格式  2022-4-27 16:17:30
        System.out.println(currDate.toLocaleString());
        //国际统一GMT(格林威治标准时间)字符串格式  27 Apr 2022 08:17:30 GMT
        System.out.println(currDate.toGMTString());

        String[] availableIDs = TimeZone.getAvailableIDs();
        for (String availableID : availableIDs) {
            System.out.println(availableID);
        }

        Date bjDate = new Date();
        System.out.println("时间戳:" + bjDate.getTime());
        //得到纽约的时区
        TimeZone newYorkTimeZone = TimeZone.getTimeZone("America/New_York");
        DateFormat newYorkDateFormat = new SimpleDateFormat(dateStyle);
        newYorkDateFormat.setTimeZone(newYorkTimeZone);
        //纽约时间
        System.out.println("纽约时间:" + newYorkDateFormat.format(bjDate));
        //背景时间
        System.out.println("北京时间:" + new SimpleDateFormat(dateStyle).format(bjDate));

    }

    public static void testInstant(){
        Instant instant = Instant.ofEpochMilli(new Date().getTime());
        Date from = Date.from(instant);
        System.out.println(instant.toEpochMilli());
        System.out.println(from.getTime());
        System.out.println(instant);
        System.out.println(from);

    }

    public static void testTransferBetweenZones(){
        //获取所有时区的id
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();

        //模拟数据库查询出来的时间
        Instant fromdb = Instant.now();

        ZonedDateTime sydney = ZonedDateTime.ofInstant(fromdb, ZoneId.of("Australia/Sydney"));
        ZonedDateTime beijing = ZonedDateTime.ofInstant(fromdb, ZoneId.systemDefault());
        System.out.println(sydney.format(DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss")));
        System.out.println(beijing.format(DateTimeFormatter.ofPattern(dateStyle)));
        for (String availableZoneId : availableZoneIds) {
            System.out.println(availableZoneId);
        }

    }

}
