public class LocalDateDemo { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("今天的日期:"+today); //一周后的日期 LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); System.out.println("一周后的日期为:"+nextWeek); //一年前的日期 LocalDate previousYear = today.minus(1, ChronoUnit.YEARS); System.out.println("一年前的日期 : " + previousYear); //一年后的日期 LocalDate nextYear = today.plus(1, ChronoUnit.YEARS); System.out.println("一年后的日期:"+nextYear); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); System.out.println("year:"+year); System.out.println("month:"+month); System.out.println("day:"+day); //获取当前时间 LocalTime time = LocalTime.now(); System.out.println("当前时间为 :"+time); LocalTime newTime = time.plusHours(3); System.out.println("三个小时后的时间为:"+newTime); //Java判断日期是早于还是晚于另一个日期 LocalDate tomorrow = LocalDate.of(2020,2,7); if(tomorrow.isAfter(today)){ System.out.println("之后的日期:"+tomorrow); } LocalDate yesterday = today.minus(1, ChronoUnit.DAYS); if(yesterday.isBefore(today)){ System.out.println("之前的日期:"+yesterday); } //在Java 8中检查闰年 if(today.isLeapYear()){ System.out.println("This year is Leap year"); }else { System.out.println("This year is not a Leap year"); } //获取当前的时间戳 Instant timestamp = Instant.now(); System.out.println("What is value of this instant " + timestamp.toEpochMilli()); //使用预定义的格式化工具去解析或格式化日期 String dayAfterTommorrow = "20200206"; LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE); System.out.println(dayAfterTommorrow+" 格式化后的日期为: "+formatted); //字符串互转日期类型 LocalDateTime date = LocalDateTime.now(); DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //日期转字符串 String str = date.format(format1); System.out.println("日期转换为字符串:"+str); DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //字符串转日期 LocalDate date2 = LocalDate.parse(str,format2); System.out.println("日期类型:"+date2); } }