package cn.com.qmth.examcloud.commons.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException; /** * 日期工具 * * @author WANGWEI */ public class DateUtil { /** * patterns describing the date and time format * * @author WANGWEI */ public interface DatePatterns { public static final String DEFAULT = "yyyyMMddHHmmss"; public static final String YYYY = "yyyy"; public static final String YYYYMM = "yyyyMM"; public static final String YYYYMMDD = "yyyyMMdd"; public static final String YYYYMMDDHH = "yyyyMMddHH"; public static final String YYYYMMDDHHMM = "yyyyMMddHHmm"; public static final String CHINA_DEFAULT = "yyyy-MM-dd HH:mm:ss"; public static final String YYYY_MM = "yyyy-MM"; public static final String YYYY_MM_DD = "yyyy-MM-dd"; public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS"; } /** * get now date. * * @param pattern * @return */ public static String now(String pattern) { return format(new Date(), pattern); } /** * get now china date. * * @return */ public static String chinaNow() { return format(new Date(), DatePatterns.CHINA_DEFAULT); } /** * format date. * * @param date * @param pattern * @return */ public static String format(Date date, String pattern) { try { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } catch (Exception e) { throw new ExamCloudRuntimeException(e); } } /** * format now date. * * @author WANGWEI * @param pattern * @return */ public static String formatNow(String pattern) { try { SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(new Date()); } catch (Exception e) { throw new ExamCloudRuntimeException(e); } } /** * parse date. * * @param source * @param pattern * @return */ public static Date parse(String source, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { return df.parse(source); } catch (ParseException e) { throw new ExamCloudRuntimeException(e); } } /** * 是否同一天 * * @author WANGWEI * @param date1 * @param date2 * @return */ public static boolean isSameDay(Date date1, Date date2) { if (null == date1) { throw new ExamCloudRuntimeException("first argument must not be null"); } if (null == date2) { throw new ExamCloudRuntimeException("second argument must not be null"); } Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return isSameDay(cal1, cal2); } /** * 是否同一天 * * @author WANGWEI * @param calendar1 * @param calendar2 * @return */ public static boolean isSameDay(Calendar calendar1, Calendar calendar2) { if (null == calendar1) { throw new ExamCloudRuntimeException("first argument must not be null"); } if (null == calendar2) { throw new ExamCloudRuntimeException("second argument must not be null"); } return calendar1.get(0) == calendar2.get(0) && calendar1.get(1) == calendar2.get(1) && calendar1.get(6) == calendar2.get(6); } }