package com.panchan.tsmese.utils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.panchan.tsmese.entity.ErrorCodeEnum; import com.panchan.tsmese.exception.TsmEseException; /** * @Description: 时间工具类 * @version 1.0 * @since JDK1.8 * @author * @Created on 2018年8月29日 */ public class DateUtils { public static final String DATE_FORMAT_DATE = "yyyy-MM-dd"; public static final String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_FORMAT_TIME = "HH:mm:ss"; public static final String DATE_FORMAT_DATEMILTIME = "yyyy-MM-dd HH:mm:ss.SSS"; public static final String DATE_FORMAT_DATEMALTIME = "yyyyMMddHHmmss"; public static final String DATE_FORMAT_DATEALLTIME = "yyyyMMddHHmmssSSS"; /** * 当前时间转long毫秒 * @param dateFormat 转字符串格式 * @return */ public static String getCurrentDateStr(String dateFormat) { Calendar c = Calendar.getInstance(); DateFormat df = new SimpleDateFormat(dateFormat); return df.format(c.getTime()); } /** * 时间字符串转时间格式 * @param dateStr 时间字符串 * @param dateFormat 转字符串格式 * @return */ public static Date getDateFromStr(String dateStr, String dateFormat) { try { DateFormat df = new SimpleDateFormat(dateFormat); return df.parse(dateStr); } catch (ParseException e) { throw new TsmEseException(ErrorCodeEnum.SYSTEM_ERROR.getErrorCode(), e); } } /** * * @param date 时间 * @param dateFormat 转字符串格式 * @return */ public static String getFormatedDateStr(Date date, String dateFormat){ DateFormat df = new SimpleDateFormat(dateFormat); return df.format(date); } /** * 当前时间转字符串 * @param format * @return */ public static String getNowTime(String format) { SimpleDateFormat df = new SimpleDateFormat(format); return df.format(new Date()); } /** * getCurrentDate 获取时间,具体到日 * @return CurrentDate */ public static String getCurrentDate(){ Date result = new Date(); Long timeLong = System.currentTimeMillis(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); result.setTime(timeLong); return dateFormat.format(result); } /** * getCurrentDateTime 获取时间到秒 * @return CurrentDateTime */ public static String getCurrentDateTime(){ Date result = new Date(); Long timeLong = System.currentTimeMillis(); DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); result.setTime(timeLong); return format.format(result); } /** * @return * @Description:判断两个时间是否超过24小时 * @param * @return void * @throws */ public static boolean calculatTtime(String createTime, String updateTime) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date start = sdf.parse(createTime); Date end = sdf.parse(updateTime); long cha = end.getTime() - start.getTime(); double result = cha * 1.0 / (1000 * 60 * 60); if(result <= 24){ return false; //说明小于24小时 }else{ return true; } } }