package com.mgm.mcrm.common.utils; import com.mgm.mcrm.appsetting.services.SettingService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Calendar; import java.util.Date; import java.util.Locale; @Slf4j public class DateTimeUtil { @Resource private SettingService settingService; /** * 根据String型时间,获取long型时间,单位毫秒 * @param inVal 时间字符串 * @return long型时间 */ public static long fromDateStringToLong(String inVal) { Date date = null; SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS"); try { date = inputFormat.parse(inVal); } catch (Exception e) { e.printStackTrace(); } return date.getTime(); } public static long getTimeSpanSSS(Date startDate,Date endDate) { SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS"); String startStr = timeFormat.format(startDate); String endStr = timeFormat.format(endDate); long startMS = fromDateStringToLong(startStr); long endMS = fromDateStringToLong(endStr); return endMS-startMS; } public static Date getDateFromString(String dateTimeValue,String dateFormat) { SimpleDateFormat dtFormat = new SimpleDateFormat(dateFormat); if (StringUtils.isNotBlank(dateTimeValue)) { try { return dtFormat.parse(dateTimeValue); } catch (Exception err) { log.warn(err.getMessage(), err); } } return null; } public static String getDateByFormat(Date dtDateTime, String strFormat) { String returnDate = null; if (dtDateTime != null) { try { SimpleDateFormat formatter = new SimpleDateFormat(strFormat); returnDate = formatter.format(dtDateTime); } catch (Exception err) { } } return returnDate; } public static String getDateByFormat(LocalDateTime dateTime, String strFormat) { String returnDate = null; if (dateTime != null) { try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(strFormat); returnDate = dateTime.format(formatter); } catch (Exception err) { // 可以考虑打日志 } } return returnDate; } public static String getCurrentDate(String strFormat) { return getDateByFormat(new Date(), strFormat); } public static Date getCurrentDateTime(String strFormat) { SimpleDateFormat dtFormat = new SimpleDateFormat(strFormat); String dateTimeValue = getDateByFormat(new Date(), strFormat); Date returnDate = null; try { returnDate = dtFormat.parse(dateTimeValue); } catch (Exception err) { } return returnDate; } public static Date addSecond(Date nowTime,Integer seconds) { long getTime = nowTime.getTime(); long diffTime = seconds * 1000L; long newTime = getTime+diffTime; String strTime = getDateByFormatUseTimeStamp(newTime,"yyyy-MM-dd HH:mm:ss"); return getDateFromString(strTime,"yyyy-MM-dd HH:mm:ss"); } public static Date addMin(Date nowTime,Integer mins) { long getTime = nowTime.getTime(); long diffTime = mins * 60000L; long newTime = getTime+diffTime; String strTime = getDateByFormatUseTimeStamp(newTime,"yyyy-MM-dd HH:mm:ss"); return getDateFromString(strTime,"yyyy-MM-dd HH:mm:ss"); } public static Date addDay(Date currdate,Integer days) { Calendar ca = Calendar.getInstance(); ca.setTime(currdate); ca.add(Calendar.DATE, days); Date newTime = ca.getTime(); return newTime; } public static Date addMonth(Date currdate,Integer months) { Calendar ca = Calendar.getInstance(); ca.setTime(currdate); ca.add(Calendar.MONTH, months); Date newTime = ca.getTime(); return newTime; } public static Date addYear(Date currdate,Integer years) { Calendar ca = Calendar.getInstance(); ca.setTime(currdate); ca.add(Calendar.YEAR, years); Date newTime = ca.getTime(); return newTime; } public static String getDateByFormatUseTimeStamp(Long timeStamp, String strFormat) { String returnDate = null; if (timeStamp != null) { try { SimpleDateFormat formatter; formatter = new SimpleDateFormat(strFormat); returnDate = formatter.format(timeStamp); } catch (Exception err) { } } return returnDate; } public static String getDiffDate(Date DateStart, Date DateEnd, String dateType) { String returnDate = null; try { Instant instant1 = DateStart.toInstant(); Instant instant2 = DateEnd.toInstant(); Duration duration = Duration.between(instant1, instant2); //long diffTime = DateEnd.getTime() - DateStart.getTime(); if("day".equalsIgnoreCase(dateType)) { //returnDate = Long.toString(diffTime / 1000 / (60 * 60 * 24)); long days = duration.toDays(); returnDate = Long.toString(days); }else if("hour".equalsIgnoreCase(dateType)) { //returnDate = Long.toString(diffTime / 1000 / (60 * 60)); long hours = duration.toHours(); returnDate = Long.toString(hours); }else { //minute long minutes = duration.toMinutes(); returnDate = Long.toString(minutes); } } catch (Exception err) { } return returnDate; } public static Date getDateFromLong(Long timestamp){ return new Date(timestamp); } public static Long getLongFromDate(Date date){ return date.getTime(); } public static boolean isValidDate(String dateStr) { try { dateStr=dateStr.split("T")[0]; dateStr=dateStr.split(" ")[0]; String[] parts = dateStr.split("-"); int year = Integer.parseInt(parts[0]); int month = Integer.parseInt(parts[1]); int day = Integer.parseInt(parts[2]); if (month < 1 || month > 12) { return false; } YearMonth yearMonth = YearMonth.of(year, month); int maxDay = yearMonth.lengthOfMonth(); boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if (month == 2 && isLeapYear) { maxDay = 29; } else if (month == 2) { maxDay = 28; } if (day < 1 || day > maxDay) { return false; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate.parse(dateStr, formatter); return true; } catch (DateTimeParseException e) { return false; } } public static Date getTodoYesterdayDay(String cutoffStartTime,String cutoffEndTime){ String date = DateTimeUtil.getDateByFormat(new Date(), "yyyy-MM-dd"); Date curentTime = DateTimeUtil.getCurrentDateTime("yyyy-MM-dd HH:mm"); Date getStartTime = DateTimeUtil.getDateFromString(date+" "+cutoffStartTime, "yyyy-MM-dd HH:mm"); Date getEndTime = DateTimeUtil.getDateFromString(date+" "+cutoffEndTime, "yyyy-MM-dd HH:mm"); Date queryDate = DateTimeUtil.getCurrentDateTime("yyyy-MM-dd"); if(curentTime.getTime()>=getStartTime.getTime()&&curentTime.getTime()<=getEndTime.getTime()) { queryDate = DateTimeUtil.addDay(queryDate, -1); } return queryDate; } /** * OPERA cashier comment 入住日期段:英文 Mar 30,禁止 Date#toString()。 * 单晚:30 Mar;同月多晚:30-31 Mar;跨月:30 Mar - 01 Apr */ public static String formatOperaCommentStayDateRange(Date checkin, Date checkout, Integer numOfNight) { Locale en = Locale.ENGLISH; if (checkin == null) { return ""; } boolean stayIsSingleNight = Integer.valueOf(1).equals(numOfNight); if (!stayIsSingleNight && numOfNight == null && checkout != null) { stayIsSingleNight = "1".equals(getDiffDate(checkin, checkout, "day")); } if (stayIsSingleNight) { return getDateByFormat(checkin, "dd MMM", en); } if (checkout == null) { return getDateByFormat(checkin, "dd MMM", en); } Date lastNightDate = addDay(checkout, -1); String firstMonthAbbr = getDateByFormat(checkin, "MMM", en); String lastMonthAbbr = getDateByFormat(lastNightDate, "MMM", en); String firstDayOfMonth = getDateByFormat(checkin, "dd", en); String lastDayOfMonth = getDateByFormat(lastNightDate, "dd", en); if (firstMonthAbbr != null && firstMonthAbbr.equals(lastMonthAbbr)) { return firstDayOfMonth + "-" + lastDayOfMonth + " " + firstMonthAbbr; } return firstDayOfMonth + " " + firstMonthAbbr + " - " + lastDayOfMonth + " " + lastMonthAbbr; } /** * 按指定区域格式化日期(如 OPERA comment 需固定英文月份缩写 Mar/Apr)。 */ public static String getDateByFormat(Date dtDateTime, String strFormat, Locale locale) { String returnDate = null; if (dtDateTime != null && locale != null) { try { SimpleDateFormat formatter = new SimpleDateFormat(strFormat, locale); returnDate = formatter.format(dtDateTime); } catch (Exception err) { } } return returnDate; } public static String getDateByFormatPlusOneDay(Date dtDateTime, String strFormat) { if (dtDateTime == null) { return null; } try { Calendar calendar = Calendar.getInstance(); calendar.setTime(dtDateTime); calendar.add(Calendar.DAY_OF_MONTH, 1); return new SimpleDateFormat(strFormat).format(calendar.getTime()); } catch (Exception err) { return null; } } }