LocalDateTime 随行笔记

public class Subject {
private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
//当前原生格式时间
System.out.println(“当前原生格式时间:” + currentDateTime);
//当前指定格式时间
String format = currentDateTime.format(FORMATTER);
System.out.println(“当前指定格式时间:” + format);
//当前时间向后推一天
LocalDateTime nextDateTime = currentDateTime.plusDays(1);
System.out.println(“当前时间向后推一天” + nextDateTime);
//当前时间向前推一天
LocalDateTime afterDateTime = currentDateTime.minusDays(1);
System.out.println(“当前时间向前推一天” + afterDateTime);
//当前时间推一天一小时一分钟
LocalDateTime addDateTime = currentDateTime.plusDays(1).plusHours(1).plusMinutes(1);
System.out.println(“当前时间推一天一小时一分钟” + addDateTime);
//判断目标天是否在当前天后
boolean result = currentDateTime.isBefore(addDateTime);
System.out.println(“判断目标天是否在当前天后” + result);
//获取当前月的第一天以及最后一天
LocalDateTime firstday = currentDateTime.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime lastDay = currentDateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(“当月第一天:” + firstday);
System.out.println(“当月最后一天:” + lastDay);
//两个时间相差几天
long days = lastDay.toLocalDate().toEpochDay() - firstday.toLocalDate().toEpochDay();
Period between = Period.between(firstday.toLocalDate(), lastDay.toLocalDate());
System.out.println(“两个时间相差:” + days);
System.out.println(“两个时间相差:” + between.getDays());
//计算当月有几天
System.out.println(“当月有几天”+lastDay.toLocalDate().getDayOfYear());
}
}
当前原生格式时间:2020-01-15T10:54:16.519
当前指定格式时间:2020-01-15 10:54:16
当前时间向后推一天2020-01-16T10:54:16.519
当前时间向前推一天2020-01-14T10:54:16.519
当前时间推一天一小时一分钟2020-01-16T11:55:16.519
判断目标天是否在当前天后true
当月第一天:2020-01-01T10:54:16.519
当月最后一天:2020-01-31T10:54:16.519
两个时间相差:30
两个时间相差:30
当月有几天31

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值