|
|
@@ -0,0 +1,474 @@
|
|
|
+package cn.clevercsc.niuniutool.util;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.joda.time.DateTime;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class DateUtils {
|
|
|
+
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
|
|
+
|
|
|
+ public final static String DEFAULT_TIMEZONE = "GMT+8";
|
|
|
+
|
|
|
+ public final static String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
|
|
|
+
|
|
|
+ public final static String ISO_SHORT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
|
|
|
+
|
|
|
+ public final static String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ public final static String SHORT_TIME_FORMAT = "yyyy-MM-dd HH:mm";
|
|
|
+
|
|
|
+ public final static String FULL_SEQ_FORMAT = "yyyyMMddHHmmssSSS";
|
|
|
+
|
|
|
+ public final static String[] MULTI_FORMAT = { DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, ISO_DATE_TIME_FORMAT,
|
|
|
+ ISO_SHORT_DATE_TIME_FORMAT, SHORT_TIME_FORMAT, "yyyy-MM" };
|
|
|
+
|
|
|
+ public final static String FORMAT_YYYY = "yyyy";
|
|
|
+
|
|
|
+ public final static String FORMAT_YYYYMM = "yyyyMM";
|
|
|
+
|
|
|
+ public final static String FORMAT_YYYYMMDD = "yyyyMMdd";
|
|
|
+
|
|
|
+ public final static String FORMAT_YYYYMMDDHH = "yyyyMMddHH";
|
|
|
+
|
|
|
+ public final static String FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
|
+ public final static String FORMAT_EXCEL = "yyyy/MM/dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static String formatDate(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDate(Date date, String format) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat(format).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer formatDateToInt(Date date, String format) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return Integer.valueOf(new SimpleDateFormat(format).format(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long formatDateToLong(Date date, String format) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return Long.valueOf(new SimpleDateFormat(format).format(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatTime(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat(DEFAULT_TIME_FORMAT).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatShortTime(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat(SHORT_TIME_FORMAT).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDateNow() {
|
|
|
+ return formatDate(DateUtils.currentDate());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatTimeNow() {
|
|
|
+ return formatTime(DateUtils.currentDate());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String date, String format) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(format).parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseTime(String date, String format) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(format).parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String date) {
|
|
|
+ return parseDate(date, DEFAULT_DATE_FORMAT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseTime(String date) {
|
|
|
+ return parseTime(date, DEFAULT_TIME_FORMAT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String plusOneDay(String date) {
|
|
|
+ DateTime dateTime = new DateTime(parseDate(date).getTime());
|
|
|
+ return formatDate(dateTime.plusDays(1).toDate());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String plusOneDay(Date date) {
|
|
|
+ DateTime dateTime = new DateTime(date.getTime());
|
|
|
+ return formatDate(dateTime.plusDays(1).toDate());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getHumanDisplayForTimediff(Long diffMillis) {
|
|
|
+ if (diffMillis == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ long day = diffMillis / (24 * 60 * 60 * 1000);
|
|
|
+ long hour = (diffMillis / (60 * 60 * 1000) - day * 24);
|
|
|
+ long min = ((diffMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
|
+ long se = (diffMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ if (day > 0) {
|
|
|
+ sb.append(day + "D");
|
|
|
+ }
|
|
|
+ DecimalFormat df = new DecimalFormat("00");
|
|
|
+ sb.append(df.format(hour) + ":");
|
|
|
+ sb.append(df.format(min) + ":");
|
|
|
+ sb.append(df.format(se));
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把类似2014-01-01 ~ 2014-01-30格式的单一字符串转换为两个元素数组
|
|
|
+ */
|
|
|
+ public static Date[] parseBetweenDates(String date) {
|
|
|
+ if (StringUtils.isBlank(date)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ date = date.replace("~", "~");
|
|
|
+ Date[] dates = new Date[2];
|
|
|
+ String[] values = date.split("~");
|
|
|
+ dates[0] = parseMultiFormatDate(values[0].trim());
|
|
|
+ dates[1] = parseMultiFormatDate(values[1].trim());
|
|
|
+ return dates;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseMultiFormatDate(String date) {
|
|
|
+ try {
|
|
|
+ return org.apache.commons.lang3.time.DateUtils.parseDate(date, MULTI_FORMAT);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title:getDiffDay
|
|
|
+ * @Description:获取日期相差天数
|
|
|
+ * @param:@param beginDate
|
|
|
+ * 字符串类型开始日期
|
|
|
+ * @param:@param endDate
|
|
|
+ * 字符串类型结束日期
|
|
|
+ * @param:@return
|
|
|
+ * @return:Long 日期相差天数
|
|
|
+ * @author:谢
|
|
|
+ * @thorws:
|
|
|
+ */
|
|
|
+ public static Long getDiffDay(String beginDate, String endDate) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Long checkday = 0l;
|
|
|
+ // 开始结束相差天数
|
|
|
+ try {
|
|
|
+ checkday = (formatter.parse(endDate).getTime() - formatter.parse(beginDate).getTime())
|
|
|
+ / (1000 * 24 * 60 * 60);
|
|
|
+ } catch (ParseException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ checkday = null;
|
|
|
+ }
|
|
|
+ return checkday;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title:getDiffDay
|
|
|
+ * @Description:获取日期相差天数
|
|
|
+ * @param:@param beginDate
|
|
|
+ * Date类型开始日期
|
|
|
+ * @param:@param endDate
|
|
|
+ * Date类型结束日期
|
|
|
+ * @param:@return
|
|
|
+ * @return:Long 相差天数
|
|
|
+ * @author: 谢
|
|
|
+ * @thorws:
|
|
|
+ */
|
|
|
+ public static Long getDiffDay(Date beginDate, Date endDate) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String strBeginDate = format.format(beginDate);
|
|
|
+
|
|
|
+ String strEndDate = format.format(endDate);
|
|
|
+ return getDiffDay(strBeginDate, strEndDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * N天之后
|
|
|
+ *
|
|
|
+ * @param n
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date nDaysAfter(Integer n, Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * N天之前
|
|
|
+ *
|
|
|
+ * @param n
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date nDaysAgo(Integer n, Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - n);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Date currentDate;
|
|
|
+
|
|
|
+ public static Date setCurrentDate(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ currentDate = null;
|
|
|
+ } else {
|
|
|
+ currentDate = date;
|
|
|
+ }
|
|
|
+ return currentDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 为了便于在模拟数据程序中控制业务数据获取到的当前时间
|
|
|
+ * 提供一个帮助类处理当前时间,为了避免误操作,只有在devMode开发模式才允许“篡改”当前时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date currentDate() {
|
|
|
+ if (currentDate == null) {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+ return currentDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某段时这里写代码片间内的所有日期
|
|
|
+ * @param dBegin
|
|
|
+ * @param dEnd
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> findDates(Date dBegin, Date dEnd) {
|
|
|
+ List<Date> lDate = new ArrayList<Date>();
|
|
|
+ lDate.add(dBegin);
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 测试此日期是否在指定日期之后
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ lDate.add(calBegin.getTime());
|
|
|
+ }
|
|
|
+ return lDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getWeekOfYear(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ String yearString = formatDate(date, FORMAT_YYYY);
|
|
|
+ int weekNum = c.get(Calendar.WEEK_OF_YEAR);
|
|
|
+ if (weekNum < 10) {
|
|
|
+ yearString = StringUtils.rightPad(yearString, 5, "0");
|
|
|
+ }
|
|
|
+ return Integer.valueOf(yearString + weekNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date formatDate(String time) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ try {
|
|
|
+ return sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date fromISODate(String time) {
|
|
|
+ if (!time.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z")) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ time = time.replaceFirst("T", " ").replaceFirst(".\\d{3}Z", "");
|
|
|
+ Date date = formatDate(time);
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.add(Calendar.HOUR_OF_DAY, 8);
|
|
|
+ return ca.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date fromOBTISODate(String time) {
|
|
|
+ if (StringUtils.isBlank(time) || !time.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}")) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ time = time.replaceFirst("T", " ");
|
|
|
+ Date date = formatDate(time);
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.add(Calendar.HOUR_OF_DAY, 8);
|
|
|
+ return ca.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isValidDate(String str) {
|
|
|
+ boolean convertSuccess = true;
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ try {
|
|
|
+ format.setLenient(false);
|
|
|
+ format.parse(str);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
|
|
|
+ convertSuccess = false;
|
|
|
+ }
|
|
|
+ return convertSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isValidDate(String str, String formatStr) {
|
|
|
+ boolean convertSuccess = true;
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(formatStr);
|
|
|
+ try {
|
|
|
+ format.setLenient(false);
|
|
|
+ format.parse(str);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
|
|
|
+ convertSuccess = false;
|
|
|
+ }
|
|
|
+ return convertSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+
|
|
|
+ * @Title: getDaysOfMonth
|
|
|
+
|
|
|
+ * @Description: 获取某年某月有几天
|
|
|
+
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+
|
|
|
+ * @throws
|
|
|
+
|
|
|
+ * @author chensc
|
|
|
+
|
|
|
+ * @Date 2018年11月2日 下午4:23:39
|
|
|
+ */
|
|
|
+ public static int getDaysOfMonth(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取某个日期的开始时间
|
|
|
+
|
|
|
+ public static Date getDayStartTime(Date d) {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+
|
|
|
+ if(null != d) calendar.setTime(d);
|
|
|
+
|
|
|
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+
|
|
|
+ return calendar.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取某个日期的结束时间
|
|
|
+
|
|
|
+ public static Date getDayEndTime(Date d) {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+
|
|
|
+ if(null != d) calendar.setTime(d);
|
|
|
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
|
|
|
+
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+
|
|
|
+ return calendar.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+ //获取一个月的结束时间
|
|
|
+ public static Date getMonthEndTime(Date d) {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+
|
|
|
+ if(null != d) calendar.setTime(d);
|
|
|
+
|
|
|
+ calendar.add(Calendar.MONTH, 1);
|
|
|
+ calendar.add(Calendar.MILLISECOND, -1);
|
|
|
+ return calendar.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取一年的结束时间
|
|
|
+ public static Date getYearEndTime(Date d) {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+
|
|
|
+ if(null != d) calendar.setTime(d);
|
|
|
+
|
|
|
+ calendar.add(Calendar.YEAR, 1);
|
|
|
+ calendar.add(Calendar.MILLISECOND, -1);
|
|
|
+ return calendar.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// System.err.println((parseDate("201910",FORMAT_YYYYMM)));
|
|
|
+// System.err.println(formatDate((parseDate("201910",FORMAT_YYYYMM))));
|
|
|
+// System.err.println(formatDate(getDayStartTime(parseDate("201910",FORMAT_YYYYMM)),DEFAULT_TIME_FORMAT));
|
|
|
+// System.err.println(formatDate(getYearEndTime(parseDate("2019",FORMAT_YYYY)),DEFAULT_TIME_FORMAT));
|
|
|
+// int len="2019".split("-").length;
|
|
|
+// System.err.println( len);
|
|
|
+// SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+// Calendar cal=Calendar.getInstance();
|
|
|
+// cal.set(Calendar.YEAR, 2006);
|
|
|
+// cal.set(Calendar.MONTH, 8);
|
|
|
+// cal.set(Calendar.DAY_OF_MONTH, 3);
|
|
|
+// cal.add(Calendar.DATE, -4);
|
|
|
+// Date date=cal.getTime();
|
|
|
+// System.err.println(cal.get(Calendar.MONTH));
|
|
|
+// System.out.println(df.format(date));
|
|
|
+ DateUtils.getDayStartTime(DateUtils.parseDate("2019",DateUtils.FORMAT_YYYY));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|