|
@@ -0,0 +1,378 @@
|
|
|
+package com.fs4a.commons.basic.utils;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description: 时间格式工具类
|
|
|
+ * @author: huangLin
|
|
|
+ * @date: 2021/5/17 15:36:26
|
|
|
+ */
|
|
|
+public class DateUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 年月日字符串转换date (yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static Date getDateByStr(String str) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ try {
|
|
|
+ return sdf.parse(str);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上一个月的相同日期
|
|
|
+ */
|
|
|
+ public static String getLastMonthDay() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.MONTH, -1);
|
|
|
+ SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
|
|
|
+ String lastMonth = dft.format(cal.getTime());
|
|
|
+ //获取当前日期
|
|
|
+ SimpleDateFormat ddft = new SimpleDateFormat("dd");
|
|
|
+ String day = ddft.format(new Date());
|
|
|
+ return lastMonth + "-" + day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取去年的相同日期
|
|
|
+ */
|
|
|
+ public static String getLastYearDay() {
|
|
|
+ SimpleDateFormat formats = new SimpleDateFormat("yyyy");
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.add(Calendar.YEAR, -1);
|
|
|
+ Date date = c.getTime();
|
|
|
+ SimpleDateFormat dft = new SimpleDateFormat("MM-dd");
|
|
|
+ return formats.format(date) + "-" + dft.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询两个时间相差多少ms
|
|
|
+ */
|
|
|
+ public static long distanceForMillisecond(Date beginTime, Date endTime) {
|
|
|
+ return Math.abs(endTime.getTime() - beginTime.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义 格式化规则 默认(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static String toStr(Date date, String pattern) {
|
|
|
+ if (pattern == null) pattern = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ String format = sdf.format(date);
|
|
|
+ return format;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期的 天数 加法
|
|
|
+ */
|
|
|
+ public static Date getAddDay(Date date, int day) {
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.add(Calendar.DATE, day);
|
|
|
+ Date addDay = ca.getTime();
|
|
|
+ return addDay;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期的 年份 加法
|
|
|
+ */
|
|
|
+ public static Date getAddYear(Date date, int num) {
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.add(Calendar.YEAR, num);
|
|
|
+ Date addYear = ca.getTime();
|
|
|
+ return addYear;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取起始时间 延后月份的某一天的开始时间
|
|
|
+ *
|
|
|
+ * @param date 起始时间
|
|
|
+ * @param month 叠加月数
|
|
|
+ * @param day 自然日
|
|
|
+ */
|
|
|
+ public static Date assignMonthAndDay(Date date, int month, int day) {
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ // 推迟月份
|
|
|
+ ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) + month);
|
|
|
+ //设置为当月最后一天
|
|
|
+ ca.set(Calendar.DAY_OF_MONTH, day);
|
|
|
+ //将小时至0
|
|
|
+ ca.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ //将分钟至0
|
|
|
+ ca.set(Calendar.MINUTE, 0);
|
|
|
+ //将秒至0
|
|
|
+ ca.set(Calendar.SECOND, 0);
|
|
|
+ //将毫秒至0
|
|
|
+ ca.set(Calendar.MILLISECOND, 0);
|
|
|
+ return ca.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前时间加减秒
|
|
|
+ */
|
|
|
+ public static Date addSecond(Long millisecond) {
|
|
|
+ long currentTime = System.currentTimeMillis() + millisecond;
|
|
|
+ return new Date(currentTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述 : 获取今天是星期几
|
|
|
+ */
|
|
|
+ public static int getHowWeek(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (week == 0) {
|
|
|
+ week = 7;
|
|
|
+ }
|
|
|
+ return week;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单前月份
|
|
|
+ */
|
|
|
+ public static int getCurrentMonth() {
|
|
|
+ return 1 + Calendar.getInstance().get(Calendar.MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当天0点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得昨天0点时间
|
|
|
+ */
|
|
|
+ public static Date getYesterdayMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTimeInMillis(getTimesMorning().getTime() - 3600 * 24 * 1000);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当天近7天时间
|
|
|
+ */
|
|
|
+ public static Date getWeekFromNow() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTimeInMillis(getTimesMorning().getTime() - 3600 * 24 * 1000 * 7);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当天24点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesNight() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 24);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本周一0点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesWeekMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本周日24点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesWeeknight() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getTimesWeekMorning());
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 7);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本月第一天0点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesMonthMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本月未24点时间
|
|
|
+ */
|
|
|
+ public static Date getTimesMonthNight() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 24);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上月初0点时间
|
|
|
+ */
|
|
|
+ public static Date getLastMonthStartMorning() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getTimesMonthMorning());
|
|
|
+ cal.add(Calendar.MONTH, -1);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本季度開始点时间
|
|
|
+ */
|
|
|
+ public static Date getCurrentQuarterStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int currentMonth = c.get(Calendar.MONTH) + 1;
|
|
|
+ SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date now = null;
|
|
|
+ try {
|
|
|
+ if (currentMonth >= 1 && currentMonth <= 3)
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 0);
|
|
|
+ else if (currentMonth >= 4 && currentMonth <= 6)
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 3);
|
|
|
+ else if (currentMonth >= 7 && currentMonth <= 9)
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 4);
|
|
|
+ else if (currentMonth >= 10 && currentMonth <= 12)
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 9);
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+ now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return now;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前季度的结束时间
|
|
|
+ */
|
|
|
+ public static Date getCurrentQuarterEndTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getCurrentQuarterStartTime());
|
|
|
+ cal.add(Calendar.MONTH, 3);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本年開始点时间
|
|
|
+ */
|
|
|
+ public static Date getCurrentYearStartTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本年结束点时间
|
|
|
+ */
|
|
|
+ public static Date getCurrentYearEndTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getCurrentYearStartTime());
|
|
|
+ cal.add(Calendar.YEAR, 1);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上年開始点时间
|
|
|
+ */
|
|
|
+ public static Date getLastYearStartTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getCurrentYearStartTime());
|
|
|
+ cal.add(Calendar.YEAR, -1);
|
|
|
+ return cal.getTime();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否在这段时间内
|
|
|
+ *
|
|
|
+ * @param nowDate 当前日期
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ */
|
|
|
+ public static boolean isInTime(String nowDate, String startTime, String endTime) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if (sdf.format(new Date()).equals(nowDate)) {
|
|
|
+ sdf = new SimpleDateFormat("HH:mm");
|
|
|
+ if (startTime.equals("00:00")) {
|
|
|
+ startTime = "24:00";
|
|
|
+ }
|
|
|
+ if (endTime.equals("00:00")) {
|
|
|
+ endTime = "24:00";
|
|
|
+ }
|
|
|
+ long current = 0; //当前时间
|
|
|
+ try {
|
|
|
+ current = sdf.parse(sdf.format(new Date())).getTime();
|
|
|
+ long start = sdf.parse(startTime).getTime(); //开始时间
|
|
|
+ long end = sdf.parse(endTime).getTime();
|
|
|
+ if (current >= start && current < end) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ System.out.println(isInTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "17:44", "17:45"));
|
|
|
+
|
|
|
+ System.out.println("当天24点时间:" + toStr(getTimesNight(), null));
|
|
|
+ System.out.println("当前时间:" + toStr(new Date(), null));
|
|
|
+ System.out.println("当天0点时间:" + toStr(getTimesMorning(), null));
|
|
|
+ System.out.println("昨天0点时间:" + toStr(getYesterdayMorning(), null));
|
|
|
+ System.out.println("近7天时间:" + toStr(getWeekFromNow(), null));
|
|
|
+ System.out.println("本周周一0点时间:" + toStr(getTimesWeekMorning(), null));
|
|
|
+ System.out.println("本周周日24点时间:" + toStr(getTimesWeeknight(), null));
|
|
|
+ System.out.println("本月初0点时间:" + toStr(getTimesMonthMorning(), null));
|
|
|
+ System.out.println("本月未24点时间:" + toStr(getTimesMonthNight(), null));
|
|
|
+ System.out.println("上月初0点时间:" + toStr(getLastMonthStartMorning(), null));
|
|
|
+ System.out.println("本季度開始点时间:" + toStr(getCurrentQuarterStartTime(), null));
|
|
|
+ System.out.println("本季度结束点时间:" + toStr(getCurrentQuarterEndTime(), null));
|
|
|
+ System.out.println("本年開始点时间:" + toStr(getCurrentYearStartTime(), null));
|
|
|
+ System.out.println("本年结束点时间:" + toStr(getCurrentYearEndTime(), null));
|
|
|
+ System.out.println("上年開始点时间:" + toStr(getLastYearStartTime(), null));
|
|
|
+ }
|
|
|
+}
|