import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

public class JDK8时间 {
    public static void main(String[] args) {
        //jdk8新增的时间类的时间都是不可变的,都是返回一个新的对象
    /*
    static Set<String> availableZoneIds() 获取所有可用的时区
   static ZoneId systemDefault() 获取系统默认的时区
   static ZoneId of(String zoneId) 根据指定的时区ID获取对应的时区对象
     */

        //1.获取所有可用的时区
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        System.out.println(zoneIds);

        //2.获取系统默认的时区
        ZoneId systemDefault = ZoneId.systemDefault();
        System.out.println(systemDefault);//Asia/Shanghai

        //3.根据指定的时区ID获取对应的时区对象
        ZoneId of = ZoneId.of("Africa/Nairobi");
        System.out.println(of);//Africa/Nairobi


        /*
        instant 时间戳
        static Instant now() 获取当前时间戳对象
        static Instant OfXxxx() 根据(秒/毫秒/纳秒)获取当前时间戳对象
        ZonedDateTime atZone(ZoneId zone) 根据指定的时区获取对应的ZonedDateTime对象
        boolean isXxx(Instant otherInstant) 判断是否是同一个时间戳
        Instant minusXxx(long amountToSubtract) 减少时间戳
        Instant plusXxx(long amountToAdd) 增加时间戳
         */

        //1.获取当前时间戳对象(默认时区)
        Instant now=Instant.now();
        System.out.println(now);//2025-05-17T04:39:26.989530Z
        //2.根据(秒/毫秒/纳秒)获取当前时间戳对象
        Instant t1=Instant.ofEpochSecond(60*60*24*365L);
        System.out.println(t1);//1971-01-01T00:00:00Z
        //3.根据指定的时区获取对应的ZonedDateTime对象(非静态要用对象调用,不能直接用类名调用)
        Instant now1=Instant.now();
        ZonedDateTime z1 = now1.atZone(ZoneId.systemDefault());
        System.out.println(z1);//2025-05-17T12:51:02.413274800+08:00[Asia/Shanghai]
        //4.判断是否是同一个时间戳
       Instant now2=Instant.ofEpochSecond(100000L);
       Instant now3=Instant.ofEpochSecond(10000L,19999999999L);
       boolean flag=now2.isAfter(now3);
       System.out.println(flag);//true :now2>now3
        boolean flag1=now2.isBefore(now3);
        System.out.println(flag1);//false :now2>now3

        //5.减少时间戳
        Instant now4=Instant.now();
        Instant t4 = now4.minusSeconds(10000L);
        System.out.println(t4);//2025-05-17T02:14:02.631393300Z
        Instant t5 = now4.plusSeconds(100000L);
        System.out.println(t5);//2025-05-18T08:47:22.631393300Z

        /*
        ZoneDateTime 带时区的日期时间
        对ZonedDateTime进行修改,会返回一个新的对象,不会修改本身

        static ZonedDateTime now() 获取当前带时区的日期时间对象
        static ZonedDateTime ofxxx() 获取指定的带时区的日期时间对象
        ZonedDateTime withxxx() 修改时间系列
        ZonedDateTime minusxxx() 减少时间系列
        ZonedDateTime plusxxx() 增加时间系列
         */

        //1.获取当前带时区的日期时间对象
        ZonedDateTime zone1 = ZonedDateTime.now();
        //2.获取指定的带时区的日期时间对象
        ZonedDateTime zone2 = ZonedDateTime.of(2025, 5, 17, 13, 8, 11, 111, ZoneId.of("Asia/Shanghai"));
        System.out.println(zone1);//2025-05-17T13:08:59.893234400+08:00[Asia/Shanghai]
        System.out.println(zone2);//2025-05-17T13:08:11.000000111+08:00[Asia/Shanghai]
        //Instant+时区获取对象
        Instant now5=Instant.ofEpochSecond(100000L);
        ZonedDateTime zone3 = ZonedDateTime.ofInstant(now5, ZoneId.systemDefault());
        System.out.println(zone3);//1970-01-02T11:46:40+08:00[Asia/Shanghai]
        //3.修改时间系列(不会修改本身,而是返回一个新的对象)
        ZonedDateTime zone4 = zone3.withYear(2000);
        System.out.println(zone4);//2000-01-02T11:46:40+08:00[Asia/Shanghai]
        //4.减少时间系列
        ZonedDateTime zone5 = zone4.minusYears(30);
        System.out.println(zone5);//1970-01-02T11:46:40+08:00[Asia/Shanghai]
        //5.增加时间系列
        ZonedDateTime zone6 = zone5.plusYears(30);
        System.out.println(zone6);//2000-01-02T11:46:40+08:00[Asia/Shanghai]

        /*
        DateTimeFormatter 格式化器

        static DateTimeFormatter ofPattern(String pattern) 获取指定格式的格式化器
        String format(时间对象) 格式化时间
         */

        //获得时间对象
        ZonedDateTime zone10 = ZonedDateTime.now(ZoneId.systemDefault());
        //1.获取指定格式的格式化器
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE");
        //2.格式化时间
        String str=dtf.format(zone10);
        System.out.println(str);//2025-05-17 13:23:30 周六
    }
}
------------------------------------------------------------
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.MonthDay;

