weaver-hrm-salary/src/com/engine/salary/util/SalaryDateUtil.java

220 lines
7.4 KiB
Java
Raw Normal View History

2022-03-01 16:45:57 +08:00
package com.engine.salary.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @description: 时间工具类
* @author: xiajun
* @modified By: xiajun
* @date: Created in 10/19/21 4:23 PM
* @version:v1.0
*/
@Slf4j
public class SalaryDateUtil {
public static final ZoneId CTT = ZoneId.of(ZoneId.SHORT_IDS.get("CTT"));
public static final ZoneOffset SHANGHAI_ZONE_OFF_SET = ZoneOffset.ofHours(8);
public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
public static final FastDateFormat DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* yyyy-MM
**/
private static final String MONTH_REGEX = "^([1-9]\\d{3})-(([0]{0,1}[1-9])|([1][0-2]))$";
/**
* yyyy-MM-dd
**/
private static final String DAY_REGEX = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
/**
* 含斜杠日期格式
*/
private static final String DAY_BAR_REGEX = "^[1-9]\\d{3}/([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])$";
public static Long localDate2EpochMilli(LocalDate localDate) {
if (localDate == null) {
return NumberUtils.LONG_ZERO;
}
return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static Long localDateTime2EpochMilli(LocalDateTime localDateTime) {
if (localDateTime == null) {
return NumberUtils.LONG_ZERO;
}
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static String getFormatYearMonth(LocalDate localDate) {
if (localDate == null) {
return StringUtils.EMPTY;
}
try {
return localDate.format(MONTH_FORMATTER);
} catch (Exception e) {
log.warn("格式化月份错误", e);
return StringUtils.EMPTY;
}
}
2022-03-16 14:29:02 +08:00
public static String getFormatLocalDate(LocalDate localDate) {
if (localDate == null) {
return StringUtils.EMPTY;
}
try {
return localDate.format(DATE_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
2022-03-01 16:45:57 +08:00
public static String getFormatLocalDate(LocalDateTime localDateTime) {
if (localDateTime == null) {
return StringUtils.EMPTY;
}
try {
return localDateTime.format(DATE_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static String getFormatLocalDateTime(LocalDateTime localDateTime) {
if (localDateTime == null) {
return StringUtils.EMPTY;
}
try {
return localDateTime.format(DATE_TIME_FORMATTER);
} catch (Exception e) {
log.warn("格式化日期错误", e);
return StringUtils.EMPTY;
}
}
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public static Date localDateToDate(LocalDate localDate) {
if (null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
public static String getFormatLocalDate(Date date) {
if (date == null) {
return StringUtils.EMPTY;
}
LocalDateTime localDateTime = dateToLocalDateTime(date);
return getFormatLocalDate(localDateTime);
}
public static YearMonth localDate2YearMonth(LocalDate localDate) {
if (localDate == null) {
return null;
}
return YearMonth.of(localDate.getYear(), localDate.getMonthValue());
}
// public static LocalDateRange localDate2Range(LocalDate localDate) {
// if (localDate == null) {
// return null;
// }
// return LocalDateRange.builder()
// .fromDate(localDate.with(TemporalAdjusters.firstDayOfMonth()))
// .endDate(localDate.with(TemporalAdjusters.lastDayOfMonth()))
// .build();
// }
//
// public static LocalDateRange localDate2YearRange(LocalDate localDate) {
// if (localDate == null) {
// return null;
// }
// return LocalDateRange.builder()
// .fromDate(localDate.with(TemporalAdjusters.firstDayOfYear()))
// .endDate(localDate.with(TemporalAdjusters.lastDayOfYear()))
// .build();
// }
public static String getMonthBegin(String specifiedDay) {
int year;
int month;
Pattern pattern = Pattern.compile("\\d+-\\d+");
Matcher matcher = pattern.matcher(specifiedDay);
if (StringUtils.isEmpty(specifiedDay) || !matcher.matches()) {
return null;
} else {
year = Integer.parseInt(specifiedDay.split("-")[0]);
month = Integer.parseInt(specifiedDay.split("-")[1]);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date startDate = calendar.getTime();
return sdf.format(startDate);
}
public static String getYearMonth(int yearNum, int monthNum) {
LocalDateTime dateTime = LocalDateTime.now();
int year = dateTime.getYear() + yearNum;
int month = dateTime.getMonthValue() + monthNum;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date startDate = calendar.getTime();
return sdf.format(startDate);
}
/**
* 检查年月格式
*
* @param yearMonth
* @return
*/
public static boolean checkYearMonth(String yearMonth) {
return Pattern.matches(MONTH_REGEX, yearMonth);
}
/**
* 检查日期格式
*
* @param day
* @return
*/
public static boolean checkDay(String day) {
return Pattern.matches(DAY_REGEX, day) || Pattern.matches(DAY_BAR_REGEX, day);
}
}