Java8 LocalDateTime 常见使用_获取当天、本周、本月、本季度、本年度起止时间
一、返回 LocalDateTime 格式的起止时间
import java.time.*;
import java.time.temporal.TemporalAdjusters;
public class LocalDateTimeUtils {
public static final String MinTime = "T00:00:00";
public static final String MaxTime = "T23:59:59.999999999";
public static LocalDateTime getStartOrEndDayOfDay(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if(isFirst){
return LocalDateTime.of(today, LocalTime.MIN);
}else{
return LocalDateTime.of(today, LocalTime.MAX);
}
}
public static LocalDateTime getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
DayOfWeek week = today.getDayOfWeek();
int value = week.getValue();
if (isFirst) {
resDate = today.minusDays(value - 1);
} else {
resDate = today.plusDays(7 - value);
time = MaxTime;
}
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
return localDateTime;
}
public static LocalDateTime getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now()