LocalDateTime的增加和减少
格式化输出
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之间的相差的天数
//指定一个字符串时间
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的减少指定的天数、年数等
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计算上周日的日期
LocalDate localDate = LocalDate.now(); //当前日期
int week = localDate.getDayOfWeek().getValue(); //计算今天是星期几
LocalDate endTime = localDate.minusDays(week); //计算上周日的日期
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/14/localdatetime-increase-decrease/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
LocalDateTime的增加和减少
格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.n……
文章目录
关闭
共有 0 条评论