|
@@ -0,0 +1,794 @@
|
|
|
|
+/*
|
|
|
|
+ * 文 件 名: DateUtils.java
|
|
|
|
+ * 版 权: 浩鲸云计算科技有限公司
|
|
|
|
+ * 描 述: 日期工具类
|
|
|
|
+ * 修 改 人: lvwenbin
|
|
|
|
+ * 修改时间: 2018年9月20日
|
|
|
|
+ * 修改内容: <修改内容>
|
|
|
|
+ */
|
|
|
|
+package com.ruoyi.ems.util;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.util.Calendar;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.GregorianCalendar;
|
|
|
|
+import java.util.TimeZone;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 日期工具类 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @author lvwenbin
|
|
|
|
+ * @version [版本号, 2018年9月20日]
|
|
|
|
+ * @see [相关类/方法]
|
|
|
|
+ * @since [产品/模块版本]
|
|
|
|
+ */
|
|
|
|
+public abstract class DateUtils {
|
|
|
|
+ public static final String DEFAULT_TIME_FORMAT = "yyyyMMddHHmmss";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通用时间展示格式
|
|
|
|
+ */
|
|
|
|
+ public static final String COMMON_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
+
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $ 时间年偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return date 日期
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustYear(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.YEAR, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间年偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustYear(String time, int offset) {
|
|
|
|
+ return adjustYear(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间年偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @param format 格式化
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustYear(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustYear(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustYear fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 时间月偏移
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return date 日期
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustMonth(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.MONTH, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间月偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustMonth(String time, int offset) {
|
|
|
|
+ return adjustMonth(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间月偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @param format 格式化时间
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustMonth(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustMonth(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustMonth fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间周偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return date
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustWeek(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.DATE, 7 * offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间周偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustWeek(String time, int offset) {
|
|
|
|
+ return adjustWeek(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间周偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time 字符串日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustWeek(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustWeek(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustWeek fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间日偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustDay(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.DATE, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间日偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustDay(String time, int offset) {
|
|
|
|
+ return adjustDay(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间日偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time 字符串日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustDay(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustDay(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustDay fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间小时偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustHour(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.HOUR_OF_DAY, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间小时偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustHour(String time, int offset) {
|
|
|
|
+ return adjustHour(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间小时偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustHour(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustHour(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustHour fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间分钟偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustMinute(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.MINUTE, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间分钟偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustMinute(String time, int offset) {
|
|
|
|
+ return adjustMinute(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间分钟偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time 字符串日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustMinute(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustMinute(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustMinute fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间秒偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 字符串日期
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date adjustSecond(Date date, int offset) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ calendar.add(Calendar.SECOND, offset);
|
|
|
|
+ Date newDate = calendar.getTime();
|
|
|
|
+ return newDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间秒偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustSecond(String time, int offset) {
|
|
|
|
+ return adjustSecond(time, offset, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间秒偏移 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMddHHmmss
|
|
|
|
+ * @param offset 偏移量
|
|
|
|
+ * @return yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String adjustSecond(String time, int offset, String format) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = formatter.parse(time);
|
|
|
|
+ Date newDate = adjustSecond(date, offset);
|
|
|
|
+ retTime = formatter.format(newDate);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("adjustSecond fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $获取两个间隔日期间的天数 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param timeBefore yyyyMMdd格式
|
|
|
|
+ * @param timeAfter yyyyMMdd格式
|
|
|
|
+ * @return 天数
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static int getDayBetween(String timeBefore, String timeAfter) {
|
|
|
|
+ SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
+ long num = 0;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ long diff = sd.parse(timeAfter).getTime() - sd.parse(timeBefore).getTime();
|
|
|
|
+ num = diff / (1000 * 24 * 60 * 60);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("getDayBetween fail!", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return (int) num;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $获取一个月最后一天
|
|
|
|
+ *
|
|
|
|
+ * @param time yyyyMMdd格式
|
|
|
|
+ * @return 日期
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String getMaxDateOfMonth(String time, String format) {
|
|
|
|
+ SimpleDateFormat sd = new SimpleDateFormat(format);
|
|
|
|
+ Calendar cDay = Calendar.getInstance();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Date date = sd.parse(time);
|
|
|
|
+ cDay.setTime(date);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("getMaxDateOfMonth fail!", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return String.valueOf(cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Date转String <br>
|
|
|
|
+ * 根据format格式转换时间为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param date 时刻
|
|
|
|
+ * @param format 转换模板
|
|
|
|
+ * @return 字符串日期
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String dateToString(Date date, String format) {
|
|
|
|
+ if (date == null) {
|
|
|
|
+ date = new Date();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = formatter.format(date);
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $格式化取时间
|
|
|
|
+ *
|
|
|
|
+ * @param dateString yyyyMMddHHmmss时刻
|
|
|
|
+ * @param format 转换模板
|
|
|
|
+ * @return 字符串日期
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String formater(String dateString, String format) {
|
|
|
|
+ Date date = stringToDate(dateString);
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(format);
|
|
|
|
+ String retTime = formatter.format(date);
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 格式化取时间
|
|
|
|
+ * @param dateString 字符串日期
|
|
|
|
+ * @param from 源格式
|
|
|
|
+ * @param to 目标格式
|
|
|
|
+ * @return String
|
|
|
|
+ */
|
|
|
|
+ public static String formater(String dateString, String from, String to) {
|
|
|
|
+ Date date = stringToDate(dateString, from);
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(to);
|
|
|
|
+ String retTime = formatter.format(date);
|
|
|
|
+
|
|
|
|
+ return retTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $时间转换 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param dateKey 天
|
|
|
|
+ * @param timeKey 时间
|
|
|
|
+ * @param format 格式
|
|
|
|
+ * @return String
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String formater(int dateKey, int timeKey, String format) {
|
|
|
|
+ int year = dateKey / 10000;
|
|
|
|
+ int month = (dateKey / 100) % 100;
|
|
|
|
+ int day = dateKey % 100;
|
|
|
|
+ int hour = timeKey / 10000;
|
|
|
|
+ int min = (timeKey / 100) % 100;
|
|
|
|
+ int second = timeKey % 100;
|
|
|
|
+
|
|
|
|
+ GregorianCalendar calender = new GregorianCalendar(year, month - 1, day, hour, min, second);
|
|
|
|
+ return dateToString(calender.getTime(), format);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Date转String <br>
|
|
|
|
+ * 根据format格式转换时间为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param date 时刻
|
|
|
|
+ * @return 字符串日期
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String dateToString(Date date) {
|
|
|
|
+ return dateToString(date, COMMON_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * String转Date <br>
|
|
|
|
+ * 根据format格式转换时间为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param dateString 时刻
|
|
|
|
+ * @return date
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Date stringToDate(String dateString) {
|
|
|
|
+ return stringToDate(dateString, DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * String转Date
|
|
|
|
+ * @param dateString 字符串日期
|
|
|
|
+ * @param format 格式
|
|
|
|
+ * @return date
|
|
|
|
+ */
|
|
|
|
+ public static Date stringToDate(String dateString, String format) {
|
|
|
|
+ SimpleDateFormat sd = new SimpleDateFormat(format);
|
|
|
|
+ Date date = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ date = sd.parse(dateString);
|
|
|
|
+ }
|
|
|
|
+ catch (ParseException e) {
|
|
|
|
+ log.error("stringToDate fail!", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return date;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * String转Calendar <br>
|
|
|
|
+ * 根据format格式转换时间为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param dateString 时刻
|
|
|
|
+ * @return Calendar
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Calendar stringToCalendar(String dateString) {
|
|
|
|
+ if ((dateString == null) || (dateString.length() < 14)) {
|
|
|
|
+ throw new IllegalArgumentException(dateString);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Date date = stringToDate(dateString);
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+
|
|
|
|
+ return calendar;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * String转Calendar <br>
|
|
|
|
+ * 根据format格式转换时间为字符串
|
|
|
|
+ *
|
|
|
|
+ * @param dateString 时间字符串
|
|
|
|
+ * @param format 格式
|
|
|
|
+ * @return Calendar
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static Calendar stringToCalendar(String dateString, String format) {
|
|
|
|
+ Date date = stringToDate(dateString, format);
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ return calendar;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $获取当前时间 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @return String yyyyMMddHHmmss
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static String getCurrentTime() {
|
|
|
|
+ return dateToString(new Date(), DEFAULT_TIME_FORMAT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $获取两个时间之间的间隔(毫秒) <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param d1 日期1
|
|
|
|
+ * @param d2 日期2
|
|
|
|
+ * @return
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static long getTimeBetween(Date d1, Date d2) {
|
|
|
|
+ return Math.abs(d2.getTime() - d1.getTime());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $获取星期几
|
|
|
|
+ *
|
|
|
|
+ * @param date 输入日期
|
|
|
|
+ * @return 星期几
|
|
|
|
+ */
|
|
|
|
+ public static int getWeekOfDate(Date date) {
|
|
|
|
+ Calendar cd = Calendar.getInstance();
|
|
|
|
+ cd.setTime(date);
|
|
|
|
+ int wd = cd.get(Calendar.DAY_OF_WEEK);
|
|
|
|
+
|
|
|
|
+ if (wd == 1) {
|
|
|
|
+ return 7;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return wd - 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $早高峰区间
|
|
|
|
+ */
|
|
|
|
+ private static final String[] MORING_PEAK_INTERVAL = new String[] {
|
|
|
|
+ "070000", "090000"
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $晚高峰区间
|
|
|
|
+ */
|
|
|
|
+ private static final String[] EVENING_PEAK_INTERVAL = new String[] {
|
|
|
|
+ "170000", "190000"
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $平峰区间
|
|
|
|
+ */
|
|
|
|
+ private static final String[] FLAT_PEAK_INTERVAL = new String[] {
|
|
|
|
+ "090000", "170000"
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $判断是否早高峰 <07:00~09:00>
|
|
|
|
+ *
|
|
|
|
+ * @param date 输入日期
|
|
|
|
+ * @return boolean
|
|
|
|
+ */
|
|
|
|
+ public static boolean checkMoringPeak(Date date) {
|
|
|
|
+ String time = StringUtils.substring(dateToString(date, DEFAULT_TIME_FORMAT), 8, 14);
|
|
|
|
+ return StringUtils.compare(time, MORING_PEAK_INTERVAL[0]) >= 0
|
|
|
|
+ && StringUtils.compare(time, MORING_PEAK_INTERVAL[1]) <= 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $判断是否晚高峰 <07:00~09:00>
|
|
|
|
+ *
|
|
|
|
+ * @param date 输入日期
|
|
|
|
+ * @return boolean
|
|
|
|
+ */
|
|
|
|
+ public static boolean checkEveningPeak(Date date) {
|
|
|
|
+ String time = StringUtils.substring(dateToString(date, DEFAULT_TIME_FORMAT), 8, 14);
|
|
|
|
+ return StringUtils.compare(time, EVENING_PEAK_INTERVAL[0]) >= 0
|
|
|
|
+ && StringUtils.compare(time, EVENING_PEAK_INTERVAL[1]) <= 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $判断是否平峰 <09:00~17:00>
|
|
|
|
+ *
|
|
|
|
+ * @param date 输入日期
|
|
|
|
+ * @return boolean
|
|
|
|
+ */
|
|
|
|
+ public static boolean checkFlatPeak(Date date) {
|
|
|
|
+ String time = StringUtils.substring(dateToString(date, DEFAULT_TIME_FORMAT), 8, 14);
|
|
|
|
+ return StringUtils.compare(time, FLAT_PEAK_INTERVAL[0]) >= 0
|
|
|
|
+ && StringUtils.compare(time, FLAT_PEAK_INTERVAL[1]) <= 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $判断是否工作日 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @return
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static boolean checkWorkDay(Date date) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ int wd = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
|
+ return wd >= 1 && wd <= 5;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * $判断是否非工作日 <功能详细描述>
|
|
|
|
+ *
|
|
|
|
+ * @param date 日期
|
|
|
|
+ * @return
|
|
|
|
+ * @see [类、类#方法、类#成员]
|
|
|
|
+ */
|
|
|
|
+ public static boolean checkNoWorkDay(Date date) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ int wd = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
|
+ return wd == 6 || wd == 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Integer dateToStepIndex(String YmDHms, String tp) {
|
|
|
|
+ LocalDateTime localDateTime = LocalDateTime.parse(YmDHms, DateTimeFormatter.ofPattern(COMMON_TIME_FORMAT));
|
|
|
|
+ int hour = localDateTime.getHour();
|
|
|
|
+ int minute = localDateTime.getMinute();
|
|
|
|
+ int stepIndex = 0;
|
|
|
|
+
|
|
|
|
+ if (tp.endsWith("mi")) {
|
|
|
|
+ int interval = Integer.parseInt(tp.split("mi")[0]);
|
|
|
|
+ Double floor = Math.floor((hour * 60 + minute) / (double) interval);
|
|
|
|
+ stepIndex = floor.intValue();
|
|
|
|
+ }
|
|
|
|
+ else if (tp.endsWith("h")) {
|
|
|
|
+ stepIndex = hour;
|
|
|
|
+ }
|
|
|
|
+ return stepIndex;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 计算时间序列 - 当前时刻是当天第N个时间片
|
|
|
|
+ * @param period 时间片间隔
|
|
|
|
+ * @param timeStr 当前时间yyyy-MM-dd HH:mm:ss
|
|
|
|
+ * @return 时间序列
|
|
|
|
+ */
|
|
|
|
+ public static int buildColTimeIndex(long period, String timeStr) {
|
|
|
|
+ long dataTp = period / 60;
|
|
|
|
+ return dateToStepIndex(timeStr, dataTp + "mi");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String dateToUtcString(Date date){
|
|
|
|
+ SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
|
|
|
|
+ sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
+ String format = sdfutc.format(date);
|
|
|
|
+ return format;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Date stringUtcToDate(String date) {
|
|
|
|
+ SimpleDateFormat sdfutc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
|
|
+ sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
+ Date parse = null;
|
|
|
|
+ try {
|
|
|
|
+ parse = sdfutc.parse(date);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ log.error("mongoDbUtcToBjDate fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return parse;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Date currentDateZeroHours(Date date){
|
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ Date parse = null;
|
|
|
|
+ try {
|
|
|
|
+ parse = simpleDateFormat.parse(simpleDateFormat.format(date));
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ log.error("currentDateZeroHours fail!" + ExceptionUtils.getStackTrace(e));
|
|
|
|
+ }
|
|
|
|
+ return parse;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Integer secondDifferenceCount(Date start,Date end,Integer frequency){
|
|
|
|
+ Long count = (end.getTime()-start.getTime())/frequency;
|
|
|
|
+ return count.intValue();
|
|
|
|
+ }
|
|
|
|
+}
|