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天