package com.kmall.common.utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.Logger; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; /** * 日期处理 * * @author Scott * @email * @date 2016年12月21日 下午12:53:33 */ public class DateUtils { // 日志 private static final Log logger = LogFactory.getLog(DateUtils.class); /** * 时间格式(yyyy-MM-dd) */ public final static String DATE_PATTERN = "yyyy-MM-dd"; public static String DATE_TIME_PATTERN_YYYY_MM_DD = "yyyyMMdd"; /** * 无分隔符日期格式 "yyyyMMddHHmmss" */ public static String DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss"; /** * 无分隔符日期格式 "yyyyMMddHHmmssSSS" */ public static String DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS_SSS = "yyyyMMddHHmmssSSS"; /** * 不带秒的标准日期格式 "yyyy.MM.dd HH:mm" */ public static String PATTERN_YYYY_MM_DD_HH_MM = "yyyy.MM.dd HH:mm"; /** * 时间格式(yyyy-MM-dd HH:mm:ss) */ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; // 日期转换格式数组 public static String[][] regularExp = new String[][]{ // 默认格式 {"\\d{4}-((([0][1,3-9]|[1][0-2]|[1-9])-([0-2]\\d|[3][0,1]|[1-9]))|((02|2)-(([1-9])|[0-2]\\d)))\\s+([0,1]\\d|[2][0-3]|\\d):([0-5]\\d|\\d):([0-5]\\d|\\d)", DATE_TIME_PATTERN}, // 仅日期格式 年月日 时 分 秒 {"\\d{4}.((([0][1,3-9]|[1][0-2]|[1-9]).([0-2]\\d|[3][0,1]|[1-9]))|((02|2).(([1-9])|[0-2]\\d)))\\s+([0,1]\\d|[2][0-3]|\\d):([0-5]\\d|\\d)", PATTERN_YYYY_MM_DD_HH_MM}, // 仅日期格式 年月日 {"\\d{4}-((([0][1,3-9]|[1][0-2]|[1-9])-([0-2]\\d|[3][0,1]|[1-9]))|((02|2)-(([1-9])|[0-2]\\d)))", DATE_PATTERN}, // 带毫秒格式 {"\\d{4}((([0][1,3-9]|[1][0-2]|[1-9])([0-2]\\d|[3][0,1]|[1-9]))|((02|2)(([1-9])|[0-2]\\d)))([0,1]\\d|[2][0-3])([0-5]\\d|\\d)([0-5]\\d|\\d)\\d{1,3}", DATE_TIME_PATTERN_YYYY_MM_DD_HH_MM_SS_SSS} }; public static String format(Date date) { return format(date, DATE_PATTERN); } public static String format(Date date, String pattern) { if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; } public static String timeToStr(Long time, String pattern) { SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); if (time.toString().length() < 13) { time = time * 1000L; } Date date = new Date(time); String value = dateFormat.format(date); return value; } public static long strToTime(String timeStr) { Date time = strToDate(timeStr); return time.getTime() / 1000; } /** * 获取过去的分钟 * * @param date * @return */ public static long pastMinutes(Date date) { long t = new Date().getTime() - date.getTime(); return t / (60 * 1000); } /** * 转换为时间类型格式 * * @param strDate 日期 * @return */ public static Date strToDate(String strDate) { try { String strType = getDateFormat(strDate); SimpleDateFormat sf = new SimpleDateFormat(strType); return new Date((sf.parse(strDate).getTime())); } catch (Exception e) { return null; } } /** * 根据传入的日期格式字符串,获取日期的格式 * * @return 秒 */ public static String getDateFormat(String date_str) { String style = null; if (StringUtils.isEmpty(date_str)) { return null; } boolean b = false; for (int i = 0; i < regularExp.length; i++) { b = date_str.matches(regularExp[i][0]); if (b) { style = regularExp[i][1]; } } if (StringUtils.isEmpty(style)) { logger.info("date_str:" + date_str); logger.info("日期格式获取出错,未识别的日期格式"); } return style; } /** * 将字符串类型的转换成Date类型 * * @param dateStr 字符串类型的日期 yyyy-MM-dd * @return Date类型的日期 * @throws ParseException */ public static Date convertStringToDate(String dateStr,String format) { // 返回的日期 Date resultDate = null; try { // 日期格式转换 SimpleDateFormat sdf = new SimpleDateFormat(format); resultDate = sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return resultDate; } /** * 获取月份的开始和结束时间 * * @param time */ public static Date[] getMonthStartEnd(Date time) { Date[] startEndDate = new Date[2]; // 获取当前月第一天 Calendar cal = Calendar.getInstance();// 获取当前日期 cal.setTime(time); cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天 cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); startEndDate[0] = cal.getTime(); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); startEndDate[1] = cal.getTime(); return startEndDate; } public static String getDate(String datdString){ datdString = datdString.replace("GMT", "").replaceAll("\\(.*\\)", ""); //将字符串转化为date类型,格式2016-10-12 SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH); Date dateTrans = null; try { dateTrans = format.parse(datdString); return new SimpleDateFormat("yyyy-MM-dd").format(dateTrans); } catch (ParseException e) { e.printStackTrace(); } return datdString; } /** * true为开始时间小于或等于结束时间,false为开始时间大于结束时间 * @param startDate * @param endDate * @return */ public static Boolean compareDate(Date startDate, Date endDate) { // System.out.println(startDate.compareTo(endDate)); if(startDate.compareTo(endDate) == 0){ return true; } if (startDate.getTime() > endDate.getTime()) { return false; } return true; } public static Date addDay(Date date, Integer day){ Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String currdate = format.format(d); // System.out.println("现在的日期是:" + currdate); Calendar ca = Calendar.getInstance(); ca.setTime(date); ca.add(Calendar.DATE, day);// num为增加的天数,可以改变的 d = ca.getTime(); String enddate = format.format(d); // System.out.println("增加天数以后的日期:" + enddate); return d; } public static String addMin(Date date, Integer min){ SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Calendar ca = Calendar.getInstance(); ca.setTime(date); ca.add(Calendar.MINUTE, min);// min为增加的分钟数,可以改变的 Date d = ca.getTime(); String enddate = format.format(d); return enddate; } public static void main(String[] args) { // Date date = new Date(); // String str = "20170818223629599"; // System.out.println(DateUtils.getDateFormat(str)); // System.out.println(DateUtils.addDay(2)); System.out.println(DateUtils.compareDate(new Date(), new Date())); System.out.println(new BigDecimal("11").compareTo(new BigDecimal("11"))); System.out.println(BigDecimal.valueOf(Double.valueOf("0.112"))); } /** * 获取date的日期和time的时间 组成的新的日期对象 * * @return */ public static Date getDateAddTime(String d1, String d2) { Date allDate = parseToDate(d1 + " " + d2, "yyyy-MM-dd HH:mm:ss"); return allDate; } /** * @desc:String转date * @author Clyde * @date:2014-3-16 * @param strDate * @param format * @return 若失败则返回当前时间 */ public static Date parseToDate(String strDate, String format) { try { if (strDate == null || strDate.trim().length() == 0) { return new Date(); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.parse(strDate); } catch (ParseException e) { logger.error("[ERROR:时间转换失败;]", e); return new Date(); } } /** * @desc:格式化日期 * @author Clyde * @date:2014-2-23 * @param date * @param format * @return */ public static String formatDate(Date date, String format) { String customDate = ""; if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat(format); customDate = sdf.format(date); } return customDate; } public static Date add(int field, Date date, int value) { if (value == 0) { return date; } Calendar c = Calendar.getInstance(); c.setTime(date); int fieldNewValue = c.get(field) + value; c.set(field, fieldNewValue); return c.getTime(); } /** * 加减日 * @param date * @param value * @return */ public static Date addDate(Date date, int value){ return add(Calendar.DATE,date,value); } }