import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateUtils { public final static String DATE_CHINESE_PATTERN = "yyyy年MM月dd日"; public final static String DATE_MONTH_DAY_CHINESE_PATTERN = "MM月dd日"; public final static String HOUR_MINUTE_CHINESE_PATTERN = "HH点mm分"; public final static String DATE_PATTERN = "yyyy-MM-dd"; public final static String DATE_YYYY_MM = "yyyy-MM"; public final static String DATE_SHORT_PATTERN = "yyyyMMdd"; public final static String DATE_SLASH_PATTERN = "yyyy/MM/dd"; public final static String DATETIME_MILL_SECOND = "yyyy-MM-dd HH:mm:ss.SSS"; public final static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; public final static String DATETIME_PATTERN_SHORT = "yyyy-MM-dd HH:mm"; public final static String DATETIME_PATTERN_CREAT_ORDER = "yyyy-MM-ddHH:mm"; public final static String DATETIME_SHORT_PATTERN = "yyyyMMddHHmmss"; public final static String DATETIME_MILLi_PATTERN = "yyyyMMddHHmmssSSS"; public final static String HOUR_MINUTE = "HH:mm"; public final static String HOUR_MINUTE_SECOND = "HH:mm:ss"; public final static String DATE_POINT = "yyyy.MM.dd"; private final static Logger logger = LoggerFactory.getLogger(DateUtils.class); public static final long MILLIS_PER_DAY = 24 * DateUtils.MILLIS_PER_HOUR; public static final long MILLIS_PER_HOUR = 60 * DateUtils.MILLIS_PER_MINUTE; public static final long MILLIS_PER_MINUTE = 60 * DateUtils.MILLIS_PER_SECOND; public static final long MILLIS_PER_SECOND = 1000; public static final int DAYS_PER_YEAR = 365; public final static String MONTH_PATTERN = "yyyy-MM"; private final static String[] WEEK_NAMES = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"}; public static Date addMonth(Date date, int months) { if (months == 0) { return date; } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, months); return c.getTime(); } public static Date addDay(Date date, int days) { if (days == 0) { return date; } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_YEAR, days); return c.getTime(); } public static Date getStartTime(Date date) { Calendar start = Calendar.getInstance(); start.setTime(date); start.set(Calendar.HOUR_OF_DAY, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); start.set(Calendar.MILLISECOND, 0); return convertDate(start.getTime(), DATETIME_PATTERN); } public static Date getMiddleTime() { Date date = convertDate(new Date(), DateUtils.DATE_PATTERN); Calendar middle = Calendar.getInstance(); middle.setTime(date); middle.set(Calendar.MONTH, 6); middle.set(Calendar.DAY_OF_MONTH, 1); middle.set(Calendar.HOUR_OF_DAY, 0); middle.set(Calendar.MINUTE, 0); middle.set(Calendar.SECOND, 0); middle.set(Calendar.MILLISECOND, 0); return convertDate(middle.getTime(), DATETIME_PATTERN); } public static Date getEndTime(Date date) { Calendar end = Calendar.getInstance(); end.setTime(date); end.set(Calendar.HOUR_OF_DAY, 23); end.set(Calendar.MINUTE, 59); end.set(Calendar.SECOND, 59); end.set(Calendar.MILLISECOND, 999); return convertDate(end.getTime(), DATETIME_PATTERN); } public static String getStartTime(String startDate) { if(StringUtils.isBlank(startDate)){ return null; } Date date = convertDate(startDate, DateUtils.DATE_PATTERN); Calendar start = Calendar.getInstance(); start.setTime(date); start.set(Calendar.HOUR_OF_DAY, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); start.set(Calendar.MILLISECOND, 0); return formatDate(start.getTime()); } public static String getMiddleDateTime() { Date date = convertDate(new Date(), DateUtils.DATE_PATTERN); Calendar middle = Calendar.getInstance(); middle.setTime(date); middle.set(Calendar.MONTH, 6); middle.set(Calendar.DAY_OF_MONTH, 1); middle.set(Calendar.HOUR_OF_DAY, 0); middle.set(Calendar.MINUTE, 0); middle.set(Calendar.SECOND, 0); middle.set(Calendar.MILLISECOND, 0); return formatDate(middle.getTime()); } public static String getEndTime(String endDate) { if (StringUtils.isBlank(endDate)) { return null; } Date date = convertDate(endDate, DateUtils.DATE_PATTERN); Calendar end = Calendar.getInstance(); end.setTime(date); end.set(Calendar.HOUR_OF_DAY, 23); end.set(Calendar.MINUTE, 59); end.set(Calendar.SECOND, 59); end.set(Calendar.MILLISECOND, 999); return formatDate(end.getTime()); } public static Date addDay(String date, int days) { return DateUtils.addDay(DateUtils.convertDate(date), days); } public static Date addMinute(Date date, int minute) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MINUTE, minute); return c.getTime(); } public static Date addHour(Date date, int hour) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.HOUR, hour); return c.getTime(); } public static boolean afterToday(Object date) { Date currentDate = new Date(); return DateUtils.compareDate(date, currentDate) == 1; } public static boolean afterTodayDate(Date date) { Date currentDate = new Date(); return currentDate.compareTo(date) == -1; } public static boolean beforeToday(Object date) { Date currentDate = new Date(); return DateUtils.compareDate(date, currentDate) == -1; } public static boolean beforeTodayDate(Date date) { Date currentDate = new Date(); return currentDate.compareTo(date) == 1; } public static int compareDate(Object date1, Object date2) { if (date1 == null || date2 == null) { String msg = "illegal arguments,date1 and date2 must be not null."; throw new IllegalArgumentException(msg); } Date d1 = (Date) (date1 instanceof String ? DateUtils.convertDate((String) date1) : date1); Date d2 = (Date) (date2 instanceof String ? DateUtils.convertDate((String) date2) : date2); return DateUtils.round(d1, Calendar.DATE).compareTo(DateUtils.round(d2, Calendar.DATE)); } public static Date convertDate(Date date, String pattern) { if (StringUtils.isBlank(pattern)) { String msg = "the date or pattern is empty."; throw new IllegalArgumentException(msg); } String dateForPattern = DateUtils.formatDate(date, pattern); return DateUtils.convertDate(dateForPattern, pattern); } public static Date convertDate(Long date) { return new Date(date); } public static Date convertDate(String date) { if (StringUtils.isBlank(date)) { return null; } if (StringUtils.isNumeric(date)) { long timestamp = Long.parseLong(date); if (timestamp > 0 && timestamp < Long.MAX_VALUE) { return new Date(timestamp); } else { return null; } } if (date.indexOf(':') > 0) { return DateUtils.convertDate(date, DateUtils.DATETIME_PATTERN); } else if (date.indexOf('.') > 0) { return DateUtils.convertDate(date, DateUtils.DATE_POINT); } else { return DateUtils.convertDate(date, DateUtils.DATE_PATTERN); } } public static Date convertDate(String date, String pattern) { try { if (StringUtils.isEmpty(pattern) || StringUtils.isEmpty(date)) { String msg = "the date or pattern is empty."; throw new IllegalArgumentException(msg); } SimpleDateFormat df = new SimpleDateFormat(pattern.trim()); return df.parse(date.trim()); } catch (Exception e) { DateUtils.logger.error("Method===DateUtils.convertDate error!", e); return null; } } public static Time convertTime(String time, String pattern) { if ("24:00:00".equals(time)) { time = "23:59:59"; } Date d = DateUtils.convertDate(time, pattern); return new Time(d.getTime()); } public static int diffDate(Date date1, Date date2) { return (int) ((date1.getTime() - date2.getTime()) / DateUtils.MILLIS_PER_DAY); } public static int diffMinute(Date date1, Date date2) { return (int) ((date1.getTime() - date2.getTime()) / DateUtils.MILLIS_PER_MINUTE); } public static String formatDate(Date date) { try { return DateUtils.formatDate(date, DateUtils.DATETIME_PATTERN); } catch (Exception e) { return null; } } public static String formatDate(Date date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern.trim()); return format.format(date); } public static String formatDateTime(Date date) { try { return DateUtils.formatDate(date, DateUtils.DATETIME_PATTERN); } catch (Exception e) { logger.error("时间转换异常",e); return null; } } public static String formatDateToYMDHMS(Date date) { return DateUtils.formatDate(date, DateUtils.DATETIME_SHORT_PATTERN); } public static String formatMonth(Date date) { return DateUtils.formatDate(date, DateUtils.MONTH_PATTERN); } public static String formatNowToYMDHMS() { return DateUtils.formatDateToYMDHMS(new Date()); } public static Timestamp getCurrentTimestamp() { return new Timestamp(new Date().getTime()); } public static Date getDateFromShortString(String str) { SimpleDateFormat simpleDF = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDF.parse(str); } catch (ParseException e) { DateUtils.logger.warn("parse date error", e); } return null; } public static Date getFirstDayOfThisWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } public static int getHourOfDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.HOUR_OF_DAY); } public static Date getLastMonth() { Calendar c = Calendar.getInstance(); int month = c.get(Calendar.MONTH); c.set(Calendar.MONTH, month - 1); return c.getTime(); } public static int getMinute(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MINUTE); } public static Date getNextNDay(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, n); return cal.getTime(); } public static int getWeekNumber(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int number = c.get(Calendar.DAY_OF_WEEK) - 1; if (number == 0) { number = 7; } return number; } public static String getWeekNumberString(Date date) { int dayNum = DateUtils.getWeekNumber(date); return DateUtils.WEEK_NAMES[dayNum - 1]; } public static boolean isSameDay(Object date1, Object date2) { return DateUtils.compareDate(date1, date2) == 0; } public static boolean isValidDate(String date, String pattern) { try { DateUtils.convertDate(date, pattern); return true; } catch (Exception e) { DateUtils.logger.debug("Method===DateUtils.isValidDate error!", e); return false; } } public static Timestamp now() { return new Timestamp(new Date().getTime()); } public static Long getStartOfDay(Long time) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); calendar.set(11, 0); calendar.set(12, 0); calendar.set(13, 0); calendar.set(14, 0); return calendar.getTimeInMillis(); } public static Long getEndOfDay(Long time) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); calendar.set(11, 23); calendar.set(12, 59); calendar.set(13, 59); calendar.set(14, 59); return calendar.getTimeInMillis(); } public static Date localDateTimeToDate(LocalDateTime localDateTime) { try { ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = localDateTime.atZone(zoneId); return Date.from(zdt.toInstant()); } catch (Exception e) { e.printStackTrace(); } return null; } public static String nowDateTime() { return DateUtils.formatDate(new Date(), DateUtils.DATETIME_PATTERN); } public static Date round(Date date, int roundType) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(date.getTime()); switch (roundType) { case Calendar.MONTH: c.set(Calendar.DAY_OF_MONTH, 1); case Calendar.DATE: c.set(Calendar.HOUR_OF_DAY, 0); case Calendar.HOUR: c.set(Calendar.MINUTE, 0); case Calendar.MINUTE: c.set(Calendar.SECOND, 0); case Calendar.SECOND: c.set(Calendar.MILLISECOND, 0); return c.getTime(); default: throw new IllegalArgumentException("invalid round roundType."); } } public static Date today() { return DateUtils.convertDate(DateUtils.formatDate(new Date()), DateUtils.DATE_PATTERN); } public static String todayDate() { return DateUtils.formatDate(new Date(), DateUtils.DATE_PATTERN); } public static Timestamp toTimestamp(String date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern.trim()); try { return new Timestamp(format.parse(date).getTime()); } catch (ParseException e) { return null; } } public static Date getFirstDateOfCurrentMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, 1); 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 getFirstDateFromDate(Date date) { 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.MILLISECOND, 0); return cal.getTime(); } public static String getFirstDate(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return formatDate(cal.getTime()); } public static String getLastDate(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, 11); cal.set(Calendar.DAY_OF_MONTH, 31); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 0); return formatDate(cal.getTime()); } public static boolean todayInClose(Date startDate, Date endDate) { Date currentDate = convertDate(new Date(), DATE_PATTERN); return currentDate.getTime() >= startDate.getTime() && currentDate.getTime() <= endDate.getTime(); // Rangerange=Ranges.closed(startDate,endDate); // return range.contains(currentDate); } // 时间转换成毫秒,空返回-1 public static Long getTime(Date date) { if (null == date) { return null; } return date.getTime(); } public static boolean timeIntersection(Date closeOneStartDate, Date closeOneEndDate, Date closeTwoStartDate, Date closeTwoEndDate) { long start = Math.max(closeOneStartDate.getTime(), closeTwoStartDate.getTime()); long end = Math.min(closeOneEndDate.getTime(), closeTwoEndDate.getTime()); return start <= end; } public static Date getMonthFirstDay(Date date) { Calendar cale = Calendar.getInstance(); cale.setTime(date); cale.add(Calendar.MONTH, 0); cale.set(Calendar.DAY_OF_MONTH, 1); return convertDate(cale.getTime(), DATE_PATTERN); } public static Date getMonthEndDay(Date date) { Calendar cale = Calendar.getInstance(); cale.setTime(date); cale.add(Calendar.MONTH, 1); cale.set(Calendar.DAY_OF_MONTH, 0); return convertDate(cale.getTime(), DATE_PATTERN); } public static String getMonthFirstDayString(String date) { Date date1 = DateUtils.convertDate(date); Calendar cale = Calendar.getInstance(); cale.setTime(date1); cale.add(Calendar.MONTH, 0); cale.set(Calendar.DAY_OF_MONTH, 1); return formatDate(cale.getTime(), DATE_PATTERN); } public static String getMonthEndDayString(String date) { Date date1 = DateUtils.convertDate(date); Calendar cale = Calendar.getInstance(); cale.setTime(date1); cale.add(Calendar.MONTH, 1); cale.set(Calendar.DAY_OF_MONTH, 0); return formatDate(cale.getTime(), DATE_PATTERN); } public static int getSeason(Date date) { int season = 0; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH); switch (month) { case Calendar.JANUARY: case Calendar.FEBRUARY: case Calendar.MARCH: season = 1; break; case Calendar.APRIL: case Calendar.MAY: case Calendar.JUNE: season = 2; break; case Calendar.JULY: case Calendar.AUGUST: case Calendar.SEPTEMBER: season = 3; break; case Calendar.OCTOBER: case Calendar.NOVEMBER: case Calendar.DECEMBER: season = 4; break; default: break; } return season; } public static int getYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); return year; } public static String getPreSeason(Date date) { int year = getYear(date); int season = getSeason(date); if (season == 1) { return (year - 1) + "-" + 4; } return year + "-" + (season - 1); } public static Long convertTimestamp(LocalDateTime localDateTime) { return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); } public static LocalDateTime convertLocalDateTime(Long second) { return LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8)); } public static Long getStartOfDay(String sdate) { DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN); try { Date date = dateFormat.parse(sdate); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); } catch (ParseException e) { return null; } } public static String getLastWeekMonday(String time, boolean isGetThisWeek) { SimpleDateFormat foramt = new SimpleDateFormat(DateUtils.DATE_PATTERN); Date date = null; try { date = foramt.parse(time); } catch (ParseException e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); //判断当前日期是否为周末,因为周末是本周第一天,如果不向后推迟一天的到的将是下周一的零点,而不是本周周一零点 if (1 == cal.get(Calendar.DAY_OF_WEEK)) { cal.add(Calendar.DATE, -1); } if (!isGetThisWeek) { //时间减去7天 cal.add(Calendar.DAY_OF_MONTH, -7); } //Calendar.MonDAY 这个是周一的意思 想获取周几就更换这个 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return foramt.format(cal.getTime()) + " 00:00:00"; } public static String getLastWeekySaturday(String time) { SimpleDateFormat foramt = new SimpleDateFormat(DateUtils.DATE_PATTERN); Date date = null; try { date = foramt.parse(time); } catch (ParseException e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); //时间减去7天 cal.add(Calendar.DAY_OF_MONTH, 6); return foramt.format(cal.getTime()) + " 23:59:59"; } public static int getWeek(String dateTime) { Date date = DateUtils.convertDate(dateTime, DateUtils.DATETIME_PATTERN); GregorianCalendar g = new GregorianCalendar(); g.setTime(date); return g.get(Calendar.WEEK_OF_YEAR); //获得周数 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)