package com.iden.common.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * @description 时间控件 * @author make Java */ public class DateUtils { private static final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss"); private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); private static final SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM"); private static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd"); /** * 一天毫秒数 */ private static final long ONE_DAY = 1000 *60* 60 * 24L; private DateUtils(){} /** * 俩个时间间隔天数 * @param beginDate * @param endDate * @return */ public static int getDaysGap(Date beginDate, Date endDate) { long beginTimeMillis = beginDate.getTime(); long endTimeMills = endDate.getTime(); long time = endTimeMills - beginTimeMillis; double day = (time *1.0D / ONE_DAY); return (int)Math.floor(day); } /**    * 俩个时间间隔天数字符串的日期格式的计算    */ public static int daysBetween(String smdate, String bdate) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * date日期过num天的日期 * @param date * @param num * @return */ public static Date dateGapDays(Date date, int num) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(5, num); return cal.getTime(); } /** * 字符串格式日期转日期格式日期 * @param date * @param pattern * @return */ public static Date strToDate(String date,String pattern){ if(isEmpty(date) || isEmpty(pattern)){ return null ; } SimpleDateFormat format = new SimpleDateFormat(pattern); try { return format.parse(date) ; } catch (ParseException e) { e.printStackTrace(); return null ; } } /** * 日期转日期格式日期 * @param date * @param pattern * @return */ public static String formatToDateStr(Date date,String pattern) { if(date==null || isEmpty(pattern)){ return null ; }else{ SimpleDateFormat sd = new SimpleDateFormat(pattern); return sd.format(date); } } /** * 检查字符串 * @param str * @return */ private static boolean isEmpty(String str) { return null == str ? true : "".equals(str.replaceAll(" +", "")); } /** * 日期的间隔分钟 * * @param startDate * @param endDate * @return */ public static Long betweenDate(Date startDate, Date endDate) { return betweenDateSSS(startDate, endDate) / 1000/60; } /** * 日期的间隔毫秒数 * * @param startDate * @param endDate * @return */ public static long betweenDateSSS(Date startDate, Date endDate) { return null == startDate || null == endDate ? 0 : endDate.getTime() - startDate.getTime(); } /** * 俩时间段之间的间隔秒数 * * @param startTime(HH:mm:ss) * @param enTime(HH:mm:ss) * @return */ public static long betweenTimeSS(String startTime, String enTime) { String dateStr = getCurrYyyy_MM_ddDate(); String start = dateStr + " " + startTime; String end = dateStr + " " + enTime; Date startDate = strToDate(start,"yyyy-MM-dd HH:mm:ss"); Date endDate = strToDate(end,"yyyy-MM-dd HH:mm:ss"); return (endDate.getTime() - startDate.getTime()) /1000; } /** * 判断一个字符串是不是一个合法的日期格式 * * @param str * @param pattern * @return */ public static boolean isValidDate(String str,String pattern) throws ParseException{ SimpleDateFormat format = new SimpleDateFormat(pattern); try { format.setLenient(false); format.parse(str); } catch (ParseException e) { throw new java.text.ParseException("不是一个合法的日期格式",0); } return true; } /** * 根据毫秒数获取时间字符串 * yyyy-MM-dd HH:mm:ss * @param millisecondTime * @return */ public static String formatMillisecondTime(String millisecondTime){ String time = String.valueOf(Long.valueOf(millisecondTime)); return yyyy_MM_dd_HH_mm_ss.format(new Date(Long.valueOf(time))); } /** * 获取当时间并格式化为:yyyyMMddHHmmss格式 * * @return 返回yyyyMMddHHmmss格式的时间字符串 * @throws Exception */ public static String getCurrYyyyMMddHHmmssDate() { return yyyyMMddHHmmss.format(new Date()); } /** * 获取当时间并格式化为:yyyyMMdd格式 * * @return 返回yyyyMMdd格式的时间字符串 * @throws Exception */ public static String getCurrYyyyMMddDate() { return yyyyMMdd.format(new Date()); } /** * 获取昨天并格式化为:yyyyMMdd格式 * * @return 返回yyyyMMdd格式的时间字符串 * @throws Exception */ public static String getYestodayYyyyMMddDate() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE,-1); String yesterday = yyyyMMdd.format(cal.getTime()); return yesterday; } /** * 把字符串时间段转成日期时间 * @param startTime * @param endTime * @return */ public static Map convertDate(String startTime, String endTime) { Map map = new HashMap<>(); try { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); Calendar start = Calendar.getInstance(); start.setTime(sdf.parse(startTime)); Calendar end = Calendar.getInstance(); end.setTime(sdf.parse(endTime)); if (start.after(end)) { //跨天了 end.add(Calendar.DATE,1); } map.put("startDate", start.getTime()); map.put("endDate", end.getTime()); } catch (Exception e) { e.printStackTrace(); } return map; } /** * 获取num天前date * * @return 返回yyyyMMdd格式的时间字符串 * @throws Exception */ public static Date getBeforeDates(int num) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE,-num); cal.getTime(); return cal.getTime(); } /** * 获取当时间并格式化为:yyyy-MM-dd格式 * * @return 返回yyyy-MM-dd格式的时间字符串 * @throws Exception */ public static String getCurrYyyy_MM_ddDate() { return yyyy_MM_dd.format(new Date()); } /** * 计算时间格式为:yyyy-MM-dd HH:mm:ss的时间差值 * * @param startTime 开始时间 * @param endTime 结束时间 * @return 返回时间,单位秒 * @throws Exception */ public static long compute_yyyy_MM_ddHHmmssTime(String startTime, String endTime) throws Exception { Date startDate = yyyy_MM_dd_HH_mm_ss.parse(startTime); Date endDate = yyyy_MM_dd_HH_mm_ss.parse(endTime); return (endDate.getTime() - startDate.getTime()) / 1000L; } /** * 获取两个日期之间的所有日期(yyyyMMdd,yyyyMMdd) * @Description TODO * @param begin * @param end * @return * @author XuJD * @date 2017-4-1 */ public static List getBetweenDates(String begin, String end) { List result = new ArrayList<>(); try { Date beginDate = yyyyMMdd.parse(begin); Date endDate = yyyyMMdd.parse(end); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(beginDate); while (beginDate.getTime() <= endDate.getTime()) { result.add(yyyyMMdd.format(tempStart.getTime())); tempStart.add(Calendar.DAY_OF_YEAR, 1); beginDate = tempStart.getTime(); } } catch (Exception e){ e.printStackTrace(); } return result; } public static void main(String[] args) { Map map = convertDate("20:12:00","23:00:00"); System.out.println(map); } }