import org.slf4j.LoggerFactory; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by IntelliJ IDEA * 这是一个神奇的Class * * @author zhz * @date 2020/3/26 14:01 */ public class TimeUtil { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TimeUtil.class); public static final SimpleDateFormat FMyyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static final SimpleDateFormat FMyyyyMMdd= new SimpleDateFormat("yyyy-MM-dd"); public static final SimpleDateFormat FMyyyyMM= new SimpleDateFormat("yyyy-MM"); public static final SimpleDateFormat CN_FMyyyyMMdd= new SimpleDateFormat("yyyy年MM月dd日"); public static final SimpleDateFormat yyyyMMdd= new SimpleDateFormat("yyyyMMdd"); //自定义格式,格式化时间 public static String getFormatDate(Date date, String aFormat) { String tDate =new SimpleDateFormat(aFormat).format(date); return tDate; } //yyyy-MM-dd HH:mm:ss格式时间字符串 public static String getFormatDateTime(Date date) { String tDate =FMyyyyMMddHHmmss.format(date); return tDate; } //yyyy-MM-dd 格式时间字符串 public static String getFormatDate(Date date) { String tDate =FMyyyyMMdd.format(date); return tDate; } //yyyy-MM格式时间字符串 public static String getFormatYearMonth(Date date) { String tDate =FMyyyyMM.format(date); return tDate; } //yyyy年MM月dd日格式时间字符串 public static String getCnFormatDateTime(Date date) { String tDate =CN_FMyyyyMMdd.format(date); return tDate; } //yyyy-MM-dd HH:mm:ss或者yyyy-MM-dd转换为日期类型 public static Date parseFormatDateTime(String dateStr) { try { return dateStr.length()>12?FMyyyyMMddHHmmss.parse(dateStr):FMyyyyMMdd.parse(dateStr); } catch (ParseException e) { logger.error("无效的日期格式,{}",e); return null; } } //yyyyMMdd转换为日期类型 public static Date parseIDFormatDate(String dateStr) { try { return new SimpleDateFormat("yyyyMMdd").parse(dateStr); } catch (ParseException e) { logger.error("无效的日期格式,{}",e); return null; } } /** * 获取今天的开始时间(日期类型) * @return */ public static Date getTodayStart(){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date(System.currentTimeMillis())); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); return calendar.getTime(); } /** * 获取今天的开始时间(yyyy-MM-dd HH:mm:ss类型字符串) * @return */ public static String getFormatTodayStart() { return getFormatDateTime(getTodayStart()); } /** * 获取今天的结束时间(日期类型) * @return */ public static Date getTodayEnd(){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date(System.currentTimeMillis())); calendar.set(Calendar.HOUR_OF_DAY,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); return calendar.getTime(); } /** * 获取今天的结束时间(yyyy-MM-dd HH:mm:ss类型字符串) * @return */ public static String getFormatTodayEnd() { return getFormatDateTime(getTodayEnd()); } /** * 获取一天的开始时间(日期类型) * @param date * @return */ public static Date getStartDateOfDay(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(); } /** * 获取一天的开始时间(yyyy-MM-dd HH:mm:ss类型字符串) * @param date * @return */ public static String getStartTimeOfDay(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 getFormatDateTime(calendar.getTime()); } /** * 获取一天的开始时间 * @param dateStr * @return */ public static String getStartTimeOfDay(String dateStr){ try { getStartTimeOfDay(FMyyyyMMdd.parse(dateStr)); } catch (ParseException e) { logger.error("日期格式错误:{}",e); } return null; } /** * 某一个日期是否属于最近几天 * @param date * @param days * @return */ public boolean lastDay(Date date,Integer days){ if(date==null) { return false; } Calendar ca= Calendar.getInstance(); ca.roll(Calendar.DAY_OF_YEAR, -days); return date.after(ca.getTime()); } /** * 获取一天的结束时间 * @param date * @return */ public static String getEndTimeOfDay(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 getFormatDateTime(calendar.getTime()); } public static String getEndTimeOfDay(String dateStr){ try { getEndTimeOfDay(FMyyyyMMdd.parse(dateStr)); } catch (ParseException e) { logger.error("日期格式错误:{}",e); } return null; } /** * 获取下个月结束时间 * @param currentTime * @return */ public static Date getNextMonthTime(Date currentTime){ Calendar calendar = Calendar.getInstance(); calendar.setTime(currentTime); calendar.set(Calendar.HOUR_OF_DAY,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); calendar.add(Calendar.MONTH,1); return calendar.getTime(); } /** * 获取本月开始时间 * @param currentTime * @return */ public static Date getMonthStartTime(Date currentTime){ Calendar calendar = Calendar.getInstance(); calendar.setTime(currentTime); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.DAY_OF_MONTH,1); return calendar.getTime(); } /** * 获取本月开始时间 * @param currentTime * @return */ public static Date getMonthEndTime(Date currentTime){ Calendar calendar = Calendar.getInstance(); calendar.setTime(currentTime); calendar.set(Calendar.HOUR_OF_DAY,23); calendar.set(Calendar.MINUTE,59); calendar.set(Calendar.SECOND,59); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); } /** * 取前后日期 * @param currentTime 当前时间 * @param stepLen 长度 * @return */ public static Date getNextDay(Date currentTime,int stepLen){ Calendar calendar = Calendar.getInstance(); calendar.setTime(currentTime); calendar.add(Calendar.DAY_OF_MONTH,stepLen); return calendar.getTime(); } /** * 比较是否同一天 * @param date1 * @param date2 * @return */ public static boolean isSameDay(Date date1,Date date2){ Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); if(cal1 != null && cal2 != null) { return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6); } else { return false; } } /** * 获取天数区间 * @param start * @param end * @return */ public static int getDaysBetween(Date start, Date end) { Calendar d1 = Calendar.getInstance(); d1.setTime(start); Calendar d2 = Calendar.getInstance(); d2.setTime(end); if (d1.after(d2)) { Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { d1 = (Calendar) d1.clone(); do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数 d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; } /** * 获取天数区间 * @param start * @param end * @return */ public static int getYearsBetween(Date start, Date end) { Calendar d1 = Calendar.getInstance(); d1.setTime(start); Calendar d2 = Calendar.getInstance(); d2.setTime(end); if (d1.after(d2)) { Calendar swap = d1; d1 = d2; d2 = swap; } return d2.get(Calendar.YEAR) - d1.get(Calendar.YEAR); } /** * 获取当前时间 * @return */ public static Date getCurrentDate() { return new Date(System.currentTimeMillis()); } /** * 向一个老日期追加天数,返回一个新日期; * @param oldDate 被追加天数的日期; * @param days 追加的天数; * @return 返回一个日历对象,追加天数后的日期;通过Calendar.getTime()可获得; */ public static Calendar addDaysFromOldDate(Date oldDate, int days) { // 添加结束时间; Calendar calendar = Calendar.getInstance(); calendar.setTime(oldDate); calendar.add(Calendar.DATE, days); return calendar; } }
参考: https://blog.csdn.net/Solder_Sun/article/details/81364757