diff --git a/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoBaseEntity.java b/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoBaseEntity.java new file mode 100644 index 0000000..c6503ba --- /dev/null +++ b/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoBaseEntity.java @@ -0,0 +1,128 @@ +package com.chinaunicom.zyhy.ebtp.supplier.common; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Entity基类 + * + * @author ruoyi + */ +@ApiModel(description = "实体基类") +public class CoscoBaseEntity implements Serializable +{ + private static final long serialVersionUID = 1L; + + /** 搜索值 */ + @JsonIgnore + @ApiModelProperty(value = "搜索值") + private String searchValue; + + /** 创建者 */ + @ApiModelProperty(value = "创建人") + private String createBy; + + /** 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @ApiModelProperty(value = "创建时间") + private Date createTime; + + /** 更新者 */ + @ApiModelProperty(value = "修改人") + private String updateBy; + + /** 更新时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @ApiModelProperty(value = "修改时间") + private Date updateTime; + + /** 备注 */ + @ApiModelProperty(value = "备注") + private String remark; + + /** 请求参数 */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private Map params; + + public String getSearchValue() + { + return searchValue; + } + + public void setSearchValue(String searchValue) + { + this.searchValue = searchValue; + } + + public String getCreateBy() + { + return createBy; + } + + public void setCreateBy(String createBy) + { + this.createBy = createBy; + } + + public Date getCreateTime() + { + return createTime; + } + + public void setCreateTime(Date createTime) + { + this.createTime = createTime; + } + + public String getUpdateBy() + { + return updateBy; + } + + public void setUpdateBy(String updateBy) + { + this.updateBy = updateBy; + } + + public Date getUpdateTime() + { + return updateTime; + } + + public void setUpdateTime(Date updateTime) + { + this.updateTime = updateTime; + } + + public String getRemark() + { + return remark; + } + + public void setRemark(String remark) + { + this.remark = remark; + } + + public Map getParams() + { + if (params == null) + { + params = new HashMap<>(); + } + return params; + } + + public void setParams(Map params) + { + this.params = params; + } +} diff --git a/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoDateUtils.java b/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoDateUtils.java new file mode 100644 index 0000000..dbf8d80 --- /dev/null +++ b/src/main/java/com/chinaunicom/zyhy/ebtp/supplier/common/CoscoDateUtils.java @@ -0,0 +1,326 @@ +package com.chinaunicom.zyhy.ebtp.supplier.common; + +import org.apache.commons.lang3.time.DateFormatUtils; + +import java.lang.management.ManagementFactory; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; + +/** + * 时间工具类 + * + * @author ruoyi + */ +public class CoscoDateUtils extends org.apache.commons.lang3.time.DateUtils { + public static String YYYY = "yyyy"; + + public static String DD = "dd"; + + public static String YYYY_MM = "yyyy-MM"; + + public static String YYYY_MM_DD = "yyyy-MM-dd"; + + public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; + + public static String YYYYMMDD="yyyyMMdd"; + + public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; + public static String YYYY_MM_DD__HH_MM_SS = "yyyy-MM-dd+HH:mm:ss"; + private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; + + /** + * 获取当前Date型日期 + * + * @return Date() 当前日期 + */ + public static Date getNowDate() { + return new Date(); + } + + /** + * 获取当前日期, 默认格式为yyyy-MM-dd + * + * @return String + */ + public static String getDate() { + return dateTimeNow(YYYY_MM_DD); + } + + public static final String getTime() { + return dateTimeNow(YYYY_MM_DD_HH_MM_SS); + } + + public static final String dateTimeNow() { + return dateTimeNow(YYYYMMDDHHMMSS); + } + + public static final String dateTimeNow(final String format) { + return parseDateToStr(format, new Date()); + } + + public static final String dateTime(final Date date) { + return parseDateToStr(YYYY_MM_DD, date); + } + + public static final String parseDateToStr(final String format, final Date date) { + return new SimpleDateFormat(format).format(date); + } + + public static final Date dateTime(final String format, final String ts) { + try { + return new SimpleDateFormat(format).parse(ts); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + /** + * 日期路径 即年/月/日 如2018/08/08 + */ + public static final String datePath() { + Date now = new Date(); + return DateFormatUtils.format(now, "yyyy/MM/dd"); + } + + /** + * 日期路径 即年/月/日 如20180808 + */ + public static final String dateTime() { + Date now = new Date(); + return DateFormatUtils.format(now, "yyyyMMdd"); + } + + /** + * 日期型字符串转化为日期 格式 + */ + public static Date parseDate(Object str) { + if (str == null) { + return null; + } + try { + return parseDate(str.toString(), parsePatterns); + } catch (ParseException e) { + return null; + } + } + + /** + * 获取服务器启动时间 + */ + public static Date getServerStartDate() { + long time = ManagementFactory.getRuntimeMXBean().getStartTime(); + return new Date(time); + } + + /** + * 计算相差天数 + */ + public static int differentDaysByMillisecond(Date date1, Date date2) { + return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); + } + + /** + * 计算时间差 + * + * @param endDate 最后时间 + * @param startTime 开始时间 + * @return 时间差(天/小时/分钟) + */ + public static String timeDistance(Date endDate, Date startTime) { + long nd = 1000 * 24 * 60 * 60; + long nh = 1000 * 60 * 60; + long nm = 1000 * 60; + // long ns = 1000; + // 获得两个时间的毫秒时间差异 + long diff = endDate.getTime() - startTime.getTime(); + // 计算差多少天 + long day = diff / nd; + // 计算差多少小时 + long hour = diff % nd / nh; + // 计算差多少分钟 + long min = diff % nd % nh / nm; + // 计算差多少秒//输出结果 + // long sec = diff % nd % nh % nm / ns; + return day + "天" + hour + "小时" + min + "分钟"; + } + + /** + * 增加 LocalDateTime ==> Date + */ + public static Date toDate(LocalDateTime temporalAccessor) { + ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); + return Date.from(zdt.toInstant()); + } + + /** + * 增加 LocalDate ==> Date + */ + public static Date toDate(LocalDate temporalAccessor) { + LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); + ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); + return Date.from(zdt.toInstant()); + } + + // 默认前置时间小时数 + private final static int defMinusHours = 12; + + /** + * 根据前置时间获取Date + * 例如 minusHours = 12 为获取12小时前Date + */ + public static Date backDate() { + return backDate(defMinusHours); + } + + /** + * 根据前置时间获取Date + * 例如 minusHours = 12 为获取12小时前Date + */ + public static Date backDate(int minusHours) { + // 获取当前时间 + LocalDateTime now = LocalDateTime.now(); + // 减去小时数 + LocalDateTime twelveHoursAgo = now.minusHours(minusHours); + return Date.from(twelveHoursAgo.atZone(ZoneId.systemDefault()).toInstant()); + } + + /** + * 获取指定日期月份的所有日期 + * + * @param date 指定日期 + * @return 该月份的所有日期 + */ + /* public static List getDatesOfMonth(Date date) { + LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + YearMonth yearMonth = YearMonth.from(localDate); + int daysInMonth = yearMonth.lengthOfMonth(); + List dates = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + for (int i = 1; i <= daysInMonth; i++) { + LocalDate day = yearMonth.atDay(i); + dates.add(day.format(formatter)); + } + return dates; + }*/ + public static List getDatesOfMonth(Date date) { + LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + YearMonth yearMonth = YearMonth.from(localDate); + // 获取每月1日 + LocalDate firstDayOfMonth = yearMonth.atDay(1); + // 获取每月最后一日 + LocalDate lastDayOfMonth = yearMonth.atEndOfMonth(); + LocalDate currentDate = LocalDate.now(ZoneId.systemDefault()); + + // 月份日期集合 + List dates = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + LocalDate day = firstDayOfMonth; + // 当月份日期超过月份最后一日或大于当前日期 停止遍历 + while (!day.isAfter(currentDate) && !day.isAfter(lastDayOfMonth)) { + dates.add(day.format(formatter)); + day = day.plusDays(1); + } + + return dates; + } + + public static Date convertMonthStringToDate(String monthString, String format) { + // 解析字符串为 YearMonth 对象 + YearMonth yearMonth = YearMonth.parse(monthString, DateTimeFormatter.ofPattern(format)); + // 获取该月份的第一天 + LocalDate firstDayOfMonth = yearMonth.atDay(1); + // 将 LocalDate 转换为 Date 对象 + Date date = Date.from(firstDayOfMonth.atStartOfDay(ZoneId.systemDefault()).toInstant()); + return date; + } + + public static Date stringToDate(String date, String format) { + SimpleDateFormat formatter = new SimpleDateFormat(format); + try { + return formatter.parse(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + public static boolean isValidFormat(String format, String value) { + SimpleDateFormat sdf = new SimpleDateFormat(format); + sdf.setLenient(false); + try { + sdf.parse(value); + } catch (ParseException e) { + return false; + } + return true; + } + + public static String formatDate(Date date, String format) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); + LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + return localDateTime.format(formatter); + } + + private static final ThreadLocal inputFormatThreadLocal = ThreadLocal.withInitial(() -> { + SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); + sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT + return sdf; + }); + + private static final ThreadLocal outputFormatThreadLocal = ThreadLocal.withInitial(() -> { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT + return sdf; + }); + + public static String EEEtoYYYY(String oldDate) { + try { + System.out.println("Input date string: " + oldDate); // 打印输入字符串 + Date date = inputFormatThreadLocal.get().parse(oldDate); + return outputFormatThreadLocal.get().format(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Date转为LocalDate + * @param date + * @return + */ + public static LocalDate getDate(Date date) { + return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + + /** + * 获取当前日期几天前的日期 + * + * @param days 几天前 + * @return 几天前的日期 + */ + public static Date getDaysBefore(Date date,int days) { + LocalDate currentDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + LocalDate daysBefore = currentDate.minusDays(days); + ZonedDateTime midnight = daysBefore.atStartOfDay(ZoneId.systemDefault()); + return Date.from(midnight.toInstant()); + } + + /** + * 获取当前日期几月前的时间 + */ + public static Date getMonthsBefore(Date date,int months) { + LocalDateTime currentDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + LocalDateTime monthsBefore = currentDate.minusMonths(months); + ZoneId zoneId = ZoneId.systemDefault(); // or any specific zone, e.g., ZoneId.of("America/New_York") + ZonedDateTime zonedDateTime = monthsBefore.atZone(zoneId); + return Date.from(zonedDateTime.toInstant()); + } + +}