LocalDateTime 和LocalDate。
获取当前日期:LocalDate now = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
获取前一天:now.minusDays(1);
获取前一周:now.minusWeeks(1);获取前一月:now.minusMonths(1);获取前一年:now.minusYears(1);
获取后一天:now.plusDays(1);
获取后一周:now.plusWeeks(1);获取后一月:now.plusMonths(1);获取后一年:now.plusYears(1);
获取当前时间:LocalDateTime nowTime = LocalDate.now();
获取前一小时:nowTime.minusHours(1);
获取前一分钟:nowTime.minusMinutes(1);获取前30秒:nowTime.minusSeconds(30);
获取后一小时:now.plusHours(1);
获取后一分钟:now.plusMinutes(1);获取后30秒:now.plusSeconds(30);nowTime也可以获取前一天,前一月。。。后一天,后一月。。。
时间格式转换:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String nowTimeString = nowTime.format(format);LocalDateTime nowTimechange = LocalDateTime.parse(nowTimeString,format);
时间比较大小
LocalDateTime time1 = LocalDateTime.of(2018, 5, 23, 8, 20,30); or time1 = LocalDateTime.parse("2018-05-12 08:30:30",format);
LocalDateTime time2 = LocalDateTime.of(2018, 4, 10, 18, 30, 20); or time2 = LocalDateTime.parse("2018-04-20 16:30:30",format);time1.isAfter(time2) truetime1.isEqual(time2) falsetime1.isBefore(time2) false//获取指定的日期
LocalDate specifiedDay = LocalDate.of(2008, 8, 18);