123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package cn.com.qmth.examcloud.commons.util;
- import java.math.BigDecimal;
- 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);
- }
- }
- /**
- * parse date randomly.
- *
- * @author WANGWEI
- * @param s
- * @return
- */
- public static Date parseRandomly(String s) {
- if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
- return parse(s, "yyyy/MM/dd HH:mm:ss");
- } else if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}")) {
- return parse(s, "yyyy/MM/dd HH:mm");
- } else if (s.matches("\\d{4}/\\d{1,2}/\\d{1,2}")) {
- return parse(s, "yyyy/MM/dd");
- } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
- return parse(s, "yyyy-MM-dd HH:mm:ss");
- } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}")) {
- return parse(s, "yyyy-MM-dd HH:mm");
- } else if (s.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
- return parse(s, "yyyy-MM-dd");
- } else {
- throw new ExamCloudRuntimeException("unsupported date string.");
- }
- }
- /**
- * 解析excel日期
- *
- * @author WANGWEI
- * @param number
- * @return
- */
- public static Date parseExcel(String number) {
- BigDecimal bd = new BigDecimal(number);
- int days = bd.intValue();
- int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);
- Calendar c = Calendar.getInstance();
- c.set(1900, 0, 1);
- c.add(Calendar.DATE, days - 2);
- int hour = mills / 3600;
- int minute = (mills - hour * 3600) / 60;
- int second = mills - hour * 3600 - minute * 60;
- c.set(Calendar.HOUR_OF_DAY, hour);
- c.set(Calendar.MINUTE, minute);
- c.set(Calendar.SECOND, second);
- Date date = c.getTime();
- return date;
- }
- /**
- * 是否同一天
- *
- * @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);
- }
- }
|