提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
DataScopeEnum
1.需求
根据时间类型 :不同维度去统计指标
最新(近5年)、
去年至今、
昨日、
本周、
上周、
本月、
上月、
今年、
去年
2.代码实现
2.1 枚举类
java
import.dto.DateRangeDTO;
import framework.error.SystemError;
import framework.exception.AppException;
import framework.utils.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@AllArgsConstructor
public enum DataScopeEnum {
ALL("all", "最新", 0, true, 0),
YEAR("year", "今年", 2, true, 7),
LAST_MONTH("last_month", "上月", 3, true, 6),
MONTH("month", "本月", 4, true, 5),
LAST_WEEK("last_week", "上周", 5, true, 4),
WEEK("week", "本周", 6, true, 3),
YESTERDAY("yesterday", "昨日", 7, true, 2),
LAST_YEAR_TO_PRESENT("last_year_to_present", "去年至今", 9, true, 1),
LAST_YEAR("last_year", "去年", 10, true, 8),
;
/**
* 字符串code
*/
@Getter
String code;
/**
* 描述 描述涉及到产品文案,修改时应对应需求
*/
@Getter
String description;
/**
* 数值型code
*/
@Getter
Integer intCode;
/**
* 是否启用
*/
@Getter
boolean active;
/**
* 展示顺序
*/
@Getter
Integer sort;
/**
* 根据code获取
*
* @param code String
* @return DataScopeEnum
*/
public static DataScopeEnum getByCode(String code) {
for (DataScopeEnum e : DataScopeEnum.values()) {
if (e.getCode().equals(code)) {
return e;
}
}
return null;
}
/**
* 校验code
*
* @param code code
*/
public static void validateCode(String code) {
Optional.ofNullable(getByCode(code)).orElseThrow(() -> new AppException(SystemError.SYSTEM_PARAM_ERROR));
}
/**
* 获取使用中的周期类型
*
* @return List
*/
public static List<DataScopeEnum> actives() {
return Arrays.stream(DataScopeEnum.values()).filter(DataScopeEnum::isActive)
.sorted(Comparator.comparing(DataScopeEnum::getSort)).collect(Collectors.toList());
}
/**
* 获取使用中的时间
*
* @param code
* @return
*/
public static DateRangeDTO getDateRangeByCode(Integer code){
DateRangeDTO dateRangeDTO = new DateRangeDTO();
LocalDateTime startTime = null;
LocalDateTime endTime = null;
Long startTimeMill = null;
Long endTimeMill = null;
Long startTimeEpochSecond = null;
Long endTimeEpochSecond = null;
String startTimeStr = null;
String endTimeStr = null;
LocalDate todayDate = LocalDate.now();
LocalDate yesterdayDate = LocalDate.now().minusDays(1);
LocalDate lastWeekDate = LocalDate.now().minusDays(7);
LocalDate lastMonthDate = LocalDate.now().minusMonths(1);
LocalDate lastYearDate = LocalDate.now().minusYears(1);
switch (code){
case 0:
//
startTime = LocalDateTime.of(2021, 1, 1, 0, 0,0);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 2:
//今年
startTime = LocalDateTime.of(todayDate.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 3:
//上月
startTime = LocalDateTime.of(lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()),LocalTime.MIN);
endTime = LocalDateTime.of(lastMonthDate.with(TemporalAdjusters.lastDayOfMonth()),LocalTime.MAX);
break;
case 4:
//本月
startTime = LocalDateTime.of(todayDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 5:
//上周
startTime = LocalDateTime.of(lastWeekDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)),LocalTime.MIN);
endTime = LocalDateTime.of(lastWeekDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)),LocalTime.MAX);
break;
case 6:
//本周
startTime = LocalDateTime.of(todayDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)),LocalTime.MIN);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 7:
//昨日
startTime = LocalDateTime.of(yesterdayDate, LocalTime.MIN);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 9:
// 去年至今
startTime = LocalDateTime.of(lastYearDate.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
endTime = LocalDateTime.of(yesterdayDate, LocalTime.MAX);
break;
case 10:
// 去年
startTime = LocalDateTime.of(lastYearDate.with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
endTime = LocalDateTime.of(lastYearDate.with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
break;
default:
//近一年
startTime = LocalDateTime.of(lastYearDate, LocalTime.MIN);
endTime = LocalDateTime.now();
break;
}
startTimeMill = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
endTimeMill = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli() + 999L;
startTimeEpochSecond = startTime.toEpochSecond(ZoneOffset.of("+8"));
endTimeEpochSecond = endTime.toEpochSecond(ZoneOffset.of("+8"))+ 999L;
startTimeStr = startTime.format(DateTimeFormatter.ofPattern(DateUtils.DATE_TIME_PATTERN));
endTimeStr = endTime.format(DateTimeFormatter.ofPattern(DateUtils.DATE_TIME_PATTERN));
dateRangeDTO.setStartTimeStr(startTimeStr).setEndTimeStr(endTimeStr).setStartTime(startTime).setEndTime(endTime)
.setStartTimeMillion(startTimeMill).setEndTimeMillion(endTimeMill)
.setStartTimeEpochSecond(startTimeEpochSecond).setEndTimeEpochSecond(endTimeEpochSecond);
return dateRangeDTO;
}
}
2.2 DateUtils
java
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 日期处理
*/
public class DateUtils {
/**
* 时间格式(yyyy-MM-dd)
*/
public final static String DATE_PATTERN = "yyyy-MM-dd";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 时间格式(yyyy-MM-dd HH:mm)
*/
public final static String DATE_TIME_PATTERN2 = "yyyy-MM-dd HH:mm";
/**
* 时间格式(yyyyMMddHHmm)
*/
public final static String DATE_TIME_PATTERN3 = "yyyyMMddHHmmss";
/**
* 时间格式(yyyy/MM/dd HH:mm:ss)
*/
public final static String EN_DATE_TIME_PATTERN = "yyyy/MM/dd HH:mm:ss";
/**
* 时间格式(yyyyMMdd)
*/
public final static String SIMPLE_PATTERN = "yyyyMMdd";
/**
* 日期格式化 日期格式为:yyyy-MM-dd
*
* @param date 日期
* @return 返回yyyy-MM-dd格式日期
*/
public static String format(Date date) {
return format(date, DATE_PATTERN);
}
/**
* 日期格式化 日期格式为:yyyy-MM-dd
*
* @param date 日期
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
* @return 返回yyyy-MM-dd格式日期
*/
public static String format(Date date, String pattern) {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}
/**
* 字符串转换成日期
*
* @param strDate 日期字符串
* @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
*/
public static Date stringToDate(String strDate, String pattern) {
if (StringUtils.isBlank(strDate)) {
return null;
}
DateTimeFormatter fmt = null;
try {
fmt = DateTimeFormat.forPattern(pattern);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return fmt.parseLocalDateTime(strDate).toDate();
}
/**
* 根据周数,获取开始日期、结束日期
*
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
* @return 返回date[0]开始日期、date[1]结束日期
*/
public static Date[] getWeekStartAndEnd(int week) {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
date = date.dayOfWeek().withMinimumValue();
Date beginDate = date.toDate();
Date endDate = date.plusDays(6).toDate();
return new Date[]{beginDate, endDate};
}
/**
* 对日期的【秒】进行加/减
*
* @param date 日期
* @param seconds 秒数,负数为减
* @return 加/减几秒后的日期
*/
public static Date addDateSeconds(Date date, int seconds) {
DateTime dateTime = new DateTime(date);
return dateTime.plusSeconds(seconds).toDate();
}
/**
* 对日期的【分钟】进行加/减
*
* @param date 日期
* @param minutes 分钟数,负数为减
* @return 加/减几分钟后的日期
*/
public static Date addDateMinutes(Date date, int minutes) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMinutes(minutes).toDate();
}
/**
* 对日期的【小时】进行加/减
*
* @param date 日期
* @param hours 小时数,负数为减
* @return 加/减几小时后的日期
*/
public static Date addDateHours(Date date, int hours) {
DateTime dateTime = new DateTime(date);
return dateTime.plusHours(hours).toDate();
}
/**
* 对日期的【天】进行加/减
*
* @param date 日期
* @param days 天数,负数为减
* @return 加/减几天后的日期
*/
public static Date addDateDays(Date date, int days) {
DateTime dateTime = new DateTime(date);
return dateTime.plusDays(days).toDate();
}
/**
* 对日期的【周】进行加/减
*
* @param date 日期
* @param weeks 周数,负数为减
* @return 加/减几周后的日期
*/
public static Date addDateWeeks(Date date, int weeks) {
DateTime dateTime = new DateTime(date);
return dateTime.plusWeeks(weeks).toDate();
}
/**
* 对日期的【月】进行加/减
*
* @param date 日期
* @param months 月数,负数为减
* @return 加/减几月后的日期
*/
public static Date addDateMonths(Date date, int months) {
DateTime dateTime = new DateTime(date);
return dateTime.plusMonths(months).toDate();
}
/**
* 对日期的【年】进行加/减
*
* @param date 日期
* @param years 年数,负数为减
* @return 加/减几年后的日期
*/
public static Date addDateYears(Date date, int years) {
DateTime dateTime = new DateTime(date);
return dateTime.plusYears(years).toDate();
}
public static String getNow() {
return LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));
}
/**
* 得到周几
*
* @param day 第几天
* @return 周几
*/
public static String getWeek(Integer day) {
switch (day) {
case 1:
return "MON";
case 2:
return "TUE";
case 3:
return "WED";
case 4:
return "THU";
case 5:
return "FRI";
case 6:
return "SAT";
case 7:
return "SUN";
default:
return null;
}
}
// 获取当天的开始时间
public static Date getDayBegin(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// 获取当天的结束时间
public static Date getDayEnd(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
return cal.getTime();
}
/**
* 获取当前周的第一天
*
* @param date
* @return
* @throws ParseException
*/
public static String getFirstDayOfWeek(String date) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
Date d = format.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return format.format(cal.getTime());
}
/**
* 获取当前月的第一天
*
* @param date
* @return
* @throws ParseException
*/
public static String getFirstDayOfMonth(String date) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
Date d = format.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
return format.format(cal.getTime());
}
/**
* 获取当前时间前/后几个月的第一天
* @example:
* date:2021-08-15 num:2 before:true result:2021-06-01
* date:2021-08-15 num:2 before:false result:2021-10-01
* @param date 当前时间
* @param num 月数
* @return
* @throws ParseException
*/
public static String getFirstDayOfMonth(String date, int num, boolean before) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_PATTERN);
Date d = format.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
if (before) {
cal.add(Calendar.MONTH, Math.negateExact(num));
} else {
cal.add(Calendar.MONTH, num);
}
cal.set(Calendar.DAY_OF_MONTH, 1);
return format.format(cal.getTime());
}
}
2.3 DateRangeDTO
java
import com.fasterxml.jackson.annotation.JsonFormat;
import framework.utils.DateUtils;
import lombok.Data;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
@Data
@Accessors(chain = true)
public class DateRangeDTO {
/**
* 开始时间
*/
private String startTimeStr;
/**
* 结束时间
*/
private String endTimeStr;
/**
* 开始时间
*/
@JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN,timezone ="GMT+8")
private LocalDateTime startTime;
/**
* 结束时间
*/
@JsonFormat(pattern = DateUtils.DATE_TIME_PATTERN,timezone ="GMT+8")
private LocalDateTime endTime;
/**
* 开始时间,毫秒时间戳 主要用于es查询
*/
private Long startTimeMillion;
/**
* 结束时间,毫秒时间戳 主要用于es查询
*/
private Long endTimeMillion;
/**
* 开始时间,秒时间戳 主要用于ada相关数据查询
*/
private Long startTimeEpochSecond;
/**
* 结束时间,秒时间戳 主要用于ada相关数据查询
*/
private Long endTimeEpochSecond;
}
3.应用
- 应用1
java
for (DataScopeEnum dataScopeEnum : DataScopeEnum.actives()) {
// 周一
if (localDate.getDayOfWeek().equals(DayOfWeek.MONDAY)
&& dataScopeEnum.getCode().equals(DataScopeEnum.WEEK.getCode())) {
continue;
}
// 每月第一天
if (localDate.getDayOfMonth() == 1
&& dataScopeEnum.getCode().equals(DataScopeEnum.MONTH.getCode())) {
continue;
}
// 每年第一天
if (localDate.getDayOfYear() == 1
&& dataScopeEnum.getCode().equals(DataScopeEnum.YEAR.getCode())) {
continue;
}
DateRangeDTO dateRangeDTO = DataScopeEnum.getDateRangeByCode(dataScopeEnum.getIntCode());
// 每一个任务按照不同维度去实现
try {
batchQueryStat(channelOrgMap, taskId, dataScopeEnum, dateRangeDTO);
} catch (Exception e) {
}
}
- 应用2
java
//时间维度
for (DataScopeEnum dataScopeEnum : DataScopeEnum.actives()) {
DateRangeDTO dateRangeDTO = DataScopeEnum.getDateRangeByCode(dataScopeEnum.getIntCode());
handleStat(bsCategoryId, dataScopeEnum.getIntCode(), taskIdList);
}