LocalDate today =LocalDate.now();LocalDate before =LocalDate.of(2017,9,1);if(today.equals(before))System.out.println("相等");elseSystem.out.println("不相等");
LocalDate now =LocalDate.now();//当前日期:2024-05-06System.out.println("当前日期:"+now);//一周后的日期:2024-05-13System.out.println("一周后的日期:"+now.plusWeeks(1));//一周前的日期:2024-04-29System.out.println("一周前的日期:"+now.minusWeeks(1));//一月后的日期:2024-06-06System.out.println("一月后的日期:"+now.plusMonths(1));//一月前的日期:2024-04-06System.out.println("一月前的日期:"+now.minusMonths(1));//一年后的日期:2025-05-06System.out.println("一年后的日期:"+now.plusYears(1));//一年前的日期:2023-05-06System.out.println("一年前的日期:"+now.minusYears(1));
LocalDate now =LocalDate.now();System.out.println("今天是:"+now);//今天是:2024-05-06LocalDate day =LocalDate.of(2024,5,6);LocalDate yesterday =LocalDate.of(2024,5,5);LocalDate tomorrow =LocalDate.of(2024,5,7);if(now.isAfter(day)){//now和day是同一天,所以不会输出System.out.println("now是day之后的日期");}if(now.isBefore(day)){//now和day是同一天,所以不会输出System.out.println("now是day之前的日期");}if(now.isAfter(yesterday)){//正常输出 now是yesterday之后的日期System.out.println("now是yesterday之后的日期");}if(now.isBefore(tomorrow)){//正常输出 now是tomorrow之前的日期System.out.println("now是tomorrow之前的日期");}
时区
ZoneId america =ZoneId.of("America/New_York");LocalDateTime localtDateAndTime =LocalDateTime.now();//当前本地时间:2024-05-06T16:52:11.234393300System.out.println("当前本地时间:"+localtDateAndTime);ZonedDateTime dateAndTimeInNewYork =ZonedDateTime.of(localtDateAndTime, america );//TODO: 这一块输出的时间应该和currentDateTimeInNY一样吧。//把当前时间转换成纽约时区的时间 New_York Time Zone : 2024-05-06T16:52:11.234393300-04:00[America/New_York]System.out.println("New_York Time Zone : "+ dateAndTimeInNewYork);ZonedDateTime currentDateTimeInNY =ZonedDateTime.now(ZoneId.of("America/New_York"));//直接获取纽约时区的时间 Current Time in New York: 2024-05-06T04:52:11.237388200-04:00[America/New_York]System.out.println("Current Time in New York: "+ currentDateTimeInNY);
闰年判断
LocalDate now =LocalDate.now();if(now.isLeapYear()){// 闰年System.out.println("闰年");}else{System.out.println("平年");}LocalDate leapYear =LocalDate.of(2018,2,5);if(leapYear.isLeapYear()){System.out.println("闰年");}else{// 平年System.out.println("平年");}