package ele;
import java.util.Calendar;
import java.util.Date;
/**
* 时间计算枚举
*/
public enum DateUtil {
MILLISECOND((type, calendar, val) -> {
calendar.add(Calendar.MILLISECOND, val);
if (!TypeEnum.SELF.equals(type)) {
val = val < 0 ? -val : val;
if (val < 10) {
calendar.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND) - calendar.get(Calendar.MILLISECOND) % 10 + (TypeEnum.FLOOR.equals(type) ? 9 : 0));
} else if (val < 100) {
calendar.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND) - calendar.get(Calendar.MILLISECOND) % 100 + (TypeEnum.FLOOR.equals(type) ? 99 : 0));
} else if (val < 1000) {
calendar.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND) - calendar.get(Calendar.MILLISECOND) % 1000 + (TypeEnum.FLOOR.equals(type) ? 999 : 0));
}
}
}),
SECOND((type, calendar, val) -> {
calendar.add(Calendar.SECOND, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.MILLISECOND, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.set(Calendar.MILLISECOND, 999);
}
}),
MINUTE((type, calendar, val) -> {
calendar.add(Calendar.MINUTE, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
}
}),
HOUR((type, calendar, val) -> {
calendar.add(Calendar.HOUR, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
}
}),
DAY((type, calendar, val) -> {
calendar.add(Calendar.DATE, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);
}
}),
WEEK((type, calendar, val) -> {
calendar.add(Calendar.WEEK_OF_YEAR, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.add(Calendar.DAY_OF_YEAR, 1 - calendar.get(Calendar.DAY_OF_WEEK));
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.add(Calendar.DAY_OF_YEAR, 7 - calendar.get(Calendar.DAY_OF_WEEK));
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);
}
}),
MONTH((type, calendar, val) -> {
calendar.add(Calendar.MONTH, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
}
}),
YEAR((type, calendar, val) -> {
calendar.add(Calendar.YEAR, val);
if (TypeEnum.CEILING.equals(type)) {
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
} else if (TypeEnum.FLOOR.equals(type)) {
calendar.add(Calendar.YEAR, 1);
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.add(Calendar.DATE, -1);
}
}),
;
DateConsumer<TypeEnum, Calendar, Integer> rule;
DateUtil(DateConsumer<TypeEnum, Calendar, Integer> rule) {
this.rule = rule;
}
/**
* 获取单位当前时间开始值
* @return
*/
public Date getCeiling() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.CEILING, calendar, 0);
return calendar.getTime();
}
/**
* 获取单位当前时间最大值
* @return
*/
public Date getFloor() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.FLOOR, calendar, 0);
return calendar.getTime();
}
/**
* 取自定义偏移量的时间
* @param offset
* @return
*/
public Date calculateAndGet(int offset) {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.SELF, calendar, offset);
return calendar.getTime();
}
/**
* 取自定义偏移量的开始时间
* @param offset
* @return
*/
public Date calculateAndGetCeiling(int offset) {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.CEILING, calendar, offset);
return calendar.getTime();
}
/**
* 取自定义偏移量的结束时间
* @param offset
* @return
*/
public Date calculateAndGetFloor(int offset) {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.FLOOR, calendar, offset);
return calendar.getTime();
}
/**
* 取当前时间单位减 1
* @return
*/
public Date getForSubOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.SELF, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1
* @return
*/
public Date getForAddOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.SELF, calendar, 1);
return calendar.getTime();
}
/**
* 取当前时间单位减 1 开始值
* @return
*/
public Date getCeilingForSubOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.CEILING, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位减 1 最大值
* @return
*/
public Date getFloorForSubOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.FLOOR, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1 最小值
* @return
*/
public Date getCeilingForAddOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.CEILING, calendar, 1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1 最大值
* @return
*/
public Date getFloorForAddOne() {
Calendar calendar = Calendar.getInstance();
rule.accept(TypeEnum.FLOOR, calendar, 1);
return calendar.getTime();
}
/**
* 取自定义偏移量的时间
* @param date 指定时间
* @param offset
* @return
*/
public Date calculateAndGet(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.SELF, calendar, offset);
return calendar.getTime();
}
/**
* 取自定义偏移量的开始时间
* @param date 指定时间
* @param offset
* @return
*/
public Date calculateAndGetCeiling(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.CEILING, calendar, offset);
return calendar.getTime();
}
/**
* 取自定义偏移量的结束时间
* @param date 指定时间
* @param offset
* @return
*/
public Date calculateAndGetFloor(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.FLOOR, calendar, offset);
return calendar.getTime();
}
/**
* 取当前时间单位减 1
* @param date 指定时间
* @return
*/
public Date getForSubOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.SELF, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1
* @param date 指定时间
* @return
*/
public Date getForAddOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.SELF, calendar, 1);
return calendar.getTime();
}
/**
* 取当前时间单位减 1 开始值
* @param date 指定时间
* @return
*/
public Date getCeilingForSubOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.CEILING, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位减 1 最大值
* @param date 指定时间
* @return
*/
public Date getFloorForSubOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.FLOOR, calendar, -1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1 最小值
* @param date 指定时间
* @return
*/
public Date getCeilingForAddOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.CEILING, calendar, 1);
return calendar.getTime();
}
/**
* 取当前时间单位加 1 最大值
* @param date 指定时间
* @return
*/
public Date getFloorForAddOne(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
rule.accept(TypeEnum.FLOOR, calendar, 1);
return calendar.getTime();
}
@FunctionalInterface
public interface DateConsumer<A,B,C> {
void accept(A a,B b,C c);
}
private enum TypeEnum {
CEILING,
FLOOR,
SELF,
;
}
}
Java8时间工具类
最新推荐文章于 2024-09-19 15:40:44 发布
此博客介绍了一个名为DateUtil的Java枚举,用于方便地进行日期的加减操作,支持自定义偏移量和边界处理(天花板和地板)。它提供了多种方法,如获取当前时间单位的开始、结束值,以及针对特定时间的调整等。
摘要由CSDN通过智能技术生成