Java8 时间类

Java8 时间类


时间戳 Instant

        // 时间戳  时间线上的瞬时点
        System.out.println("*********************时间戳*********************");
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault());
        System.out.println(zonedDateTime);

Java8 时间类

本地日期 LocalDate

        //获取当前的日期 本地日期
        System.out.println("*********************LocalDate 本地日期*********************");
        LocalDate now = LocalDate.now();
        System.out.println(now);

        //这个是作为对比
        Date date = new Date();
        System.out.println(date);

        //获取年、月、日信息
        System.out.println("年: " + now.getYear());
        System.out.println("月: " + now.getMonthValue());
        System.out.println("日: " + now.getDayOfMonth());

        //处理特定日期
        System.out.println("-------------------特定日期--------------------");
        LocalDate dateOfBirth = LocalDate.of(2018, 07, 7);
        System.out.println("The specil date is : " + dateOfBirth);

        //判断两个日期是否相等
        System.out.println("-------------------检查像生日这种周期性事件--------------------");
        LocalDate today = LocalDate.now();
        LocalDate dateOfBirth2 = LocalDate.of(2018, 8, 5);

        MonthDay birthday = MonthDay.of(dateOfBirth2.getMonth(), dateOfBirth2.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);

        if (currentMonthDay.equals(birthday)) {
            System.out.println("Many Many happy returns of the day !!");
        } else {
            System.out.println("Sorry, today is not your birthday");
        }

        //如何计算一个星期之后的日期
        System.out.println("-------------------如何计算一个星期之后的日期--------------------");
        LocalDate today2 = LocalDate.now();
        LocalDate nextWeek = today2.plus(1, ChronoUnit.WEEKS);    //使用变量赋值
        System.out.println("Today is : " + today2);
        System.out.println("Date after 1 week : " + nextWeek);

        //计算一年前或一年后的日期
        System.out.println("-------------------计算一年前或一年后的日期--------------------");
        LocalDate today3 = LocalDate.now();
        LocalDate previousYear = today3.minus(1, ChronoUnit.YEARS);
        System.out.println("Date before 1 year : " + previousYear);

        LocalDate nextYear = today3.plus(1, ChronoUnit.YEARS);
        System.out.println("Date after 1 year : " + nextYear);

        //判断日期是早于还是晚于另一个日期
        System.out.println("-------------------判断日期是早于还是晚于另一个日期--------------------");
        LocalDate today4 = LocalDate.now();

        LocalDate tomorrow = LocalDate.of(2021, 11, 29);
        if (tomorrow.isAfter(today4)) {
            System.out.println("Tomorrow comes after today");
        }

        //减去一天
        LocalDate yesterday = today4.minus(1, ChronoUnit.DAYS);

        if (yesterday.isBefore(today4)) {
            System.out.println("Yesterday is day before today");

        }

Java8 时间类

当前时间 LocalTime

        //获取当前时间
        System.out.println("*********************LocalTime 获取当前时间*********************");
        LocalTime now1 = LocalTime.now();
        System.out.println(now1);

        //在现有的时间上增加小时
        System.out.println("-------------------在现有的时间上增加小时--------------------");
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusHours(3); // 增加两小时
        System.out.println("Time after 3 hours : " + newTime);

Java8 时间类

时钟类 Clock


        //使用Java 8的Clock时钟类
        System.out.println("*********************Clock 时钟类*********************");
        Clock clock = Clock.systemUTC();
        System.out.println("Clock : " + clock);

        // 根据系统时钟区域返回时间
        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("Clock : " + clock);

Java8 时间类

处理时区 LocalDateTime

 //处理时区
        System.out.println("*********************LocalDateTime 处理时区*********************");
        //设置时区
        ZoneId america = ZoneId.of("America/New_York");

        LocalDateTime localtDateAndTime = LocalDateTime.now();

        ZonedDateTime dateAndTimeInNewYork  = ZonedDateTime.of(localtDateAndTime, america );
        System.out.println("现在的日期和时间在特定的时区 : " + dateAndTimeInNewYork);

Java8 时间类

上一篇:java学习路线


下一篇:Java8 Optional包装类