LocalDateTime的增加和减少

格式化输出

1
2
3
4
5
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
//加30天
LocalDateTime dueDateTime = localDateTime.plus(30, ChronoUnit.DAYS);
System.out.println(dueDateTime.format(formatter));

计算两个LocalDateTime之间的相差的天数

1
2
3
4
5
6
7
8
//指定一个字符串时间
String startTime = "2019-07-01 00:00:01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(startTime, formatter);
//获取当前时间
LocalDateTime now = LocalDateTime.now();
int days = (int) Duration.between(dateTime, now).toDays();
System.out.println(days);

LocalDate的减少指定的天数、年数等

1
2
3
4
5
6
7
LocalDate localDate = LocalDate.now();
//计算去年
LocalDate previousYear = localDate.minus(1, ChronoUnit.YEARS);
//减30天
LocalDate previousDay = localDate.minus(30, ChronoUnit.DAYS);
//减少6天
LocalDate startTime = localDate.minusDays(6);

LocalDate计算上周日的日期

1
2
3
LocalDate localDate = LocalDate.now();  //当前日期
int week = localDate.getDayOfWeek().getValue(); //计算今天是星期几
LocalDate endTime = localDate.minusDays(week); //计算上周日的日期

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :