LocalDateTime
- LocalDateTime.now();
获取当前时间
LocalDateTime ldt = LocalDateTime.now();
输出
2018-06-25T09:43:04.386
- LocalDateTime.of()
获取指定的 LocalDateTime 实例
LocalDateTime ldt = LocalDateTime.of(2015, 03,21,22,33,22,33);
- plus
增加日期
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime localDateTime = ldt.plusYears(2).plusDays(1);
- minus
减日期
LocalDateTime localDateTime = ldt.minusMonths(3).minusHours(3);
- with
指定年月日
// 将ldf1 的日指定为3,年指定为2016年
LocalDateTime localDateTime2 = ldf1.withDayOfMonth(3).withYear(2016);
- Duration.between
时间之间的间隔
Duration between = Duration.between(ldt1, ldt2);
between.toDays(); // 获取间隔的天数
between.toHours(); // 获取间隔的小时
between.toMillis(); // 获取间隔的秒
between.toMinutes(); // 获取间隔的分钟
- LocalDateTime 转 String
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
String format = dtf.format(now); // 20180625 10:46:25
- String 转 LocalDateTime
LocalDateTime parse = LocalDateTime.parse(format1, dtf); // 2018-06-25T10:46:25
LocalDate
- Period.between()
获取连个日期之间的周期
LocalDate localDate1 = LocalDate.of(2015,05,21);
LocalDate localDate2 = LocalDate.of(2018,06,25);
Period period = Period.between(localDate1, localDate2);
period.getDays(); // 4天
period.getMonths(); // 1个月
period.getYears(); // 3年