|
@@ -5,7 +5,6 @@ import lombok.Data;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
-import java.util.GregorianCalendar;
|
|
|
|
|
|
@Data
|
|
|
public class DateUtil {
|
|
@@ -39,27 +38,6 @@ public class DateUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 时间字符串转为Date类型
|
|
|
- *
|
|
|
- * @param dateStr
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date strChangeDate(String dateStr, String pattern) {
|
|
|
- if (Blank.isEmpty(pattern)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- Date date = null;
|
|
|
- try {
|
|
|
- date = sdf.parse(dateStr);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return date;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Date时间转换为年月日时分秒毫秒字符串
|
|
|
*
|
|
|
* @param date
|
|
@@ -94,14 +72,6 @@ public class DateUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前年月日字符串格式为yyyy-MM-dd HH:mm:ss
|
|
|
- */
|
|
|
- public static String getCurrentYmdHms() {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- return dateChangeStrYmdhms(calendar.getTime());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 获取当前年月日字符串格式为yyyy-MM-dd
|
|
|
*/
|
|
|
public static String getCurrentYmd() {
|
|
@@ -109,15 +79,6 @@ public class DateUtil {
|
|
|
return dateChangeStr(calendar.getTime(), "yyyy-MM-dd");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 星期1-6,对应数字1-6,星期天对应0
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Integer getCurrentWeek() {
|
|
|
- Calendar cal = Calendar.getInstance();
|
|
|
- return cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 返回今天是星期几的英文
|
|
@@ -147,313 +108,4 @@ public class DateUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 将时间戳转换为年月日时分秒
|
|
|
- *
|
|
|
- * @param time
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String longStrYmdhms(long time) {
|
|
|
- Date date = new Date(time);
|
|
|
- return dateChangeStrYmdhms(date);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将时间戳转为相应的时间格式字符串
|
|
|
- *
|
|
|
- * @param time
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String longStr(Long time, String pattern) {
|
|
|
- Date date = new Date(time);
|
|
|
- return dateChangeStr(date, pattern);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断时间字符串是否符合传入的格式验证
|
|
|
- *
|
|
|
- * @param dateStr
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Boolean isValid(String dateStr, String pattern) {
|
|
|
- if (Blank.isEmpty(pattern, dateStr)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
-// sdf.setLenient(false);
|
|
|
- try {
|
|
|
- sdf.parse(dateStr);
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取今天日期 年月日
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date getTodayYmd() {
|
|
|
- Date now = new Date();
|
|
|
- Calendar cal1 = Calendar.getInstance();
|
|
|
- cal1.setTime(now);
|
|
|
- // 将时分秒,毫秒域清零
|
|
|
- cal1.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
- cal1.set(Calendar.MINUTE, 0);
|
|
|
- cal1.set(Calendar.SECOND, 0);
|
|
|
- cal1.set(Calendar.MILLISECOND, 0);
|
|
|
- return cal1.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取昨天日期 年月日
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date getYesterdayYmd() {
|
|
|
- Calendar cal = new GregorianCalendar();
|
|
|
- cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
- cal.set(Calendar.MINUTE, 0);
|
|
|
- cal.set(Calendar.SECOND, 0);
|
|
|
- cal.set(Calendar.MILLISECOND, 0);
|
|
|
- cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
- return cal.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 传入天和小时,如果传入天为0,则是今天,为1则是明天,-1为昨天,以此类推;传入的小时,传多少是多少
|
|
|
- *
|
|
|
- * @param day
|
|
|
- * @param hour
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date getAppointDh(int day, int hour) {
|
|
|
- Calendar cal = new GregorianCalendar();
|
|
|
- if (Blank.isNotEmpty(day)) {
|
|
|
- cal.add(Calendar.DATE, day);
|
|
|
- }
|
|
|
- if (Blank.isNotEmpty(hour)) {
|
|
|
- cal.set(Calendar.HOUR, hour);
|
|
|
- }
|
|
|
- cal.set(Calendar.MINUTE, 0);
|
|
|
- cal.set(Calendar.SECOND, 0);
|
|
|
- return cal.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过传入的时间字符串和格式,返回时间戳
|
|
|
- *
|
|
|
- * @param dateStr
|
|
|
- * @param pattern
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Long genTimeStamp(String dateStr, String pattern) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateStr);
|
|
|
- return date.getTime();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return 0L;
|
|
|
- }
|
|
|
-
|
|
|
- public static Long genTimeStampDay(String dateStr, String pattern, int day) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateStr);
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.DATE, day);
|
|
|
- return calendar.getTime().getTime();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return 0L;
|
|
|
- }
|
|
|
-
|
|
|
- public static Long genTimeStampHour(String dateStr, String pattern, int hour) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateStr);
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.HOUR, hour);
|
|
|
- return calendar.getTime().getTime();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return 0L;
|
|
|
- }
|
|
|
-
|
|
|
- public static Long genTimeStampMinute(String dateStr, String pattern, int minute) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateStr);
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.MINUTE, minute);
|
|
|
- return calendar.getTime().getTime();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return 0L;
|
|
|
- }
|
|
|
-
|
|
|
- public static Long genTimeStampSecond(String dateStr, String pattern, int second) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
- try {
|
|
|
- Date date = sdf.parse(dateStr);
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(date);
|
|
|
- calendar.add(Calendar.SECOND, second);
|
|
|
- return calendar.getTime().getTime();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return 0L;
|
|
|
- }
|
|
|
-
|
|
|
- public static Long addTimeStamp(Long startTime, String pattern, Integer bucketValue) {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- Date date = new Date(startTime);
|
|
|
- calendar.setTime(date);
|
|
|
- if (pattern.length() == 10) {
|
|
|
- calendar.add(Calendar.DATE, bucketValue);
|
|
|
- } else if (pattern.length() == 13) {
|
|
|
- calendar.add(Calendar.HOUR, bucketValue);
|
|
|
- } else if (pattern.length() == 16) {
|
|
|
- calendar.add(Calendar.MINUTE, bucketValue);
|
|
|
- } else if (pattern.length() == 19) {
|
|
|
- calendar.add(Calendar.SECOND, bucketValue);
|
|
|
- }
|
|
|
- return calendar.getTime().getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static Long addTimeStamp1(Long startTime, Integer bucketType, Integer bucketValue) {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- Date date = new Date(startTime);
|
|
|
- calendar.setTime(date);
|
|
|
- if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
|
|
|
- calendar.add(Calendar.DATE, bucketValue);
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)) {
|
|
|
- calendar.add(Calendar.HOUR, bucketValue);
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE)) {
|
|
|
- calendar.add(Calendar.MINUTE, bucketValue);
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
|
|
|
- calendar.add(Calendar.SECOND, bucketValue);
|
|
|
- }
|
|
|
- return calendar.getTime().getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static Long addTimeStamp2(Long startTime, Integer bucketType, Integer bucketValue) {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- Date date = new Date(startTime);
|
|
|
- calendar.setTime(date);
|
|
|
- if (bucketType.equals(Calendar.DAY_OF_MONTH)) {
|
|
|
- calendar.add(Calendar.DATE, bucketValue);
|
|
|
- } else if (bucketType.equals(Calendar.HOUR)) {
|
|
|
- calendar.add(Calendar.HOUR, bucketValue);
|
|
|
- } else if (bucketType.equals(Calendar.MINUTE)) {
|
|
|
- calendar.add(Calendar.MINUTE, bucketValue);
|
|
|
- } else if (bucketType.equals(Calendar.SECOND)) {
|
|
|
- calendar.add(Calendar.SECOND, bucketValue);
|
|
|
- }
|
|
|
- return calendar.getTime().getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static int timeDifference(Date startTime, Date endTime, String pattern, Integer bucketValue) {
|
|
|
- if (pattern.length() == 10) {
|
|
|
- if (((endTime.getTime() - startTime.getTime()) % (bucketValue * 24 * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 24 * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 24 * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (pattern.length() == 13) {
|
|
|
- if (((endTime.getTime() - startTime.getTime()) % (bucketValue * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (pattern.length() == 16) {
|
|
|
- if (((endTime.getTime() - startTime.getTime()) % (bucketValue * 60 * 1000)) == 0) {
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 60 * 1000)) + 1;
|
|
|
- } else if (pattern.length() == 19) {
|
|
|
- if (((endTime.getTime() - startTime.getTime()) % (bucketValue * 1000)) == 0) {
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 1000));
|
|
|
- }
|
|
|
- return (int) ((endTime.getTime() - startTime.getTime()) / (bucketValue * 1000)) + 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- public static int timeDifference1(Date startTime, Date endTime, Integer bucketType, Integer bucketValue) {
|
|
|
- long time = endTime.getTime() - startTime.getTime();
|
|
|
- if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
|
|
|
- if ((time % (bucketValue * 24 * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 24 * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 24 * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)) {
|
|
|
- if ((time % (bucketValue * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE)) {
|
|
|
- if ((time % (bucketValue * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
|
|
|
- if ((time % (bucketValue * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 1000)) + 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- public static int timeDifference2(Date startTime, Date endTime, Integer bucketType, Integer bucketValue) {
|
|
|
- long time = endTime.getTime() - startTime.getTime();
|
|
|
- if (bucketType.equals(Calendar.DAY_OF_MONTH)) {
|
|
|
- if ((time % (bucketValue * 24 * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 24 * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 24 * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(Calendar.HOUR)) {
|
|
|
- if ((time % (bucketValue * 60 * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 60 * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 60 * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(Calendar.MINUTE)) {
|
|
|
- if ((time % (bucketValue * 60 * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 60 * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 60 * 1000)) + 1;
|
|
|
- } else if (bucketType.equals(Calendar.SECOND)) {
|
|
|
- if ((time % (bucketValue * 1000)) == 0) {
|
|
|
- return (int) (time / (bucketValue * 1000));
|
|
|
- }
|
|
|
- return (int) (time / (bucketValue * 1000)) + 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param type 1年,2月,5日
|
|
|
- * @param num 0当,-1昨,-2前
|
|
|
- * @param currentTime 当前时间
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date countCalendarDate(Integer type, Integer num, Date currentTime) {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(currentTime);
|
|
|
- calendar.add(type, num);
|
|
|
- return calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
}
|