DateFormat
import java.math.BigDecimal; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.*; public final class DateUtils { public static Date MIN = DateUtils.ofDate("19700101", DateFormat.NumDate); private DateUtils() { } /********************************************转化*************************************************/ /** * Date转化为时间戳 */ public static Timestamp dateToTimestamp(Date date) { return null == date ? null : new Timestamp(date.getTime()); } /** * 将timestamp本身以某种格式进行转化 * * @param date * @param dateFormat * @return */ public static Timestamp identity(Timestamp date, DateFormat dateFormat) { return null == date ? null : DateUtils.strToTimestamp(DateUtils.format(date, dateFormat)); } /** * String转化为时间戳 */ public static Timestamp strToTimestamp(String date) { return null == date ? null : Timestamp.valueOf(date); } public static Timestamp strToTimestamp(String date, DateFormat format) { try { return new Timestamp(new SimpleDateFormat(format.val()).parse(date).getTime()); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 时间戳转化为String */ public static String timestampTostr(Timestamp date) { return date == null ? null : date.toString(); } /** * 给定 format 将 Date 转换成 string **/ public static String format(Date date, DateFormat format) { return null == date ? null : new SimpleDateFormat(format.val()).format(date); } /** * 给定 format 将 LocalDate 转换成 string **/ public static String format(LocalDate date, DateFormat format) { return null == date ? null : date.format(DateTimeFormatter.ofPattern(format.val())); } /** * 给定 format 将 LocalDateTime 转换成 string **/ public static String format(LocalDateTime date, DateFormat format) { return null == date ? format.val() : date.format(DateTimeFormatter.ofPattern(format.val())); } /** * 给定 format 将秒时间戳转换成 string **/ public static String format(long unixTime, DateFormat format) { return format(ofLocalDateTime(unixTime), format); } /** * 给定 format 将 string 转换成 string **/ public static String format(Timestamp ts, DateFormat format) { try { return new SimpleDateFormat(format.val()).format(ts); } catch (Exception e) { throw new RuntimeException("ofDate error, sDate: " + ts + " format: " + format.val(), e); } } /** * 给定 format 将 yyyy-MM-dd HH:mm:ss SSS类型转化为 yyyy-MM-dd HH:mm:ss **/ public static String format(String date) { if (StringUtils.isNotBlank(date)) { int num = date.lastIndexOf(".0"); if (num != -1) { date = date.substring(0, num); } } return date; } /** * 给定 format 将 string 转换成 Date **/ public static Date ofDate(String sDate, DateFormat format) { try { return new SimpleDateFormat(format.val()).parse(sDate); } catch (Exception e) { throw new RuntimeException("ofDate error, sDate: " + sDate + " format: " + format.val(), e); } } /** * 将秒时间戳 转换成 Date **/ public static Date ofDate(long time) { return Date.from(Instant.ofEpochMilli(time)); } /** * 给定 format 将 string 转换成 LocalDate **/ public static LocalDate ofLocalDate(String sDate, DateFormat format) { return LocalDate.parse(sDate, DateTimeFormatter.ofPattern(format.val())); } /** * 将毫秒时间戳 转换成 LocalDate **/ public static LocalDate date2Local(long date) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault()).toLocalDate(); } /** * 将秒时间戳 转换成 LocalDate **/ public static LocalDate ofLocalDate(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).toLocalDate(); } /** * 将毫秒时间戳 转换成 LocalDateTime **/ public static LocalDateTime time2Local(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()); } /** * 给定 format 将 string 转换成 LocalDateTime **/ public static LocalDateTime ofLocalDateTime(String sDate, DateFormat format) { return LocalDateTime.parse(sDate, DateTimeFormatter.ofPattern(format.val())); } /** * 将秒时间戳 转换成 LocalDateTime **/ public static LocalDateTime ofLocalDateTime(long time) { return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()); } /** * 给定 format 将当前系统时间转换成 string **/ public static String now(DateFormat format) { return format(now(), format); } /** * 将 LocalDate 转换成毫秒 **/ public static Long time(Date date) { return null == date ? 0L : date.getTime(); } /** * 将 LocalDate 转换成毫秒 **/ public static Long time(LocalDate date) { return null == date ? 0L : date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 将 LocalDateTime 转换成毫秒 **/ public static Long time(LocalDateTime dateTime) { return null == dateTime ? 0L : dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /********************************************转化*************************************************/ /********************************************获取*************************************************/ /** * 当前系统时间 Date 类型 **/ public static Date now() { return Calendar.getInstance().getTime(); } /** * 取得当前时间戳 * * @return */ public static Timestamp getTimestamp() { return new Timestamp(System.currentTimeMillis()); } /** * 当前系统时间秒 **/ public static Long second() { return Instant.now().getEpochSecond(); } /** * 当前系统时间毫秒 **/ public static Long time() { return System.currentTimeMillis(); } /** * Date 时间秒 **/ public static Long second(Date date) { return null == date ? 0L : date.getTime() / 1000; } /** * LocalDate 时间秒 **/ public static Long second(LocalDate date) { return null == date ? 0L : date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } /** * LocalDateTime 时间秒 **/ public static Long second(LocalDateTime dateTime) { return null == dateTime ? 0L : dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } /** * 给定秒时间戳得出当天的开始时间秒 **/ public static long ofDayStart(long time) { Calendar calendar = Calendar.getInstance(); calendar.setTime(ofDate(time)); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.toInstant().toEpochMilli(); } /** * 在给定的 Date 时间加 days 天 **/ public static Date addDays(Date date, int days) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, days); return calendar.getTime(); } /** * from 1 (Monday) to 7 (Sunday) **/ public static int dayOfWeek() { return LocalDate.now().getDayOfWeek().getValue(); } /** * 给定的 date 为周几 from 1 (Monday) to 7 (Sunday) **/ public static int dayOfWeek(LocalDate date) { return null == date ? 0 : date.getDayOfWeek().getValue(); } /** * 给定时间为一周的第几天,from 1 (Monday) to 7 (Sunday), 0 InvalidDate **/ public static int dayOfWeek(Date date) { return null == date ? 0 : ofLocalDate(date.getTime()).getDayOfWeek().getValue(); } /** * 获取某个日期对应的星期 **/ public static String getWeekOfDate(Date date) { String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) { w = 0; } return weekDays[w]; } /** * 获取两个日期之前相差的天数据 */ public static int daySize(Date startDate, Date endDate) { if (startDate == null || endDate == null) { return -1; } long timMillis = ofDayStart(endDate.getTime()) - ofDayStart(startDate.getTime()); return BigDecimal.valueOf(timMillis) .divide(BigDecimal.valueOf(24 * 60 * 60 * 1000L), 0, BigDecimal.ROUND_HALF_UP).intValue(); } /** * 取当月天数 **/ public static int monthDays(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DATE, 1); calendar.roll(Calendar.DATE, -1); return calendar.get(Calendar.DATE); } /** * 获取当前时间所在周的开始日期 **/ public static Date firstDayOfWeek(Date date) { if (null == date) { return DateUtils.MIN; } Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(date); calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); return calendar.getTime(); } /** * 获取当前时间所在周的结束日期 **/ public static Date lastDayOfWeek(Date date) { if (null == date) { return DateUtils.MIN; } Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(date); calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); return calendar.getTime(); } //获取当天的开始时间 public static java.util.Date getDayBegin() { Calendar cal = new GregorianCalendar(); 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 java.util.Date getDayEnd() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return cal.getTime(); } //获取昨天的开始时间 public static Date getBeginDayOfYesterday() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } //获取昨天的结束时间 public static Date getEndDayOfYesterDay() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } //获取明天的开始时间 public static Date getBeginDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //获取明天的结束时间 public static Date getEndDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //获取本周的开始时间 public static Date getBeginDayOfWeek() { Date date = new Date(); if (date == null) { return null; } Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return getDayStartTime(cal.getTime()); } //获取本周的结束时间 public static Date getEndDayOfWeek() { Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek()); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } //获取本月的开始时间 public static Date getBeginDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); return getDayStartTime(calendar.getTime()); } //获取本月的结束时间 public static Date getEndDayOfMonth() { Calendar calendar = Calendar.getInstance(); calendar.set(getNowYear(), getNowMonth() - 1, 1); int day = calendar.getActualMaximum(5); calendar.set(getNowYear(), getNowMonth() - 1, day); return getDayEndTime(calendar.getTime()); } /** * 获取上月的结束时间 */ public static Date getEndDayOfLastMonth() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59); return c.getTime(); } /** * 获取上月的开始时间 */ public static Date getBeginDayOfLastMonth() { try { return new SimpleDateFormat(DateFormat.StrikeDateTime.val()).parse(new SimpleDateFormat("yyyy-MM-01 00:00:00").format(getEndDayOfLastMonth())); } catch (ParseException e) { e.printStackTrace(); return null; } } //获取本年的开始时间 public static java.util.Date getBeginDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); // cal.set cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayStartTime(cal.getTime()); } //获取本年的结束时间 public static java.util.Date getEndDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEndTime(cal.getTime()); } //获取某个日期的开始时间 public static Timestamp getDayStartTime(Date d) { Calendar calendar = Calendar.getInstance(); if (null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); return new Timestamp(calendar.getTimeInMillis()); } //获取某个日期的结束时间 public static Timestamp getDayEndTime(Date d) { Calendar calendar = Calendar.getInstance(); if (null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); return new Timestamp(calendar.getTimeInMillis()); } //获取今年是哪一年 public static Integer getNowYear() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return Integer.valueOf(gc.get(1)); } //获取本月是哪一月 public static int getNowMonth() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return gc.get(2) + 1; } //获取两个日期中的最大日期 public static Date max(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return beginDate; } return endDate; } //获取两个日期中的最小日期 public static Date min(Date beginDate, Date endDate) { if (beginDate == null) { return endDate; } if (endDate == null) { return beginDate; } if (beginDate.after(endDate)) { return endDate; } return beginDate; } //返回某月该季度的第一个月 public static Date getFirstSeasonDate(Date date) { final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int sean = SEASON[cal.get(Calendar.MONTH)]; cal.set(Calendar.MONTH, sean * 3 - 3); return cal.getTime(); } //返回某个日期下几天的日期 public static Date getNextDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); return cal.getTime(); } //返回某个日期前几天的日期 public static Date getFrontDay(Date date, int i) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); return cal.getTime(); } /** * 获取某年某月到某年某月按天的切片日期集合(间隔天数的日期集合) */ public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) { List list = new ArrayList(); if (beginYear == endYear) { for (int j = beginMonth; j <= endMonth; j++) { list.add(getTimeList(beginYear, j, k)); } } else { { for (int j = beginMonth; j < 12; j++) { list.add(getTimeList(beginYear, j, k)); } for (int i = beginYear + 1; i < endYear; i++) { for (int j = 0; j < 12; j++) { list.add(getTimeList(i, j, k)); } } for (int j = 0; j <= endMonth; j++) { list.add(getTimeList(endYear, j, k)); } } } return list; } //获取某年某月按天切片日期集合(某个月间隔多少天的日期集合) public static List getTimeList(int beginYear, int beginMonth, int k) { List list = new ArrayList(); Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); int max = begincal.getActualMaximum(Calendar.DATE); for (int i = 1; i < max; i = i + k) { list.add(begincal.getTime()); begincal.add(Calendar.DATE, k); } begincal = new GregorianCalendar(beginYear, beginMonth, max); list.add(begincal.getTime()); return list; } /** * 获取指定时间几天前或后的日期,ba表示beforeAfter * * @param flag true:表示days天后,false:表示days天前 * @param days * @return */ public static Date baDays(Date date, Integer days, boolean flag) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); if (flag) { //表示当前时间days天后的日期 calendar.add(Calendar.DATE, days); } else { //表示当前时间days天前的日期 calendar.add(Calendar.DATE, -days); } return calendar.getTime(); } /** * @param date * @param hour * @param flag * @return */ public static Date baHours(Date date, Integer hour, boolean flag) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); if (flag) { //表示当前时间days天后的日期 calendar.add(Calendar.HOUR, hour); } else { //表示当前时间days天前的日期 calendar.add(Calendar.HOUR, -hour); } return calendar.getTime(); } /** * 获取指定日期几分钟前或后的时间 * * @param date * @param minute * @param flag * @return */ public static Date baMinutes(Date date, Integer minute, boolean flag) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); if (flag) { //表示当前时间days天后的日期 calendar.add(Calendar.MINUTE, minute); } else { //表示当前时间days天前的日期 calendar.add(Calendar.MINUTE, -minute); } return calendar.getTime(); } /** * 获取指定时间的开始时间 * * @param date * @return */ public static Date begin(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 获取指定时间的结束时间 * * @param date * @return */ public static Date end(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); } /********************************************获取*************************************************/ /********************************************判断*************************************************/ /** * 是否今天 **/ public static boolean isToday(LocalDate date) { LocalDate now = LocalDate.now(); return now.getYear() == date.getYear() && now.getMonthValue() == date.getMonthValue() && now.getDayOfMonth() == date.getDayOfMonth(); } /** * 是否今天 **/ public static boolean isToday(String day, DateFormat format) { if (day == null) { return false; } Calendar pre = Calendar.getInstance(); Date predate = new Date(System.currentTimeMillis()); pre.setTime(predate); Calendar cal = Calendar.getInstance(); Date date = ofDate(day, format); cal.setTime(date); if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) { int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR); if (diffDay == 0) { return true; } } return false; } /** * 是否昨天 **/ public static boolean isYesterday(String day, DateFormat format) throws ParseException { Calendar pre = Calendar.getInstance(); Date predate = new Date(System.currentTimeMillis()); pre.setTime(predate); Calendar cal = Calendar.getInstance(); Date date = ofDate(day, format); cal.setTime(date); if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) { int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR); if (diffDay == -1) { return true; } } return false; } /********************************************判断*************************************************/ /********************************************计算*************************************************/ //两个日期相减得到的天数 public static int getDiffDays(Date beginDate, Date endDate) { if (beginDate == null || endDate == null) { throw new IllegalArgumentException("getDiffDays param is null!"); } long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24); int days = new Long(diff).intValue(); return days; } //两个日期相减得到的毫秒数 public static long dateDiff(Date beginDate, Date endDate) { long date1ms = beginDate.getTime(); long date2ms = endDate.getTime(); return date2ms - date1ms; } /********************************************计算*************************************************/ }