<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.0</version>
<scope>compile</scope>
</dependency>
package common.basemodule.baseutil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class BaseDateUtil extends DateUtil {
public static final String NORM_TIME_NON_SECOND_PATTERN = "HH:mm";
public static final FastDateFormat NORM_TIME_NON_SECOND_FORMAT = FastDateFormat.getInstance("HH:mm");
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_YYYYMM = "yyyy-MM";
public static final String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";
public static final String DATE_FORMAT_YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";
private static final String[] MONTH_STRING = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
public static String toGMTString(Date source) {
if (null == source) {
return "";
} else {
StringBuilder sb = new StringBuilder();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
DateFormat df = new SimpleDateFormat("dd {0} yyyy HH:mm:ss");
df.setCalendar(cal);
sb.append(df.format(source)).append(" GMT");
int index = getMonth(source) - 1;
String m = MONTH_STRING[index].substring(0, 3);
return MessageFormat.format(sb.toString(), m);
}
}
public static String toYMDString(Date date) {
return date != null ? (new SimpleDateFormat("yyyy-MM-dd")).format(date) : null;
}
public static String toYMDHSString(Date date) {
return date != null ? (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date) : null;
}
public static Date parseDateYMDHS(String dateStr) {
return parse(dateStr, "yyyy-MM-dd HH:mm:ss");
}
public static Date parseDate(String dateStr) {
String[] patterns = new String[]{"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"};
dateStr = filterChar(dateStr);
int var3 = patterns.length;
for (String string : patterns) {
Date d = parse(dateStr, string);
if (Objects.nonNull(d)) {
return d;
}
}
return null;
}
private static String filterChar(String dateStr) {
if (BaseStringUtil.isEmpty(dateStr)) {
return dateStr;
} else if (dateStr.contains("T") && dateStr.endsWith("Z")) {
dateStr = dateStr.replace("T", " ");
dateStr = dateStr.replace("Z", "");
return dateStr;
} else {
return dateStr;
}
}
private static Date parse(String yearMonth, String dateFormat) {
try {
return (new SimpleDateFormat(dateFormat)).parse(yearMonth);
} catch (Exception var4) {
return null;
}
}
public static int getMonth(Date date) {
Calendar c = toCalendar(date);
return c != null ? c.get(Calendar.MONTH) + 1 : -1;
}
public static Calendar toCalendar(Date date) {
Calendar c = null;
if (date != null) {
c = Calendar.getInstance();
c.setTime(date);
}
return c;
}
public static int getToday() {
int todayWeek = Calendar.getInstance().getTime().getDay();
return todayWeek == 0 ? 7 : todayWeek;
}
public static Date getDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
public static Date getDate(int iYear, int iMonth, int iDate, int iHour, int iMinute, int iSecond) {
--iMonth;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(iYear, iMonth, iDate, iHour, iMinute, iSecond);
return calendar.getTime();
}
public static Date getDate(int iYear, int iMonth, int iDate, int iHour, int iMinute) {
return getDate(iYear, iMonth, iDate, iHour, iMinute, 0);
}
public static Date getDate(int iYear, int iMonth, int iDate, int iHour) {
return getDate(iYear, iMonth, iDate, iHour, 0, 0);
}
public static Date getDate(int iYear, int iMonth, int iDate) {
return getDate(iYear, iMonth, iDate, 0, 0, 0);
}
public static Date getDate(int iYear, int iMonth) {
return getDate(iYear, iMonth, 1, 0, 0, 0);
}
public static Date getDate(int iYear) {
return getDate(iYear, 1, 1, 0, 0, 0);
}
public static Date getDate(String sYear) {
int iYear = getRightNumber(sYear);
return getDate(iYear);
}
public static Date getDate(String sYear, String sMonth) {
int iYear = getRightNumber(sYear);
int iMonth = getRightNumber(sMonth);
return getDate(iYear, iMonth);
}
public static Date getDate(String sYear, String sMonth, String sDate) {
int iYear = getRightNumber(sYear);
int iMonth = getRightNumber(sMonth);
int iDate = getRightNumber(sDate);
return getDate(iYear, iMonth, iDate);
}
public static Date getDate(String sYear, String sMonth, String sDate, String sHour) {
int iYear = getRightNumber(sYear);
int iMonth = getRightNumber(sMonth);
int iDate = getRightNumber(sDate);
int iHour = getRightNumber(sHour);
return getDate(iYear, iMonth, iDate, iHour);
}
public static Date getDate(String sYear, String sMonth, String sDate, String sHour, String sMinute) {
int iYear = getRightNumber(sYear);
int iMonth = getRightNumber(sMonth);
int iDate = getRightNumber(sDate);
int iHour = getRightNumber(sHour);
int iMinute = getRightNumber(sMinute);
return getDate(iYear, iMonth, iDate, iHour, iMinute);
}
public static Date getDate(String sYear, String sMonth, String sDate, String sHour, String sMinute, String sSecond) {
int iYear = getRightNumber(sYear);
int iMonth = getRightNumber(sMonth);
int iDate = getRightNumber(sDate);
int iHour = getRightNumber(sHour);
int iMinute = getRightNumber(sMinute);
int iSecond = getRightNumber(sSecond);
return getDate(iYear, iMonth, iDate, iHour, iMinute, iSecond);
}
public static Date parseDate(String day, String hour, String minute) {
Date date = parseDate(day, "yyyy-MM-dd");
date = addHo(date, hour);
date = addMi(date, minute);
return date;
}
private static int getRightNumber(String sNumber) {
try {
return Integer.parseInt(sNumber);
} catch (NumberFormatException var2) {
return 0;
}
}
public static long getMillisecondDif(Date date0, Date date1) {
return date0 != null && date1 != null ? date0.getTime() - date1.getTime() : 0L;
}
public static long getSecondDif(Date date0, Date date1) {
return getMillisecondDif(date0, date1) / 1000L;
}
public static long getMinuteDif(Date date0, Date date1) {
return getSecondDif(date0, date1) / 60L;
}
public static int getHourDif(Date date0, Date date1) {
return (int) getMinuteDif(date0, date1) / 60;
}
public static int getDayDif(Date date0, Date date1) {
return getHourDif(date0, date1) / 24;
}
public static int getMonthDif(Date date0, Date date1) {
int elapsed = 0;
GregorianCalendar gc0 = (GregorianCalendar) GregorianCalendar.getInstance();
GregorianCalendar gc1 = (GregorianCalendar) GregorianCalendar.getInstance();
if (date1.getTime() > date0.getTime()) {
gc0.setTime(date0);
gc1.setTime(date1);
} else {
gc1.setTime(date0);
gc0.setTime(date1);
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc1.clear(Calendar.DATE);
gc0.clear(Calendar.MILLISECOND);
gc0.clear(Calendar.SECOND);
gc0.clear(Calendar.MINUTE);
gc0.clear(Calendar.HOUR_OF_DAY);
gc0.clear(Calendar.DATE);
while (gc0.before(gc1)) {
gc0.add(Calendar.MONTH, 1);
++elapsed;
}
return elapsed;
}
private static Date addDate(Date date, int iArg0, int iDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(iArg0, iDate);
return calendar.getTime();
}
public static Date addSecond(Date date, int iSecond) {
return addDate(date, 13, iSecond);
}
public static Date addMinute(Date date, int iMinute) {
return addDate(date, 12, iMinute);
}
public static Date addHour(Date date, int iHour) {
return addDate(date, 10, iHour);
}
public static Date addDay(Date date, int iDate) {
return addDate(date, 5, iDate);
}
public static Date addMonth(Date date, int iMonth) {
return addDate(date, 2, iMonth);
}
public static Date addYear(Date date, int iYear) {
return addDate(date, 1, iYear);
}
public static Date addSecond(Date date, String sSecond) {
return addSecond(date, getRightNumber(sSecond));
}
public static Date addMi(Date date, String sMinute) {
return addMinute(date, getRightNumber(sMinute));
}
public static Date addHo(Date date, String sHour) {
return addHour(date, getRightNumber(sHour));
}
public static Date addDa(Date date, String sDate) {
return addDay(date, getRightNumber(sDate));
}
public static Date addMo(Date date, String sMonth) {
return addMonth(date, getRightNumber(sMonth));
}
public static Date addYe(Date date, String sYear) {
return addYear(date, getRightNumber(sYear));
}
public static String getDateFormat(Date date, String format) {
if (date == null) {
return "";
} else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
}
public static String get4yMdHmsS(Date date) {
return getDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS");
}
public static String get4yMdHms(Date date) {
return getDateFormat(date, "yyyy-MM-dd HH:mm:ss");
}
public static String get4yMdHm(Date date) {
return getDateFormat(date, "yyyy-MM-dd HH:mm");
}
public static String get4yMd(Date date) {
return getDateFormat(date, "yyyy-MM-dd");
}
public static String get4yMM(Date date) {
return getDateFormat(date, "yyyyMM");
}
public static String get4yHh(Date date) {
return getDateFormat(date, "hh:mm:ss");
}
public static String getHHmm(Date date) {
return null == date ? null : NORM_TIME_NON_SECOND_FORMAT.format(date);
}
public static String get4yMdNoDash(Date date) {
return getDateFormat(date, "yyyyMMdd");
}
public static Date parseDateFullYear(String sDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(sDate);
} catch (ParseException var3) {
return null;
}
}
public static Date parseDate(String sDate, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
return sDate == null ? null : simpleDateFormat.parse(sDate);
} catch (ParseException var4) {
return null;
}
}
public static int getPartOfTime(Date date, String part) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime(date);
switch (part) {
case "year":
return calendar.get(Calendar.YEAR);
case "month":
return calendar.get(Calendar.MONTH);
case "date":
return calendar.get(Calendar.DATE);
case "hour":
return calendar.get(Calendar.HOUR_OF_DAY);
case "minute":
return calendar.get(Calendar.MINUTE);
case "second":
return calendar.get(Calendar.SECOND);
default:
return part.equals("milliSecond") ? calendar.get(Calendar.MILLISECOND) : -1;
}
}
public static boolean isToday(Date date) {
return get4yMd(date).equals(get4yMd(Calendar.getInstance().getTime()));
}
public static boolean isLeapYear(int yearNum) {
boolean isLeap = false;
if (yearNum % 4 == 0 && yearNum % 100 != 0) {
isLeap = true;
} else if (yearNum % 400 == 0) {
isLeap = true;
} else {
isLeap = false;
}
return isLeap;
}
public static String getYearMonthEndDay(int yearNum, int monthNum) throws ParseException {
String tempYear = Integer.toString(yearNum);
String tempMonth = Integer.toString(monthNum);
String tempDay = "31";
if (tempMonth.equals("1") ||
tempMonth.equals("3") ||
tempMonth.equals("5") ||
tempMonth.equals("7") ||
tempMonth.equals("8") ||
tempMonth.equals("10") ||
tempMonth.equals("12")) {
tempDay = "31";
}
if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9") || tempMonth.equals("11")) {
tempDay = "30";
}
if (tempMonth.equals("2")) {
if (isLeapYear(yearNum)) {
tempDay = "29";
} else {
tempDay = "28";
}
}
String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(sdf.parse(tempDate));
}
public static int getWeekDay(Date strDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(strDate);
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
public static boolean nowIsBetweenDates(Date start, Date end) {
Date temp = null;
if (start.getTime() > end.getTime()) {
temp = start;
start = end;
end = temp;
}
long nowTime = (new Date()).getTime();
return nowTime >= start.getTime() && nowTime <= end.getTime();
}
public static Date getStartDateByDate(Date date) {
Date firstDate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
firstDate = cal.getTime();
return firstDate;
}
public static Date getEndDateByDate(Date date) {
Date firstDate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
firstDate = cal.getTime();
return firstDate;
}
public static Date getFirstDayOfWeek(Date date) {
Date firstDay = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
firstDay = addDay(cal.getTime(), 1);
return firstDay;
}
public static Date getLastDayOfWeek(Date date) {
Date lastDay = null;
lastDay = addDay(getFirstDayOfWeek(date), 6);
return lastDay;
}
public static int getYearByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static int getMonthByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = 0;
month = cal.get(Calendar.MONTH) + 1;
if (month == 13) {
month = 1;
}
return month;
}
public static int getDayByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DATE);
}
public static int getHourByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
}
public static int getMinuteByDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MINUTE);
}
public static Date getStartDateByMonth(Date date) {
Date firstDate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
firstDate = cal.getTime();
return firstDate;
}
public static Date getEndDateByMonth(Date date) {
Date firstDate = null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
firstDate = cal.getTime();
return firstDate;
}
public static Date getDatePart(Date date) throws ParseException {
String strDate = get4yMd(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(strDate);
}
public static String getWeekOfDate(Date dt) {
String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
public static Long getTimeInMillis(Date date) {
Calendar cal = Calendar.getInstance();
Date curDate = null;
curDate = Objects.requireNonNullElseGet(date, Date::new);
cal.setTime(curDate);
return cal.getTimeInMillis();
}
}