java8中LambdaQueryWrapper对象使用
LocalDateTime类常用方法
LocalDateTime用法大全
LocalDate和LocalTime的用法介绍
注意LocalDate和LocalTime可以用LocalDateTime.of(LocalDate.,LocalTime.)来拼接
Java LocalDateTime with()用法及代码示例
java8 关于获得本日,本周,本月时间
Java 8 – TemporalAdjusters示例
当天、本周、本月
@Data
public class A{
//时间查询类型:1.当日、2.本周、3.本月
private Integer timeType;
public LocalDateTime[] typeToDateArray() {
if (timeType == null) {
return null;
}
LocalDateTime localDateTime = LocalDateTime.now().withNano(0).withSecond(0).withMinute(0).withHour(0);
if (timeType == 1) {
return new LocalDateTime[] {localDateTime, localDateTime.withHour(23).withMinute(59)};
} else if (timeType == 2) {
LocalDateTime localDateTime1 = localDateTime.minusDays(localDateTime.getDayOfWeek().getValue());
return new LocalDateTime[] {localDateTime1, localDateTime.withHour(23).withMinute(59)};
} else if (timeType == 3) {
LocalDateTime localDateTime1 = localDateTime.minusDays(localDateTime.getDayOfMonth());
return new LocalDateTime[] {localDateTime1, localDateTime.withHour(23).withMinute(59)};
} else {
return null;
}
}
}
LambdaQueryWrapper<B> wrapper = new LambdaQueryWrapper<>();
A params = new A();
LocalDateTime[] localDateTimes = params.typeToDateArray();
if(localDateTimes != null){
wrapper.ge(Alarm::getUpdateTime,localDateTimes[0]).le(Alarm::getUpdateTime,localDateTimes[1]);
}
//这一周的星期一
LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY)
//这一周的星期二
LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.TUESDAY)
public class TimeUtils {
/**
* 今天0点
* @return
*/
public static LocalDateTime startOfCurrDay(){
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
}
/**
* 今天23点59分59秒
* @return
*/
public static LocalDateTime endOfCurrDay(){
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 本周一0点
* @return
*/
public static LocalDateTime startOfCurrWeek(){
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY);
}
/**
* 本月1号0点
* @return
*/
public static LocalDateTime startOfCurrMonth(){
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(TemporalAdjusters.firstDayOfMonth());
}
}