|
|
@@ -0,0 +1,905 @@
|
|
|
+package com.storlead.framework.common.util;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.ParsePosition;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.GregorianCalendar;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 时间/日期处理类
|
|
|
+ *
|
|
|
+ * @author blank
|
|
|
+ * @since 2018/5/17 上午10:16
|
|
|
+ */
|
|
|
+public class DateUtil {
|
|
|
+
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日时分秒. */
|
|
|
+ public static final String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日时分秒无间隔 */
|
|
|
+ public static final String dateFormatYMDHMSSimple = "yyyyMMddHHmmss";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日无间隔 */
|
|
|
+ public static final String dateFormatYMDSimple = "yyyyMMdd";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年 */
|
|
|
+ public static final String dateFormatYear = "yyyy";
|
|
|
+
|
|
|
+ /** 时间日期格式化到月 */
|
|
|
+ public static final String dateFormatMonth = "MM";
|
|
|
+
|
|
|
+ /** 时间日期格式化到日 */
|
|
|
+ public static final String dateFormatDay = "dd";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日. */
|
|
|
+ public static final String dateFormatYMD = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日带中文. */
|
|
|
+ public static final String dateFormatYMDCN = "yyyy年MM月dd日";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月. */
|
|
|
+ public static final String dateFormatYM = "yyyy-MM";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日时分. */
|
|
|
+ public static final String dateFormatYMDHM = "yyyy-MM-dd HH:mm";
|
|
|
+
|
|
|
+ /** 时间日期格式化到年月日时分. */
|
|
|
+ public static final String dateFormatYMDHMCN = "yyyy年MM月dd日 HH:mm";
|
|
|
+
|
|
|
+ /** 时间日期格式化到月日. */
|
|
|
+ public static final String dateFormatMD = "MM/dd";
|
|
|
+
|
|
|
+ /** 时分秒. */
|
|
|
+ public static final String dateFormatHMS = "HH:mm:ss";
|
|
|
+
|
|
|
+ /** 时分. */
|
|
|
+ public static final String dateFormatHM = "HH:mm";
|
|
|
+
|
|
|
+ /** 上午. */
|
|
|
+ public static final String AM = "AM";
|
|
|
+
|
|
|
+ /** 下午. */
|
|
|
+ public static final String PM = "PM";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:String类型的日期时间转化为Date类型.
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * String形式的日期时间
|
|
|
+ * @param format
|
|
|
+ * 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return Date Date类型日期时间
|
|
|
+ */
|
|
|
+ public static Date getDateByFormat(String strDate, String format) {
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = mSimpleDateFormat.parse(strDate);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取偏移之后的Date.
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期时间
|
|
|
+ * @param calendarField
|
|
|
+ * Calendar属性,对应offset的值,
|
|
|
+ * 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
|
|
|
+ * @param offset
|
|
|
+ * 偏移(值大于0,表示+,值小于0,表示-)
|
|
|
+ * @return Date 偏移之后的日期时间
|
|
|
+ */
|
|
|
+ public static Date getDateByOffset(Date date, int calendarField, int offset) {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ try {
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(calendarField, offset);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取指定日期时间的字符串(可偏移).
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * String形式的日期时间
|
|
|
+ * @param format
|
|
|
+ * 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param calendarField
|
|
|
+ * Calendar属性,对应offset的值,
|
|
|
+ * 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
|
|
|
+ * @param offset
|
|
|
+ * 偏移(值大于0,表示+,值小于0,表示-)
|
|
|
+ * @return String String类型的日期时间
|
|
|
+ */
|
|
|
+ public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
|
|
|
+ String mDateTime = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ c.setTime(mSimpleDateFormat.parse(strDate));
|
|
|
+ c.add(calendarField, offset);
|
|
|
+ mDateTime = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return mDateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:Date类型转化为String类型(可偏移).
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * the date
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @param calendarField
|
|
|
+ * the calendar field
|
|
|
+ * @param offset
|
|
|
+ * the offset
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
|
|
|
+ String strDate = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(calendarField, offset);
|
|
|
+ strDate = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:Date类型转化为String类型.
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * the date
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getStringByFormat(Date date, String format) {
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ String strDate = null;
|
|
|
+ try {
|
|
|
+ strDate = mSimpleDateFormat.format(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取指定日期时间的字符串,用于导出想要的格式.
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * String形式的日期时间,必须为yyyy-MM-dd HH:mm:ss格式
|
|
|
+ * @param format
|
|
|
+ * 输出格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return String 转换后的String类型的日期时间
|
|
|
+ */
|
|
|
+ public static String getStringByFormat(String strDate, String format) {
|
|
|
+ String mDateTime = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
|
|
|
+ c.setTime(mSimpleDateFormat.parse(strDate));
|
|
|
+ SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
|
|
|
+ mDateTime = mSimpleDateFormat2.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return mDateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取milliseconds表示的日期时间的字符串.
|
|
|
+ *
|
|
|
+ * @param milliseconds
|
|
|
+ * the milliseconds
|
|
|
+ * @param format
|
|
|
+ * 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return String 日期时间字符串
|
|
|
+ */
|
|
|
+ public static String getStringByFormat(long milliseconds, String format) {
|
|
|
+ String thisDateTime = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ thisDateTime = mSimpleDateFormat.format(milliseconds);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return thisDateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取表示当前日期时间的字符串.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return String String类型的当前日期时间
|
|
|
+ */
|
|
|
+ public static String getCurrentDate(String format) {
|
|
|
+ String curDateTime = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ curDateTime = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return curDateTime;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期时间的字符串 格式为 yyyyMMdd
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2018-12-19 下午 12:01
|
|
|
+ */
|
|
|
+ public static String getCurrentDateString() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatYMDSimple);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期时间的字符串 格式为 yyyyMMddHHmmss
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2018-12-19 下午 12:01
|
|
|
+ */
|
|
|
+ public static String getCurrentDateYearMonthDayMinSecString() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatYMDHMSSimple);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期时间的字符串 格式为 yyyy年-MM月-dd日
|
|
|
+ *
|
|
|
+ * @return 格式为 yyyy年-MM月-dd日
|
|
|
+ */
|
|
|
+ public static String getCurrentDateCNString() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatYMDCN);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期 年 的字符串 格式为 yyyy
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2019-1-10 下午 4:30
|
|
|
+ */
|
|
|
+ public static String getCurrentDateYear() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatYear);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期 月 的字符串 格式为 MM
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2019-1-10 下午 4:31
|
|
|
+ */
|
|
|
+ public static String getCurrentDateMonth() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatMonth);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期 天 的字符串 格式为 dd
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2019-1-10 下午 4:32
|
|
|
+ */
|
|
|
+ public static String getCurrentDateDay() {
|
|
|
+ return DateUtil.getCurrentDate(dateFormatDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回当前系统时间毫秒值
|
|
|
+ *
|
|
|
+ * @return java.lang.String
|
|
|
+ * @author blank
|
|
|
+ * @since 2018-12-19 下午 12:03
|
|
|
+ */
|
|
|
+ public static String getCurrentTimeMills(){
|
|
|
+ return System.currentTimeMillis() + "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取表示当前日期时间的字符串(可偏移).
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @param calendarField
|
|
|
+ * Calendar属性,对应offset的值,
|
|
|
+ * 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
|
|
|
+ * @param offset
|
|
|
+ * 偏移(值大于0,表示+,值小于0,表示-)
|
|
|
+ * @return String String类型的日期时间
|
|
|
+ */
|
|
|
+ public static String getCurrentDateByOffset(String format, int calendarField, int offset) {
|
|
|
+ String mDateTime = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ c.add(calendarField, offset);
|
|
|
+ mDateTime = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return mDateTime;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:计算两个日期所差的天数.
|
|
|
+ *
|
|
|
+ * @param milliseconds1
|
|
|
+ * the milliseconds1
|
|
|
+ * @param milliseconds2
|
|
|
+ * the milliseconds2
|
|
|
+ * @return int 所差的天数
|
|
|
+ */
|
|
|
+ public static int getOffectDay(long milliseconds1, long milliseconds2) {
|
|
|
+ Calendar calendar1 = Calendar.getInstance();
|
|
|
+ calendar1.setTimeInMillis(milliseconds1);
|
|
|
+ Calendar calendar2 = Calendar.getInstance();
|
|
|
+ calendar2.setTimeInMillis(milliseconds2);
|
|
|
+ // 先判断是否同年
|
|
|
+ int y1 = calendar1.get(Calendar.YEAR);
|
|
|
+ int y2 = calendar2.get(Calendar.YEAR);
|
|
|
+ int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
|
|
|
+ int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
|
|
|
+ int maxDays = 0;
|
|
|
+ int day = 0;
|
|
|
+ if (y1 - y2 > 0) {
|
|
|
+ maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
|
|
|
+ day = d1 - d2 + maxDays;
|
|
|
+ } else if (y1 - y2 < 0) {
|
|
|
+ maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
|
|
|
+ day = d1 - d2 - maxDays;
|
|
|
+ } else {
|
|
|
+ day = d1 - d2;
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:计算两个日期所差的小时数.
|
|
|
+ *
|
|
|
+ * @param date1
|
|
|
+ * 第一个时间的毫秒表示
|
|
|
+ * @param date2
|
|
|
+ * 第二个时间的毫秒表示
|
|
|
+ * @return int 所差的小时数
|
|
|
+ */
|
|
|
+ public static int getOffsetHour(long date1, long date2) {
|
|
|
+ Calendar calendar1 = Calendar.getInstance();
|
|
|
+ calendar1.setTimeInMillis(date1);
|
|
|
+ Calendar calendar2 = Calendar.getInstance();
|
|
|
+ calendar2.setTimeInMillis(date2);
|
|
|
+ int h1 = calendar1.get(Calendar.HOUR_OF_DAY);
|
|
|
+ int h2 = calendar2.get(Calendar.HOUR_OF_DAY);
|
|
|
+ int h = 0;
|
|
|
+ int day = getOffectDay(date1, date2);
|
|
|
+ h = h1 - h2 + day * 24;
|
|
|
+ return h;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:计算两个日期所差的分钟数.
|
|
|
+ *
|
|
|
+ * @param date1
|
|
|
+ * 第一个时间的毫秒表示
|
|
|
+ * @param date2
|
|
|
+ * 第二个时间的毫秒表示
|
|
|
+ * @return int 所差的分钟数
|
|
|
+ */
|
|
|
+ public static int getOffsetMinutes(long date1, long date2) {
|
|
|
+ Calendar calendar1 = Calendar.getInstance();
|
|
|
+ calendar1.setTimeInMillis(date1);
|
|
|
+ Calendar calendar2 = Calendar.getInstance();
|
|
|
+ calendar2.setTimeInMillis(date2);
|
|
|
+ int m1 = calendar1.get(Calendar.MINUTE);
|
|
|
+ int m2 = calendar2.get(Calendar.MINUTE);
|
|
|
+ int h = getOffsetHour(date1, date2);
|
|
|
+ int m = 0;
|
|
|
+ m = m1 - m2 + h * 60;
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取本周一.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getFirstDayOfWeek(String format) {
|
|
|
+ return getDayOfWeek(format, Calendar.MONDAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取本周日.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getLastDayOfWeek(String format) {
|
|
|
+ return getDayOfWeek(format, Calendar.SUNDAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取本周的某一天.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @param calendarField
|
|
|
+ * the calendar field
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ private static String getDayOfWeek(String format, int calendarField) {
|
|
|
+ String strDate = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ int week = c.get(Calendar.DAY_OF_WEEK);
|
|
|
+ if (week == calendarField) {
|
|
|
+ strDate = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } else {
|
|
|
+ int offectDay = calendarField - week;
|
|
|
+ if (calendarField == Calendar.SUNDAY) {
|
|
|
+ offectDay = 7 - Math.abs(offectDay);
|
|
|
+ }
|
|
|
+ c.add(Calendar.DATE, offectDay);
|
|
|
+ strDate = mSimpleDateFormat.format(c.getTime());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取本月第一天.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getFirstDayOfMonth(String format) {
|
|
|
+ String strDate = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ // 当前月的第一天
|
|
|
+ c.set(GregorianCalendar.DAY_OF_MONTH, 1);
|
|
|
+ strDate = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return strDate;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取本月最后一天.
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return String String类型日期时间
|
|
|
+ */
|
|
|
+ public static String getLastDayOfMonth(String format) {
|
|
|
+ String strDate = null;
|
|
|
+ try {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ // 当前月的最后一天
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+ c.roll(Calendar.DATE, -1);
|
|
|
+ strDate = mSimpleDateFormat.format(c.getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取表示当前日期的0点时间毫秒数.
|
|
|
+ *
|
|
|
+ * @return the first time of day
|
|
|
+ */
|
|
|
+ public static long getFirstTimeOfDay() {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ String currentDate = getCurrentDate(dateFormatYMD);
|
|
|
+ date = getDateByFormat(currentDate + " 00:00:00", dateFormatYMDHMS);
|
|
|
+ return date.getTime();
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取表示当前日期24点时间毫秒数.
|
|
|
+ *
|
|
|
+ * @return the last time of day
|
|
|
+ */
|
|
|
+ public static long getLastTimeOfDay() {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ String currentDate = getCurrentDate(dateFormatYMD);
|
|
|
+ date = getDateByFormat(currentDate + " 24:00:00", dateFormatYMDHMS);
|
|
|
+ return date.getTime();
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:判断是否是闰年()
|
|
|
+ * <p>
|
|
|
+ * (year能被4整除 并且 不能被100整除) 或者 year能被400整除,则该年为闰年.
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * 年代(如2012)
|
|
|
+ * @return boolean 是否为闰年
|
|
|
+ */
|
|
|
+ public static boolean isLeapYear(int year) {
|
|
|
+ return ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:根据时间返回格式化后的时间的描述. 小于1小时显示多少分钟前 大于1小时显示今天+实际日期,大于今天全部显示实际时间
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * the str date
|
|
|
+ * @param outFormat
|
|
|
+ * the out format
|
|
|
+ * @return the string
|
|
|
+ */
|
|
|
+ public static String formatDateStr2Desc(String strDate, String outFormat) {
|
|
|
+
|
|
|
+ DateFormat df = new SimpleDateFormat(dateFormatYMDHMS);
|
|
|
+ Calendar c1 = Calendar.getInstance();
|
|
|
+ Calendar c2 = Calendar.getInstance();
|
|
|
+ try {
|
|
|
+ c2.setTime(df.parse(strDate));
|
|
|
+ c1.setTime(new Date());
|
|
|
+ int d = getOffectDay(c1.getTimeInMillis(), c2.getTimeInMillis());
|
|
|
+ if (d == 0) {
|
|
|
+ int h = getOffsetHour(c1.getTimeInMillis(), c2.getTimeInMillis());
|
|
|
+ if (h > 0) {
|
|
|
+ return "今天" + getStringByFormat(strDate, dateFormatHM);
|
|
|
+ // return h + "小时前";
|
|
|
+ } else if (h < 0) {
|
|
|
+ // return Math.abs(h) + "小时后";
|
|
|
+ } else if (h == 0) {
|
|
|
+ int m = getOffsetMinutes(c1.getTimeInMillis(), c2.getTimeInMillis());
|
|
|
+ if (m > 0) {
|
|
|
+ return m + "分钟前";
|
|
|
+ } else if (m < 0) {
|
|
|
+ // return Math.abs(m) + "分钟后";
|
|
|
+ } else {
|
|
|
+ return "刚刚";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (d > 0) {
|
|
|
+ if (d == 1) {
|
|
|
+ // return "昨天"+getStringByFormat(strDate,outFormat);
|
|
|
+ } else if (d == 2) {
|
|
|
+ // return "前天"+getStringByFormat(strDate,outFormat);
|
|
|
+ }
|
|
|
+ } else if (d < 0) {
|
|
|
+ if (d == -1) {
|
|
|
+ // return "明天"+getStringByFormat(strDate,outFormat);
|
|
|
+ } else if (d == -2) {
|
|
|
+ // return "后天"+getStringByFormat(strDate,outFormat);
|
|
|
+ } else {
|
|
|
+ // return Math.abs(d) +
|
|
|
+ // "天后"+getStringByFormat(strDate,outFormat);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String out = getStringByFormat(strDate, outFormat);
|
|
|
+ if (!StringUtils.isBlank(out)) {
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取指定日期为星期几.
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * 指定日期
|
|
|
+ * @param inFormat
|
|
|
+ * 指定日期格式
|
|
|
+ * @return String 星期几
|
|
|
+ */
|
|
|
+ public static String getWeekNumber(String strDate, String inFormat) {
|
|
|
+ Assert.isTrue((StringUtils.isNoneBlank(strDate) && StringUtils.isNoneBlank(inFormat)), "strDate or inFormat can not be null!");
|
|
|
+
|
|
|
+ Date mDate = getDateByFormat(strDate, inFormat);
|
|
|
+ Calendar calendar = new GregorianCalendar();
|
|
|
+ calendar.setTime(mDate);
|
|
|
+
|
|
|
+ int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ switch (intTemp) {
|
|
|
+ case 0:
|
|
|
+ return "星期日";
|
|
|
+ case 1:
|
|
|
+ return "星期一";
|
|
|
+ case 2:
|
|
|
+ return "星期二";
|
|
|
+ case 3:
|
|
|
+ return "星期三";
|
|
|
+ case 4:
|
|
|
+ return "星期四";
|
|
|
+ case 5:
|
|
|
+ return "星期五";
|
|
|
+ case 6:
|
|
|
+ return "星期六";
|
|
|
+
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据给定的日期判断是否为上下午.
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * the str date
|
|
|
+ * @param format
|
|
|
+ * the format
|
|
|
+ * @return the time quantum Calendar.get(Calendar.HOUR_OF_DAY)
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static String getTimeQuantum(String strDate, String format) {
|
|
|
+ Date mDate = getDateByFormat(strDate, format);
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(mDate);
|
|
|
+ if (calendar.get(Calendar.AM_PM) == Calendar.AM) {
|
|
|
+ return "AM";
|
|
|
+ } else {
|
|
|
+ return "PM";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前日期是否上下午.
|
|
|
+ * 如果true 表示是上午 false 表示下午
|
|
|
+ *
|
|
|
+ * @return the time quantum Calendar.get(Calendar.HOUR_OF_DAY)
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static Boolean getIfTimeNowIsAM() {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ if (calendar.get(Calendar.AM_PM) == Calendar.AM) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据给定的毫秒数算得时间的描述.
|
|
|
+ *
|
|
|
+ * @param milliseconds
|
|
|
+ * the milliseconds
|
|
|
+ * @return the time description
|
|
|
+ */
|
|
|
+ public static String getTimeDescription(long milliseconds) {
|
|
|
+ if (milliseconds > 1000) {
|
|
|
+ // 大于一分
|
|
|
+ if (milliseconds / 1000 / 60 > 1) {
|
|
|
+ long minute = milliseconds / 1000 / 60;
|
|
|
+ long second = milliseconds / 1000 % 60;
|
|
|
+ return minute + "分" + second + "秒";
|
|
|
+ } else {
|
|
|
+ // 显示秒
|
|
|
+ return milliseconds / 1000 + "秒";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return milliseconds + "毫秒";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析时间按照小时:分:秒格式输出
|
|
|
+ *
|
|
|
+ * @param iTimeSeconds 秒
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTime(int iTimeSeconds) {
|
|
|
+ String strUserTime = iTimeSeconds / 60 + " : "
|
|
|
+ + ((iTimeSeconds % 60) < 10 ? ("0" + iTimeSeconds % 60) : iTimeSeconds % 60);
|
|
|
+ return strUserTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@Author: timo
|
|
|
+ *@Date: 2018/5/23 12:01
|
|
|
+ *@Description:
|
|
|
+ * 获取本周一日期
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getMondayOFWeek(Date date){
|
|
|
+ int mondayPlus = getMondayPlus(date);
|
|
|
+ GregorianCalendar currentDate = new GregorianCalendar();
|
|
|
+ currentDate.setTime(date);
|
|
|
+// System.out.println(" 当前时间 :"+currentDate.getTime());
|
|
|
+ currentDate.add(GregorianCalendar.DATE, mondayPlus);
|
|
|
+ return currentDate.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@Author: timo
|
|
|
+ *@Date: 2018/5/23 12:02
|
|
|
+ *@Description:
|
|
|
+ * 获取本周日日期
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getCurrentWeekday(Date date){
|
|
|
+ int mondayPlus = getMondayPlus(date);
|
|
|
+ GregorianCalendar currentDate = new GregorianCalendar();
|
|
|
+ currentDate.setTime(date);
|
|
|
+ currentDate.add(GregorianCalendar.DATE, mondayPlus+ 6);
|
|
|
+ return currentDate.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@Author: timo
|
|
|
+ *@Date: 2018/5/23 12:03
|
|
|
+ *@Description:
|
|
|
+ * 获取下周一的日期
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getNextMonday(Date date) {
|
|
|
+ int mondayPlus = getMondayPlus(date);
|
|
|
+ GregorianCalendar currentDate = new GregorianCalendar();
|
|
|
+ currentDate.setTime(date);
|
|
|
+ currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
|
|
|
+ return currentDate.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@Author: timo
|
|
|
+ *@Date: 2018/5/23 12:03
|
|
|
+ *@Description:
|
|
|
+ * 获取下周日的日期
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getNextSunday(Date date) {
|
|
|
+ int mondayPlus = getMondayPlus(date);
|
|
|
+ GregorianCalendar currentDate = new GregorianCalendar();
|
|
|
+ currentDate.setTime(date);
|
|
|
+ currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);
|
|
|
+ return currentDate.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int getMondayPlus(Date date) {
|
|
|
+ Calendar cd = Calendar.getInstance();
|
|
|
+ cd.setTime(date);
|
|
|
+ // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
|
|
|
+ // 因为按中国礼拜一作为第一天所以这里减1
|
|
|
+ int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (dayOfWeek == 0) {
|
|
|
+ return -6;
|
|
|
+ } else {
|
|
|
+ return 1-dayOfWeek;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@Author: timo
|
|
|
+ *@Date: 2018/5/24 17:32
|
|
|
+ *@Description:
|
|
|
+ * 添加分钟
|
|
|
+ * @param date 从那个时间开始
|
|
|
+ * @param minute 延长的分钟
|
|
|
+ */
|
|
|
+ public static Date addMinute(Date date, Integer minute){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.MINUTE,minute);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author: timo
|
|
|
+ * @Date: 2018/6/12 16:46
|
|
|
+ * @Description: 获取传入时间的月份的最后一天
|
|
|
+ * @param
|
|
|
+ * @return:
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public static Date getLastDayByMonth(Date date){
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return ca.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getDateShort(Date date){
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String strDate=formatter.format(date);
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ Date strtodate = formatter.parse(strDate, pos);
|
|
|
+ return strtodate;
|
|
|
+ }
|
|
|
+ public static void main(String[] args) {
|
|
|
+ LocalDateTime dateTime1 = LocalDateTime.of(2023, 10, 1, 12, 0);
|
|
|
+ LocalDateTime dateTime2 = LocalDateTime.of(2023, 10, 5, 14, 0);
|
|
|
+
|
|
|
+ // 计算两个 LocalDateTime 之间的天数差
|
|
|
+ long daysDifference = ChronoUnit.DAYS.between(dateTime1, dateTime2);
|
|
|
+
|
|
|
+ // 将结果转换为 Integer 类型
|
|
|
+ Integer daysDifferenceInteger = (int) daysDifference;
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ System.out.println("Days difference: " + daysDifferenceInteger);
|
|
|
+// System.out.println("date : "+DateUtil.getStringByOffset(new Date(),"yyyy-MM-dd HH:mm:ss", Calendar.DATE,-5));
|
|
|
+// // 本周一
|
|
|
+// Date nowDate = DateUtil.getDateByFormat("2018-07-02",DateUtil.dateFormatYMD);
|
|
|
+// Date firsDate = getMondayOFWeek(nowDate);
|
|
|
+// String firsDateStr = DateUtil.getStringByFormat(firsDate,dateFormatYMD);
|
|
|
+// System.out.println("firsDateStr : "+firsDateStr);
|
|
|
+//
|
|
|
+// // 本周日
|
|
|
+// Date lastCurDate = getCurrentWeekday(firsDate);
|
|
|
+// String lastCurDateStr = DateUtil.getStringByFormat(lastCurDate,dateFormatYMD);
|
|
|
+// System.out.println("lastCurDateStr : "+lastCurDateStr);
|
|
|
+//
|
|
|
+// // 下周一
|
|
|
+// Date nextMonday = getNextMonday(firsDate);
|
|
|
+// String nextMondayStr = DateUtil.getStringByFormat(nextMonday,dateFormatYMD);
|
|
|
+// System.out.println("nextMondayStr : "+nextMondayStr);
|
|
|
+// // 下周日
|
|
|
+// Date nextSunday = getNextSunday(firsDate);
|
|
|
+// String nextSundayStr = DateUtil.getStringByFormat(nextSunday,dateFormatYMD);
|
|
|
+// System.out.println("nextSundayStr : "+nextSundayStr);
|
|
|
+//
|
|
|
+// Date toDay = new Date();
|
|
|
+// String toDayStr = DateUtil.getStringByFormat(toDay,dateFormatYMD);
|
|
|
+// System.out.println("toDayStr : "+toDayStr);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|