package com.mwkj.common.util;
import com.mwkj.common.constant.DateConst; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*;
/**
• 日期工具类 */ public class DateUtils {
private static final Logger log = LoggerFactory.getLogger(DateUtils.class); /**• 一分钟的秒数 */ public static final int SECONDS_ONE_MINUTE = 60;
/**
• 一秒的毫秒数 */ public static final int MILLISECONDS_ONE_SECOND = 1000;
/**
• 一小时的秒数 */ public static final int SECONDS_ONE_HOUR = 60 * SECONDS_ONE_MINUTE;
/**
• 一天的秒数 / public static final int SECONDS_ONE_DAY = 24 * SECONDS_ONE_HOUR; /*
• 一天的毫秒数 */ public static final int MILL_SECONDS_ONE_DAY = 24 * SECONDS_ONE_HOUR;
private static String defaultDatePattern = null;
private static String DEFAULT_FORMAT = "yyyy-MM-dd";
private static final Date checkSinceDate = getCheckSinceDate("20160301");
public static final String YYYYMMDDHHNNSS_FORMAT = "yyyyMMddHHmmss";
public static final String YYYY_MM_DD_HH_NN_SS_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
• 根据inFmt的样式,日期时间字符串转化为日期时间对象
• 
• @param datestr String 日期时间字符串,如:20041001、2004年10月01日 15:03
• @param dateConst 时间格式的类型
• @return Date 日期时间对象,格式inFmt非法时,使用yyyyMMdd格式
• @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常 */ public static Date getDate(String datestr, DateConst dateConst) { if (null == datestr || "".equals(datestr.trim())) { return null; }
String inFmt = "yyyyMMdd"; if (null != dateConst) { // inFmt非法 inFmt = dateConst.getDesc(); }
Date inDate = null;
// 依据inFmt格式把日期字符串转化为日期对象 SimpleDateFormat inDateFormat = new SimpleDateFormat(inFmt); inDateFormat.setLenient(true); try { inDate = inDateFormat.parse(datestr); } catch (ParseException e) { log.info(e.getMessage()); } inDateFormat = null; return inDate; }public static int getIntervalMinute(Date startDate, Date endDate) { long startdate = startDate.getTime(); long enddate = endDate.getTime(); long interval = enddate - startdate; int intervalNum = (int) (interval / (1000 * 60)); return intervalNum; }
/**
• 只比较日期, 时分秒忽略,
• 
• 
@param baseDate 基准日期
• @param date2 用于比较的日期
• @return 0 表示, 同样的日期, 1 表示 日期1 在日期2 后, -1 表示日期1 在日期2 前 */ public static int compareDate(Date baseDate, Date date2) { GregorianCalendar calendar1 = new GregorianCalendar(); calendar1.setTime(baseDate); calendar1.set(Calendar.AM_PM, 0); calendar1.set(Calendar.HOUR, 0); calendar1.set(Calendar.MINUTE, 0); calendar1.set(Calendar.SECOND, 0); calendar1.set(Calendar.MILLISECOND, 0); GregorianCalendar calendar2 = new GregorianCalendar(); calendar2.setTime(date2); calendar2.set(Calendar.AM_PM, 0); calendar2.set(Calendar.HOUR, 0); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); calendar2.set(Calendar.MILLISECOND, 0); return calendar1.compareTo(calendar2); }
/**
• 返回预设Format的当前日期字符串 */ public static String getTodayStr() { Date today = new Date(); return format(today); }
/**
• 使用预设Format格式化Date成字符串yyyy-mm-dd */ public static String format(Date date) { return date == null ? "" : format(date, getDatePattern()); }
/**
• 使用参数Format格式化Date成字符串 yyyy-mm-dd */ public static String format(Date date, String pattern) { return date == null ? "" : new SimpleDateFormat(pattern).format(date); }
/**
• Return default datePattern (yyyy-MM-dd)
• 
• @return a string representing the date pattern on the UI */ public static String getDatePattern() { try { defaultDatePattern = "yyyy-MM-dd"; } catch (MissingResourceException mse) { defaultDatePattern = "yyyy-MM-dd"; }
return defaultDatePattern; }public static String formatToDateStart(String date) { if (null == date) { return null; } return date + " 00:00:00"; }
public static String formatToDateEnd(String date) { if (null == date) { return null; } return date + " 23:59:59"; }
/**
• 解析格式为yyyy-MM-dd时间
• 
• @param dateStr
• @return */ public static Date parseDate(String dateStr) { return parseDate(dateStr, DEFAULT_FORMAT); }
/**
• 根据所给格式将字符串转为日期
• 
• @param dateStr
• @param formater
• @return */ public static Date parseDate(String dateStr, String formater) { SimpleDateFormat format = new SimpleDateFormat(); format.applyPattern(formater); try { return format.parse(dateStr); } catch (ParseException e) { log.error("parse date string error", e); } return null; }
public static Date getNextDay(int days) { Date date = new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.DATE, days);//把日期往后增加一天.整数往后推,负数往前移动 date = calendar.getTime(); //这个时间就是日期往后推一天的结果
 return date; }/**
• add by zengshibin 2015-04-22
• 获取偏移X天后的某个时间 */ public static Date getNextDateTime(int days, int hours, int minute, int seconds) { try { Calendar cd = Calendar.getInstance(); cd.setTime(new Date()); cd.add(Calendar.DATE, days);// 增加一天 cd.set(Calendar.HOUR_OF_DAY, hours); cd.set(Calendar.MINUTE, minute); cd.set(Calendar.SECOND, seconds); Date date = cd.getTime(); return date; } catch (Exception e) { return new Date(); } }
/**
• 获取指定格式的当前日期串 */ public static String getCurrentDateStr(String formate) { Date today = new Date(); if (null == formate) { formate = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sf = new SimpleDateFormat(formate); return sf.format(today); }
/**
• add by zengshibin 2015-12-6
• 获取偏移后的某个时间 */ public static Date getSubDateTime(int days, int hours, int minute, int seconds) { try { Calendar cd = Calendar.getInstance(); cd.setTime(new Date()); cd.add(Calendar.DATE, -days);// 增加一天 cd.add(Calendar.HOUR_OF_DAY, -hours); cd.add(Calendar.MINUTE, -minute); cd.add(Calendar.SECOND, -seconds); Date date = cd.getTime(); return date; } catch (Exception e) { return new Date(); } }
/**
• add by zengshibin 2016-01-26
• 设置日期的时分秒 */ public static Date setDateHMS(Date date, int hours, int minutes, int seconds) { try { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.set(Calendar.HOUR, hours); cd.set(Calendar.MINUTE, minutes); cd.set(Calendar.SECOND, seconds); return cd.getTime(); } catch (Exception e) { return new Date(); } }
/**
• add by zengshibin 2016-03-01 获取 时间按天位移后的日期字符串 */ public static String getAddDaysStr(String formate, int days) { Date date = getNextDay(days); if (null == formate) { formate = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sf = new SimpleDateFormat(formate); return sf.format(date); }
/**
• 获取传入日期的前一天
• 
• @return */ public static Date getPreDate(Date date) { return DateUtils.getFrontDate(date, 1); }
/**
• 获取传入日期的前N天
• 
• @param date
• @param days
• @return */ public static Date getFrontDate(Date date, int days) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.set(Calendar.DAY_OF_YEAR, cd.get(Calendar.DAY_OF_YEAR) - days); return cd.getTime(); }
/**
• add by zengshibin 2016-03-21
• 获取偏移X天后的凌晨时间 */ public static Date getNextDateZeroTime(int days) { try { Calendar cd = Calendar.getInstance(); cd.setTime(new Date()); cd.set(Calendar.AM_PM, 0); cd.add(Calendar.DATE, days);// 增加一天 cd.set(Calendar.HOUR_OF_DAY, 0); cd.set(Calendar.MINUTE, 0); cd.set(Calendar.SECOND, 0); cd.set(Calendar.MILLISECOND, 0); Date date = cd.getTime(); return date; } catch (Exception e) { return new Date(); } }
public static Date addDay(Date date, int days) { if (date == null) { date = new Date(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, days);

 return calendar.getTime();}
/**
• 获取传入日期所在周的周一
• 
• @param date
• @return */ public static Date getMondayOfWeek(Date date) { Calendar ins = Calendar.getInstance(); ins.setTime(date); ins.add(Calendar.DAY_OF_MONTH, -1);//解决周日会出现 并到下一周的情况 ins.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return ins.getTime(); }
/**
• 获取传入日期所在周的周日
• 
• @param date
• @return */ public static Date getSundayOfWeek(Date date) { Date monday = DateUtils.getMondayOfWeek(date); Calendar ins = Calendar.getInstance(); ins.setTime(monday); ins.add(Calendar.DAY_OF_YEAR, 6); return ins.getTime(); }
/**
• 获取传入日期所在月的第一天
• 
• @param date
• @return */ public static Date getFistDayOfMonth(Date date) { Calendar ins = Calendar.getInstance(); ins.setTime(date); ins.set(Calendar.DAY_OF_MONTH, 1); return ins.getTime(); }
/**
• 获取传入日期所在月的最后一天
• 
• @param date
• @return */ public static Date getLastDayOfMonth(Date date) { Calendar ins = Calendar.getInstance(); ins.setTime(date); ins.set(Calendar.DAY_OF_MONTH, ins.getActualMaximum(Calendar.DAY_OF_MONTH)); return ins.getTime(); }
/**
• 得到本季度第一天的日期
• 
• @return Date
• @Methods Name getFirstDayOfQuarter */ public static Date getFirstDayOfSeason(Date date) { Calendar cDay = Calendar.getInstance(); cDay.setTime(date); int curMonth = cDay.get(Calendar.MONTH); if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH) { cDay.set(Calendar.MONTH, Calendar.JANUARY); } if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE) { cDay.set(Calendar.MONTH, Calendar.APRIL); } if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) { cDay.set(Calendar.MONTH, Calendar.JULY); } if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) { cDay.set(Calendar.MONTH, Calendar.OCTOBER); } cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH)); return cDay.getTime(); }
/**
• 得到本季度最后一天的日期
• 
• @return Date
• @Methods Name getLastDayOfQuarter */ public static Date getLastDayOfSeason(Date date) { Calendar cDay = Calendar.getInstance(); cDay.setTime(date); int curMonth = cDay.get(Calendar.MONTH); if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH) { cDay.set(Calendar.MONTH, Calendar.MARCH); } if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE) { cDay.set(Calendar.MONTH, Calendar.JUNE); } if (curMonth >= Calendar.JULY && curMonth <= Calendar.AUGUST) { cDay.set(Calendar.MONTH, Calendar.AUGUST); } if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) { cDay.set(Calendar.MONTH, Calendar.DECEMBER); } cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH)); return cDay.getTime(); }
/**
• 获取传入日期所在年份的第一天
• 
• @param date
• @return */ public static Date getFirstDayOfYear(Date date) { Calendar ins = Calendar.getInstance(); ins.setTime(date); int year = ins.get(Calendar.YEAR); ins.clear(); ins.set(Calendar.YEAR, year); return ins.getTime(); }
/**
• 获取传入日期所在年份的最后一天
• 
• @param date
• @return */ public static Date getLastDayOfYear(Date date) { Calendar ins = Calendar.getInstance(); ins.setTime(date); int year = ins.get(Calendar.YEAR); ins.clear(); ins.set(Calendar.YEAR, year); ins.roll(Calendar.DAY_OF_YEAR, -1); return ins.getTime(); }
public static List<Map<String, Object>> bulidQueryDateItem() { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); Date today = new Date(); Map<String, Object> yesterday = new HashMap<String, Object>(); yesterday.put("item", "昨日"); yesterday.put("startDate", DateUtils.format(DateUtils.getPreDate(today))); yesterday.put("endDate", DateUtils.format(DateUtils.getPreDate(today))); yesterday.put("active", true); result.add(yesterday); Map<String, Object> week = new HashMap<String, Object>(); week.put("item", "本周"); week.put("startDate", DateUtils.format(DateUtils.getMondayOfWeek(today))); week.put("endDate", DateUtils.format(DateUtils.getSundayOfWeek(today))); result.add(week); Map<String, Object> month = new HashMap<String, Object>(); month.put("item", "本月"); month.put("startDate", DateUtils.format(DateUtils.getFistDayOfMonth(today))); month.put("endDate", DateUtils.format(DateUtils.getLastDayOfMonth(today))); result.add(month); Map<String, Object> season = new HashMap<String, Object>(); season.put("item", "本季"); season.put("startDate", DateUtils.format(DateUtils.getFirstDayOfSeason(today))); season.put("endDate", DateUtils.format(DateUtils.getLastDayOfSeason(today))); result.add(season); Map<String, Object> year = new HashMap<String, Object>(); year.put("item", "本年"); year.put("startDate", DateUtils.format(DateUtils.getFirstDayOfYear(today))); year.put("endDate", DateUtils.format(DateUtils.getLastDayOfYear(today))); result.add(year); return result; }
/**
• @return */ public static List<Integer> buildNext6DayNum() { List<Integer> days = new ArrayList<Integer>(); days.add(6); days.add(5); days.add(4); days.add(3); days.add(2); days.add(1); return days; }
public static Date getDefaultExpireDate() { Calendar cDay = Calendar.getInstance(); cDay.set(Calendar.YEAR, 2037); cDay.set(Calendar.MONTH, Calendar.JANUARY); cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH)); return cDay.getTime(); }
private static Date getCheckSinceDate(String dateStr) { Date date = null; try { date = getDate(dateStr, null); } catch (Exception e) { log.error("日期转换错误:", e); } return date; }
/**
• 获取对账开始日期,当前为2016-03-01,之前的数据都不获取
• 
• @return */ public static Date getCheckSinceDate() { return checkSinceDate; }
/**
• 将日期转换成相应字符串,日期为空则返回空串
• 
• @param baseDate
• @param formate
• @return */ public static String getDateStrByFormate(Date baseDate, String formate) { if (null == baseDate) { return null; } if (StringUtils.isEmpty(formate)) { formate = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat sf = new SimpleDateFormat(formate); try { Calendar cd = Calendar.getInstance(); cd.setTime(baseDate); Date date = cd.getTime(); return sf.format(date); } catch (Exception e) { return sf.format(baseDate); } }
/**
• 将字符串转成日期,日期为空则返回空
• 
• @param dateStr
• @param format
• @return */ public static Date formateStrToDate(String dateStr, String format) { if (StringUtils.isEmpty(dateStr)) { return null; } if (StringUtils.isEmpty(format)) { format = YYYY_MM_DD_HH_NN_SS_FORMAT; } SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); Date date = null; try { date = simpleDateFormat.parse(dateStr); } catch (ParseException e) { log.error("字符串转化日期失败!" + e.getMessage(), e); } return date; }
/**
• 获取偏移后的时间,如指定时间的1天2小时30分钟后
• 
• @param baseDate
• @param days
• @param hours
• @param minute
• @param seconds
• @return */ public static Date getTimeAfterAddTime(Date baseDate, int days, int hours, int minute, int seconds) { try { Calendar cd = Calendar.getInstance(); cd.setTime(baseDate); cd.add(Calendar.DATE, days); cd.add(Calendar.HOUR_OF_DAY, hours); cd.add(Calendar.MINUTE, minute); cd.add(Calendar.SECOND, seconds); Date date = cd.getTime(); return date; } catch (Exception e) { return new Date(); } }
/**
• 根据传入的时间 获取添加月份后的实时间
• 如果是最后一天 --都按照最后一天
• 如果这个天数 大于其他的天数 就取最大天数
• 
• @param month
• @param now
• @return */ public static String getAdMonthStr(Integer month, Date now) { log.info("now=" + new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(now) + "\tmonth=" + month); String result = ""; Calendar nowCal = getCalendar(now); int dayNow = nowCal.get(Calendar.DATE);//获取日 String nowStr = new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(now);
nowCal.set(Calendar.DAY_OF_MONTH, 1); nowCal.roll(Calendar.DAY_OF_MONTH, -1); int dayLast = nowCal.get(Calendar.DATE);//获取日 Date nowLast = nowCal.getTime(); String nowLastStr = new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(nowLast);
Calendar addCal = getCalendar(now); int dayAddNow = addCal.get(Calendar.DATE);//获取日 addCal.add(Calendar.MONTH, month); Date add = addCal.getTime(); String addStr = new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(add);
addCal.set(Calendar.DAY_OF_MONTH, 1); addCal.roll(Calendar.DAY_OF_MONTH, -1); int dayAddLast = addCal.get(Calendar.DATE);//获取日 Date addLast = addCal.getTime(); String addLastStr = new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(addLast);
//其他都正常计算 result = addStr; //如果这个天数 大于其他的天数 就取最大天数 if (dayNow > dayAddLast) { result = addLastStr; } //如果是最后一天 --都按照最后一天 if (dayNow == dayLast) { result = addLastStr; } log.info("\t当前时间\t" + nowStr + "\tdayNow\t" + dayNow); log.info("\t当前时间月尾\t" + nowLastStr + "\tdayLast\t" + dayLast); log.info("\t加上月份后时间\t" + addStr + "\tdayAddNow\t" + dayAddNow); log.info("\t加上月份后月尾\t" + addLastStr + "\tdayAddLast\t" + dayAddLast);
log.info("最后结果是" + result); return result; }private static Calendar getCalendar(Date now) { Calendar nowCal = Calendar.getInstance(); nowCal.setTime(now); nowCal.set(Calendar.HOUR_OF_DAY, 0); nowCal.set(Calendar.MINUTE, 0); nowCal.set(Calendar.SECOND, 0); nowCal.set(Calendar.MILLISECOND, 0); return nowCal; }
/**
• 添加天数
• 
• @param days
• @return */ public static String getAddDayStr(Integer days, Date fromTime) { Calendar calendar = getCalendar(days, fromTime); Date delayTime = calendar.getTime(); return new SimpleDateFormat(DateConst.HYPHEN_DISPLAY_DATE.getDesc()).format(delayTime); }
private static Calendar getCalendar(Integer days, Date fromTime) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTime(fromTime); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DATE, days); return calendar; }
public static Date toFromDate(Date date) { if (null == date) { return toFromDate(new Date()); } String curYMD = format(date, DateConst.HYPHEN_DISPLAY_DATE.getDesc()); String beginDate = curYMD + " 00:00:00"; return getDate(beginDate, DateConst.HYPHEN_DISPLAY_TIME); }
public static Date toEndDate(Date date) { if (null == date) { return toEndDate(new Date()); } String curYMD = format(date, DateConst.HYPHEN_DISPLAY_DATE.getDesc()); String endDate = curYMD + " 23:59:59"; return getDate(endDate, DateConst.HYPHEN_DISPLAY_TIME); }
public static String toFromStr(Date date) { if (null == date) { return ""; } String curYMD = format(date, DateConst.HYPHEN_DISPLAY_DATE.getDesc()); String beginDate = curYMD + " 00:00:00"; return beginDate; }
public static String toEndStr(Date date) { if (null == date) { return ""; } String curYMD = format(date, DateConst.HYPHEN_DISPLAY_DATE.getDesc()); String endDate = curYMD + " 23:59:59"; return endDate; }
}