java 时间查询,今天,明天,昨天,月时间

LocalDateTime now = LocalDateTime.now();

//当前时间

String nowTime = now.format(DateTimeFormatter.ISO_LOCAL_DATE);//2022-06-17

String nowTime1 = now.format(DateTimeFormatter.ISO_DATE);//2022-06-17

String nowTime2 =now.format(DateTimeFormatter.ISO_DATE);//20220617

String nowTime1 = now.format(DateTimeFormatter.ISO_TIME);//17:38:57.062

//当前时间

String nowTime1=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")): 2022-06-17 17:38:57

//前1天(昨天)

String beforeYesterday = LocalDateTime.now().minusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

ps:修改minusDays(1L)内的数字来得到想要时间

//后1天(明天)

String beforeYesterday = LocalDateTime.now().plusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

//获取当天开始时间,结束时间

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);

 // 2022-06-17 00:00:00

String start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(localDateTimeFormatter);

 // 2022-06-17 23:59:59

String end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(localDateTimeFormatter);

ps:这里的LocalDate.now()表示获取今天的开始时间和结束时间,也可以换做任何一天

//获取当月第一天和最后一天

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);

LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());//2022-06-01

LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());//2022-06-30

//计算时间差

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);

String str = "2022-06-10";

LocalDate localDate = LocalDate.parse(str, formatter);

long until = LocalDate.now().until(localDate, ChronoUnit.DAYS);

// 2022-06-10到2022-06-17时间 -7为相隔7天  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flying.s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值