LocalDateTime now = LocalDateTime.now();//获取当前日期System.out.println("now = " + now);//now = 2020-01-27T22:23:33.320System.out.println("now.getYear() = " + now.getYear());//now.getYear() = 2019//注意获取月份的两个方法的区别System.out.println("now.getMonth() = " + now.getMonth());//now.getMonth() = JANUARYSystem.out.println("now.getMonthValue() = " + now.getMonthValue());//now.getMonthValue() = 1System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());//now.getDayOfMonth() = 27System.out.println("now.getHour() = " + now.getHour());System.out.println("now.getMinute() = " + now.getMinute());System.out.println("now.getSecond() = " + now.getSecond());//构造指定日期,eg.2020-08-08LocalDate diyDate = LocalDate.of(2020, 8, 8);System.out.println("diyDate = " + diyDate);//构造指定时间,eg.2020-08-08 00:00:00LocalDateTime diyTime = LocalDateTime.of(2020, 8, 8, 0, 0, 0);System.out.println("diyTime = " + diyTime);//diyDate = 2020-08-08T00:00//修改日期//①增加/减少几个月LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-01-27T22:37:05.546//下个月,plusMonth()localDateTime = localDateTime.plusMonths(1);System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-02-27T22:37:05.546//上个月,-1localDateTime = localDateTime.minusMonths(1);//或者使用 plusMonth(-1)//localDateTime = localDateTime.plusMonths(-1);System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-01-27T22:37:05.546//②修改到指定日期localDateTime = localDateTime.withYear(2200);System.out.println("localDateTime = " + localDateTime);//localDateTime = 2200-01-27T22:43:26.722//格式化日期//①自定义日期格式String diyFormatDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("diyFormatDate = " + diyFormatDate);//formatDate = 2020-01-27 22:47:21//②几种基本格式String format = now.format(DateTimeFormatter.ISO_DATE);System.out.println("format = " + format);//format = 2020-01-27format = now.format(DateTimeFormatter.BASIC_ISO_DATE);System.out.println("format = " + format);//format = 20200127//解析日期LocalDateTime parse = LocalDateTime.parse("2020-01-27 22:47:21", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println("parse = " + parse);//parse = 2020-01-27T22:47:21