public class JDK8时间2 {
    public static void main(String[] args) {
        /*
        LocalDate:获取年月日时间对象
         */
        //1.获取当前年月日
        LocalDate ld=LocalDate.now();
        System.out.println(ld);//2025-05-17
        //2.获取指定年月日
        LocalDate ld2 = LocalDate.of(2025, 5, 17);
        System.out.println(ld2);//2025-05-17
        //3.获取年月日的某个属性
        int year=ld.getYear();
        int month=ld.getMonthValue();
        int day=ld.getDayOfMonth();
        System.out.println(year);//2025
        System.out.println(month);//5
        System.out.println(day);//17
        DayOfWeek dayOfWeek=ld.getDayOfWeek();
        int value=dayOfWeek.getValue();
        System.out.println(dayOfWeek);//SATURDAY
        System.out.println(value);//6
        //with,set,plus,minus...(与jdk7的Calendar类的方法类似)

        //判断是不是生日
        LocalDate birth = LocalDate.of(2006, 8, 11);
        LocalDate now1 = LocalDate.now();

        MonthDay birMD=MonthDay.of(birth.getMonth(),birth.getDayOfMonth());
        MonthDay nowMD = MonthDay.from(now1);
        boolean flag=birMD.equals(nowMD);
        System.out.println(flag);//false

        /*
        LocalTime:获取时分秒时间对象
        //其他和LocalDate类似
         */
        /*
        LocalDateTime:获取年月日时分秒时间对象
        //其他和LocalDate类似
         */
    }
}
------------------------------------------------------------
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class JDK8时间3 {
    public static void main(String[] args) {
        /*
        Period计算年月日之间的间隔
         */
        LocalDate ld1 = LocalDate.now();
        LocalDate ld2 = LocalDate.of(2024, 1, 1);
        Period p1 = Period.between(ld2, ld1);
        System.out.println("相差时间对象"+p1);
        System.out.println("相差的年"+p1.getYears());
        System.out.println("相差的月"+p1.getMonths());
        System.out.println("相差的日"+p1.getDays());

        System.out.println("相差的总月数"+p1.toTotalMonths());

        /*
        Duration计算时分秒之间的间隔
         */
        LocalDateTime ldt1 = LocalDateTime.now();
        LocalDateTime ldt2 = LocalDateTime.of(2024, 1, 1, 1, 1, 1);
        Duration d1 = Duration.between(ldt2, ldt1);
        System.out.println("相差时间对象"+d1);
        System.out.println("相差总天数"+d1.toDays());
        System.out.println("相差总小时数"+d1.toHours());
        System.out.println("相差总分钟数"+d1.toMinutes());
        System.out.println("相差总秒数"+d1.getSeconds());
        System.out.println("相差总毫秒数"+d1.toMillis());

        /*
        ChronoUnit计算两个时间的间隔
         */
        System.out.println("相差的世纪数"+ ChronoUnit.CENTURIES.between(ldt2,ldt1));
        System.out.println("相差的十年数"+ ChronoUnit.DECADES.between(ldt2,ldt1));
        System.out.println("相差的年数"+ ChronoUnit.YEARS.between(ldt2,ldt1));
        System.out.println("相差的月数"+ ChronoUnit.MONTHS.between(ldt2,ldt1));
        System.out.println("相差的天数"+ ChronoUnit.DAYS.between(ldt2,ldt1));
        System.out.println("相差的小时数"+ ChronoUnit.HOURS.between(ldt2,ldt1));
        System.out.println("相差的分钟数"+ ChronoUnit.MINUTES.between(ldt2,ldt1));
        System.out.println("相差的秒数"+ ChronoUnit.SECONDS.between(ldt2,ldt1));
        System.out.println("相差的毫秒数"+ ChronoUnit.MILLIS.between(ldt2,ldt1));
        System.out.println("相差的纳秒数"+ ChronoUnit.NANOS.between(ldt2,ldt1));
    }
}