package com.ycl.utils.common; import com.ycl.api.CommonResult; import com.ycl.exception.ApiException; import io.netty.util.internal.StringUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.prefs.BackingStoreException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 时间工具 */ public final class DateUtil { private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class); /** * 日期时分秒 */ public static final String SF = "\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}"; /** * 日期时分 */ public static final String SFM = "\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}"; //ThreadDateUtil /** * yyyy-MM-dd */ public static final String sf1 = "yyyy-MM-dd"; // new SimpleDateFormat(""); /** * yyyy-MM-dd HH:mm */ public static final String sfm = "yyyy-MM-dd HH:mm"; //new SimpleDateFormat(""); /** * yyyy-MM-dd HH:mm:ss */ public static final String sf = "yyyy-MM-dd HH:mm:ss"; /** * yyyyMMdd 用于带日期的SN生成 */ public static final String sfPreSn = "yyyyMMdd"; /** * HH:mm:ss */ public static final String sfH = "HH:mm:ss"; /** * HH:mm */ public static final String sf2 = "HH:mm"; public DateUtil() { } public static String getNow() { return ThreadDateUtil.getDateFormat(sf).format(new Date()); } public static String getNowDate() { return ThreadDateUtil.getDateFormat(sf1).format(new Date()); } public static String getNowDateForPreSn() { return ThreadDateUtil.getDateFormat(sfPreSn).format(new Date()); } /** * 根据时间类型比较时间大小 * * @param source * @param traget * @param "YYYY-MM-DD" "yyyyMMdd HH:mm:ss" 类型可自定义 * @param * @return 0 :source和traget时间相同 * 1 :source比traget时间大 * -1:source比traget时间小 * @throws Exception */ public static int DateCompare(long source, long traget) throws Exception { if (source == traget) return 0; else if (source > traget) { return 1; } else { return -1; } } /** * 方法名: getTime * 方法描述: 获取当前时间时分 * 修改时间:2016年12月2日 上午10:12:46 * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getTime(long time) { return ThreadDateUtil.getDateFormat("HH:mm").format(time); } /** * 方法名: getTime * 方法描述: 获取当前时间时分 * 修改时间:2016年12月2日 上午10:12:46 * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getTime() { return ThreadDateUtil.getDateFormat("HH:mm").format(getNowTime()); } /** * 方法名: getEndTime * 方法描述: (这里用一句话描述这个方法的作用) * 修改时间:2016年11月24日 下午9:17:18 * 参数 @param date * 参数 @return 参数说明 * 返回类型 Date 返回类型 */ public static Date getEndTime(Date date) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } /** * 方法名: getDate * 方法描述: 将long型的时间 字符串 转换为yyyy-MM-dd格式的日期字符串 * 修改时间:2017年6月19日 下午3:24:45 * 参数 @param str * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getDate(String str) { Date d = new Date(parseLong(str)); return ThreadDateUtil.getDateFormat(sf1).format(d); } /** * 将字符串类型转换为整型 * * @param s * @return */ public static Long parseLong(String s) { if (s != null && s.trim().matches("^(-)?[0-9]+$")) return Long.parseLong(s.trim()); return 0L; } public static Date getSfDate(String str) { Date date; try { if (str.matches(SF)) { date = ThreadDateUtil.getDateFormat(sf).parse(str); } else if (str.matches(SFM)) { date = ThreadDateUtil.getDateFormat(sfm).parse(str); } else date = ThreadDateUtil.getDateFormat(sf1).parse(str); return date; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 方法名: getDate * 方法描述: long 转date * 修改时间:2017年6月13日 上午10:50:34 * 参数 @param dateTime * 参数 @return 参数说明 * 返回类型 Date 返回类型 */ public static Date getDate(long dateTime) { String date = getDateTime(dateTime); Date d = null; try { d = ThreadDateUtil.getDateFormat(sf).parse(date); return d; } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 方法名: getDateByLong * 方法描述: long 转成String YYYY-MM-dd * 修改时间:2017年6月13日 上午11:41:43 * 参数 @param dateTime * 参数 @return 参数说明 * 返回类型 String 返回类型 * * @return */ public static String getDateByLong(long dateTime) { Date date = getDate(dateTime); return ThreadDateUtil.getDateFormat(sf1).format(date); } public static String getDateTime(Long date) { if (null == date) { return ""; } try { Date d = new Date(date); return ThreadDateUtil.getDateFormat(sf).format(d); } catch (Exception e) { LOGGER.error(" getTime E: {}", date, e); } return ""; } public static String getTime(Long date) { if (null == date) { return ""; } try { Date d = new Date(date); return ThreadDateUtil.getDateFormat(sf).format(d); } catch (Exception e) { LOGGER.error(" getTime E: {}", date, e); } return ""; } /** * 方法名: getStartTime * 方法描述: 获取当前0点 * 修改时间:2017年5月8日 下午5:03:49 * 参数 @return 参数说明 * 返回类型 Long 返回类型 */ public static long getStartTime() { Calendar todayStart = Calendar.getInstance(); todayStart.set(Calendar.HOUR_OF_DAY, 0); todayStart.set(Calendar.MINUTE, 0); todayStart.set(Calendar.SECOND, 0); todayStart.set(Calendar.MILLISECOND, 0); return todayStart.getTime().getTime(); } /** * 方法名: getEndTime * 方法描述: 获取当天结束时间 * 修改时间:2017年5月8日 下午5:04:12 * 参数 @return 参数说明 * 返回类型 long 返回类型 */ public static long getEndTime() { Calendar todayEnd = Calendar.getInstance(); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime().getTime(); } /** * 获取某天最大时间 * * @param date * @return */ public static Date getEndOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); ; LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX); return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()); } /** * 获取某天最小时间 * * @param date * @return */ public static Date getStartOfDay(Date date) { LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN); return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()); } /** * 方法名: getStartTime * 方法描述: 获取当前0点 * 修改时间:2017年5月8日 下午5:03:49 * 参数 @return 参数说明 * 返回类型 Long 返回类型 * * @throws ParseException */ public static long getStartTime(String dateStr) throws ParseException { if (StringUtils.isBlank(dateStr)) { return 0; } else { Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr); 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().getTime(); } } /** * 方法名: getEndTime * 方法描述: 获取当天结束时间 * 修改时间:2017年5月8日 下午5:04:12 * 参数 @return 参数说明 * 返回类型 long 返回类型 * * @throws ParseException */ public static long getEndTime(String dateStr) throws ParseException { if (StringUtils.isBlank(dateStr)) { return 0; } else { Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr); Calendar todayEnd = Calendar.getInstance(); todayEnd.setTime(date); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime().getTime(); } } /** * 获取 指定时间毫秒数的0点 * * @param time * @return */ public static long getStartTime(long time) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); 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().getTime(); } /** * 获取 指定时间毫秒数的23点59分59秒999毫秒 * * @param time * @return */ public static long getEndTime(long time) { Calendar todayEnd = Calendar.getInstance(); todayEnd.setTimeInMillis(time); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime().getTime(); } public static String getDatem(Long date) { try { Date d = new Date(date); return ThreadDateUtil.getDateFormat(sfm).format(d); } catch (Exception e) { LOGGER.error(" getDatem E: {}", date, e); } return ""; } public static Long getNowTime() { return System.currentTimeMillis(); } /** * 方法名: getTodayEndTime * 方法描述: 获取当天结束时间 * 修改时间:2016年11月2日 下午6:53:23 * 参数 @return 参数说明 * 返回类型 Long 返回类型 */ public static int getDiffTodayEndTime() { return (int) ((getTodayEndTime() - getNowTime()) / 1000); } /** * 方法名: getStartDateTime * 方法描述: 获取一天开始time * 修改时间:2016年12月5日 上午11:57:01 * 参数 @param time * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getStartDateTime(long time) { Date d = new Date(time); return ThreadDateUtil.getDateFormat(sf).format(d); } /** * 方法名: getTodayEndTime * 方法描述: 获取当天时间差 * 修改时间:2016年11月2日 下午6:53:23 * 参数 @return 参数说明 * 返回类型 Long 返回类型 */ public static Long getTodayEndTime() { Calendar todayEnd = Calendar.getInstance(); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime().getTime(); } /** * 获取参数时间的开始时间 * * @param time * @return */ public static Date getBeginTimeByDate(long time) { Date d = new Date(time); Calendar todayBegin = Calendar.getInstance(); todayBegin.setTime(d); todayBegin.set(Calendar.HOUR_OF_DAY, 00); todayBegin.set(Calendar.MINUTE, 00); todayBegin.set(Calendar.SECOND, 00); todayBegin.set(Calendar.MILLISECOND, 000); return todayBegin.getTime(); } /** * 获取参数时间的结束时间 * * @param time * @return */ public static Date getEndTimeByDate(long time) { Date d = new Date(time); Calendar todayBegin = Calendar.getInstance(); todayBegin.setTime(d); todayBegin.set(Calendar.HOUR_OF_DAY, 23); todayBegin.set(Calendar.MINUTE, 59); todayBegin.set(Calendar.SECOND, 59); todayBegin.set(Calendar.MILLISECOND, 999); return todayBegin.getTime(); } /** * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致 * * @param nowTime 当前时间 * @param startTime 开始时间 * @param endTime 结束时间 * @return */ public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) { if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) { return true; } Calendar date = Calendar.getInstance(); date.setTime(nowTime); Calendar begin = Calendar.getInstance(); begin.setTime(startTime); Calendar end = Calendar.getInstance(); end.setTime(endTime); if (date.after(begin) && date.before(end)) { return true; } else { return false; } } public static Long getLongTime(String str) { if (StringUtils.isBlank(str)) { return getNowTime(); } Date date = null; try { if (str.matches(SF)) { date = ThreadDateUtil.getDateFormat(sf).parse(str); } else if (str.matches(SFM)) { date = ThreadDateUtil.getDateFormat(sfm).parse(str); } else { date = ThreadDateUtil.getDateFormat(sf1).parse(str); } } catch (ParseException e) { LOGGER.error(" getLongTime E: {}", str, e); } if (null == date) { return 0L; } return date.getTime(); } /** * 把时间参数 转换为 long * * @param dateStr * @return * @throws BackingStoreException */ public static long getTime(String dateStr) throws ApiException { try { Date date = ThreadDateUtil.getDateFormat(sf).parse(dateStr); return date.getTime(); } catch (ParseException e) { LOGGER.error(" getTime E: {}", dateStr, e); throw new ApiException(" time parese error: " + dateStr); } } /** * 把时间参数 转换为 long * * @param dateStr yyyy-MM-dd * @return * @throws BackingStoreException */ public static long getTimeByStringDate(String dateStr) throws ApiException { try { Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr); return date.getTime(); } catch (ParseException e) { LOGGER.error(" getTime E: {}", dateStr, e); throw new ApiException(" time parese error: " + dateStr); } } /** * 方法名: getNowYear * 方法描述: 获取当前年份 * 修改时间:2016年6月22日 下午6:52:24 * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getNowYear() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Date date = new Date(); return sdf.format(date); } /** * 方法名: getNowMonth * 方法描述: 获取当前年月yyyy/MM * 修改时间:2016年6月22日 下午6:52:24 * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getNowMonth() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM"); Date date = new Date(); return sdf.format(date); } /** * 方法名: getNowMonthDaySn * 方法描述: 获取当前年月yyyyMMdd * 修改时间:2017年6月28日 上午10:52:24 * 参数 @return 参数说明 * 返回类型 String 返回类型 */ public static String getNowMonthDay() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(); return sdf.format(date); } /** * 获取明天的日期字符串 * * @return */ public static String tomorrowDateStr(int day) { Date date = new Date();//取时间 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //把日期往后增加一天.整数往后推,负数往前移动 calendar.add(calendar.DATE, day); //这个时间就是日期往后推一天的结果 date = calendar.getTime(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String tomorrowStr = formatter.format(date); return tomorrowStr; } /** * dq * 获取指定年月yyyyMMddHHmmss * * @param dateTime * @return */ public static String getNowMonthDay(long dateTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(dateTime); return sdf.format(date); } public static String getAge(String birthDate) { if (null == birthDate) { throw new RuntimeException("出生日期不能为null"); } int age = 0; String[] str = birthDate.split("-"); if (str.length == 3) { Date now = new Date(); SimpleDateFormat format_y = new SimpleDateFormat("yyyy"); SimpleDateFormat format_M = new SimpleDateFormat("MM"); String birth_year = str[0]; String this_year = format_y.format(now); String birth_month = str[1]; String this_month = format_M.format(now); // 初步,估算 age = Integer.parseInt(this_year) - Integer.parseInt(birth_year); // 如果未到出生月份,则age - 1 if (this_month.compareTo(birth_month) < 0) { age -= 1; } if (age < 0) { age = 0; } return age + "." + (parseInt(this_month) - parseInt(this_month)); } return "0"; } /** * 将字符串类型转换为整型 * * @param s * @return */ public static Integer parseInt(String s) { if (s != null && s.trim().matches("^(-)?[0-9]+$")) return Integer.parseInt(s.trim()); return 0; } /** * 查询前一天 * * @param date * @return */ public static String getPreDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -1); date = calendar.getTime(); return ThreadDateUtil.getDateFormat(sf1).format(date); } /** * 方法名: isBirthday * 方法描述: 判断输入的字符串是否是合法的生日 生日不能大于当前日期 * 修改时间:2017年3月22日 上午9:16:09 * 参数 @param birthday * 参数 @return 参数说明 * 返回类型 boolean 返回类型 */ public static boolean isBirthday(String birthday) { if (StringUtils.isBlank(birthday)) { return false; } if (birthday.length() != 10) { return false; } Pattern pattern = Pattern .compile("^[1,2]\\d{3}(-){1}(0[1-9]||1[0-2])(-){1}(0[1-9]||[1,2][0-9]||3[0,1])$"); Matcher matcher = pattern.matcher(birthday); if (!matcher.matches()) { return false; } Date birth = null; try { birth = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); } catch (ParseException e) { e.printStackTrace(); } if (!new SimpleDateFormat("yyyy-MM-dd").format(birth).equals(birthday)) { return false; } // 获取当前日期的毫秒数 long currentTime = System.currentTimeMillis(); // 获取生日的毫秒数 long birthTime = birth.getTime(); // 如果当前时间小于生日,生日不合法。反之合法 if (birthTime > currentTime) { return false; } return true; } /** * 方法名: isAfterHour * 方法描述: 当前时间7点之后可以显示 * 修改时间:2017年5月11日 下午3:39:34 * 参数 @return 参数说明 * 返回类型 boolean 返回类型 */ public static boolean isAfterHour() { Calendar todayStart = Calendar.getInstance(); //用d.getHour()可以获取当前小时数。 int hour = todayStart.get(Calendar.HOUR_OF_DAY); return hour >= 7; } /** * wye * 获取某天开始时间0:0:0:0 * * @param date * @param addDay * @return */ public static long getStartTime(Date date, int addDay) { Calendar dayTimeStart = Calendar.getInstance(); dayTimeStart.setTime(date); if (addDay != 0) { dayTimeStart.add(Calendar.DATE, addDay); } dayTimeStart.set(Calendar.HOUR_OF_DAY, 0); dayTimeStart.set(Calendar.MINUTE, 0); dayTimeStart.set(Calendar.SECOND, 0); dayTimeStart.set(Calendar.MILLISECOND, 0); return dayTimeStart.getTime().getTime(); } /** * wye * 获取某天结束时间23:59:59:999 * * @param date * @param addDay * @return */ public static long getEndTime(Date date, int addDay) { Calendar dayTimeEnd = Calendar.getInstance(); dayTimeEnd.setTime(date); if (addDay != 0) { dayTimeEnd.add(Calendar.DATE, addDay); } dayTimeEnd.set(Calendar.HOUR_OF_DAY, 23); dayTimeEnd.set(Calendar.MINUTE, 59); dayTimeEnd.set(Calendar.SECOND, 59); dayTimeEnd.set(Calendar.MILLISECOND, 999); return dayTimeEnd.getTime().getTime(); } /** * wye * 获取当前时间 之后或之前的几天 * * @param * @return * @throws ParseException */ public static long getLongTimeByDay(long dateTime, int day) { Calendar start = Calendar.getInstance(); start.setTimeInMillis(dateTime); start.add(Calendar.DAY_OF_MONTH, day); return start.getTime().getTime(); } /** * 日期格式化字符串 * 默认格式 yyyy-MM-dd HH:mm:ss */ public static String format(Date date) { if (date == null) { return null; } return format(date, DatePattern.YYYY_MM_DD_HHmmss); } /** * 日期格式化字符串 */ public static String format(Date date, DatePattern datePattern) { if (date == null || datePattern == null) { return null; } return format(date, datePattern.pattern); } /** * 日期格式化字符串 * * @param date 日期对象 * @param pattern 日期格式 * @return */ public static String format(Date date, String pattern) { if (date == null) { return null; } return ThreadDateUtil.getDateFormat(pattern).format(date); } /** * 日期字符串 转换为日期 * 默认格式 yyyy-MM-dd HH:mm:ss * * @throws ParseException */ public static Date parse(String source) throws ParseException { return parse(source, DatePattern.YYYY_MM_DD_HHmmss); } /** * 日期格式化字符串 * * @param source 时间字符串 * @param datePattern 日期格式 * @return * @throws ParseException */ public static Date parse(String source, DatePattern datePattern) throws ParseException { return parse(source, datePattern.pattern); } /** * 日期字符串 转换为日期 * * @param source 时间字符串 * @param pattern 日期格式 * @return * @throws ParseException */ public static Date parse(String source, String pattern) throws ParseException { return ThreadDateUtil.getDateFormat(pattern).parse(source); } /** * 日期字符串 转换为日期毫秒数 * 默认格式 yyyy-MM-dd HH:mm:ss * * @throws ParseException */ public static Long parseToLong(String source) throws ParseException { return parse(source, DatePattern.YYYY_MM_DD_HHmmss).getTime(); } /** * 日期字符串 转换为日期毫秒数 * * @param source 时间字符串 * @param datePattern 日期格式 * @return * @throws ParseException */ public static Long parseToLong(String source, DatePattern datePattern) throws ParseException { return parse(source, datePattern.pattern).getTime(); } /** * 日期字符串 转换为日期毫秒数 * * @param source 时间字符串 * @param pattern 日期格式 * @return * @throws ParseException */ public static Long parseToLong(String source, String pattern) throws ParseException { return ThreadDateUtil.getDateFormat(pattern).parse(source).getTime(); } /** * 获取日期年份 * * @param date 日期 * @return */ public static String getYear(Date date) { return format(date).substring(0, 4); } /** * 功能描述:返回月 * * @param date Date 日期 * @return 返回月份 */ public static int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; } /** * 功能描述:返回日 * * @param date Date 日期 * @return 返回日份 */ public static int getDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 功能描述:返回小 * * @param date 日期 * @return 返回小时 */ public static int getHour(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 功能描述:返回分 * * @param date 日期 * @return 返回分钟 */ public static int getMinute(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /** * 返回秒钟 * * @param date Date 日期 * @return 返回秒钟 */ public static int getSecond(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.SECOND); } /** * 功能描述:返回毫 * * @param date 日期 * @return 返回毫 */ public static long getMillis(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getTimeInMillis(); } /** * 验证请求是否在合法时间内 * * @param timestamp * @return */ public static boolean verifyRequestTime(long timestamp) { long now = System.currentTimeMillis(); long time = now - timestamp; // System.out.println(now); // System.out.println(time); // System.out.println(time / 1000); if (time / 1000 > 60) { return false; } return true; } public static String formatDate(String format) { return formatDate(new Date(), format); } public static String formatDate(Date date, String format) { if ((null == date) || (StringUtils.isEmpty(format))) { return null; } return new SimpleDateFormat(format).format(date); } public static long getYesterDay() { Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(calendar.DATE, -1); date = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); String dateString = format.format(date); long time = 0; try { time = format.parse(dateString).getTime(); } catch (ParseException e) { e.printStackTrace(); } return time; } public static long get1MonthByNow() { Calendar c = Calendar.getInstance(); //过去一月 c.setTime(new Date()); c.add(Calendar.MONTH, -1); Date m = c.getTime(); long time = m.getTime(); // String mon = format.format(m); return time; } public static int dateDiff(Date date1, Date date2) { int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)); return days; } public static int dateDiff(Long date1, Long date2) { int days = (int) ((date2 - date1) / (1000 * 3600 * 24)); return days; } public static List te() { List list = new LinkedList<>(); Calendar calendar = Calendar.getInstance(); int month = calendar.get(Calendar.MONTH) + 1; int year = calendar.get(Calendar.YEAR); for (int i = 1; i <= 12; i++) { if (month == 1) { list.add(year + "-" + month); break; } list.add(year + "-" + month); month--; } return list; } /** * 获取上个月的第一天 * * @return */ public static long getLastMonthStart() { //获取前月的第一天 Calendar start = Calendar.getInstance(); //获取当前日期 start.add(Calendar.MONTH, -1); //设置为1号,当前日期既为本月第一天 start.set(Calendar.DAY_OF_MONTH, 1); return DateUtil.getStartTime(start.getTimeInMillis()); } /** * 获取本月第一天 * * @return */ public static long getCurrentMonthStart() { //获取前月的第一天 Calendar start = Calendar.getInstance(); //设置为1号,当前日期既为本月第一天 start.set(Calendar.DAY_OF_MONTH, 1); return DateUtil.getStartTime(start.getTimeInMillis()); } /** * 获取月份开始时间 * * @param date * @return */ public static long getMonthStart(Date date) { //获取前月的第一天 Calendar start = Calendar.getInstance(); start.setTime(date); //设置为1号,当前日期既为本月第一天 start.set(Calendar.DAY_OF_MONTH, 1); return DateUtil.getStartTime(start.getTimeInMillis()); } /** * 获取月份结束时间 * * @param date * @return */ public static long getMonthEnd(Date date) { Calendar end = Calendar.getInstance(); end.setTime(date); end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH)); return DateUtil.getEndTime(end.getTimeInMillis()); } /** * 获取上个月的最后一天 * * @return */ public static long getLastMonthEnd() { Calendar cale = Calendar.getInstance(); cale.set(Calendar.DAY_OF_MONTH, 0); return DateUtil.getEndTime(cale.getTimeInMillis()); } /** * 上个月 * * @return */ public static String getLastMonth() { SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 设置为当前时间 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月 date = calendar.getTime(); String accDate = format.format(date); return accDate; } /** * 前2个月 * * @return */ public static String getLast2Month() { SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 设置为当前时间 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 2); // 设置为上一个月 date = calendar.getTime(); String accDate = format.format(date); return accDate; } public static String getYearMoney(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); String format1 = format.format(date); return format1; } /** * 获取精确到秒的时间戳 * * @param date * @return */ public static int getSecondTimestamp(Date date) { if (null == date) { return 0; } String timestamp = String.valueOf(date.getTime() / 1000); return Integer.valueOf(timestamp); } /** * 获取门店营业时间 * * @param storeLocalTime 门店时间 * @param date * @return */ public static Date getStoreDate(Long storeLocalTime, Date date) { Date storeDate = getDate(storeLocalTime); String format = ThreadDateUtil.getDateFormat(sfH).format(storeDate); String dateStr = ThreadDateUtil.getDateFormat(sf1).format(date); // Date userDate = getDate(receiveTime); // String nowDate = ThreadDateUtil.getDateFormat(sf1).format(userDate); String concat = dateStr.concat(" ").concat(format); Date newDate = null; try { newDate = ThreadDateUtil.getDateFormat(sf).parse(concat); } catch (ParseException e) { e.printStackTrace(); } return newDate; } /** * 获取指定年月的第一天 * * @param year * @param month * @return */ public static String getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month - 1); //获取某月最小天数 int firstDay = cal.getMinimum(Calendar.DATE); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } /** * 获取指定年月的最后一天 * * @param year * @param month * @return */ public static String getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month - 1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DATE); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); return sdf.format(cal.getTime()); } /** * 计算时间相差多少天 * * @param startDate * @param endDate * @return * @throws ParseException */ public static int getDayDiffer(Date startDate, Date endDate) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime(); long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime(); return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24)); } /** * 计算时间差 * * @param startDate * @param endDate * @return */ public static int getOrderDayDiffer(Date startDate, Date endDate) { int dayDiffer = 0; try { dayDiffer = getDayDiffer(startDate, endDate); if (dayDiffer < 0) { throw new ApiException(CommonResult.failed().getMessage()); } if (dayDiffer == 0) { dayDiffer = 1; } } catch (ParseException e) { e.printStackTrace(); throw new ApiException(e); } return dayDiffer; } /** * 获取时分 * * @param date * @return */ public static String getHm(Date date) { DateFormat dateFormat = ThreadDateUtil.getDateFormat(sf2); return dateFormat.format(date); } /** * 获取年月日 * * @param date * @return */ public static String getYmd(Date date) { DateFormat dateFormat = ThreadDateUtil.getDateFormat(sf1); return dateFormat.format(date); } public static long swapYmd(Long localTime, Long receiveTime) { Date localDate = DateUtil.getDate(localTime); String hms = ThreadDateUtil.getDateFormat(sfH).format(localDate); Date receiveDate = DateUtil.getDate(receiveTime); String ymd = ThreadDateUtil.getDateFormat(sf1).format(receiveDate); String concat = ymd.concat(" ").concat(hms); Date newDate = null; try { newDate = ThreadDateUtil.getDateFormat(sf).parse(concat); } catch (ParseException e) { e.printStackTrace(); } return newDate.getTime(); } /** *     * 获取指定日期所在周的周一 *

*     * *

*     * @param date      日期 *

*     */ public static Date getFirstDayOfWeek(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); if (c.get(Calendar.DAY_OF_WEEK) == 1) { c.add(Calendar.DAY_OF_MONTH, -1); } c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1); return c.getTime(); } /** *     * 获取指定日期所在周的周日 *

*     * *

*     * @param date      日期 *

*     */ public static Date getLastDayOfWeek(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date);// 如果是周日直接返回 if (c.get(Calendar.DAY_OF_WEEK) == 1) { return date; } // System.out.println(c.get(Calendar.DAY_OF_WEEK)); c.add(Calendar.DATE, 7 - c.get(Calendar.DAY_OF_WEEK) + 1); return c.getTime(); } }