|
@@ -0,0 +1,719 @@
|
|
|
+package com.zswl.dataservicestarter.utils;
|
|
|
+
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.temporal.WeekFields;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DateUtils {
|
|
|
+
|
|
|
+ public final static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static Long timeToLong(String time) {
|
|
|
+ return timeToLong(time, FORMAT_LONG);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Long timeToLong(String time, String format) {
|
|
|
+ if (!org.springframework.util.StringUtils.hasText(time)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(format).parse(time).getTime();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static Double timeToDouble(String time, String format) {
|
|
|
+ assert format != null;
|
|
|
+
|
|
|
+ Long timestamp = null;
|
|
|
+ if (StringUtils.isNotBlank(time)) {
|
|
|
+ timestamp = new SimpleDateFormat(format).parse(time).getTime();
|
|
|
+ }
|
|
|
+ if (timestamp != null) {
|
|
|
+ return timestamp.doubleValue();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public final static String pattern = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * yyyyMMdd
|
|
|
+ */
|
|
|
+ public final static String pattern1 = "yyyyMMdd";
|
|
|
+
|
|
|
+ public final static String patternStr = "yyyy年MM月dd日";
|
|
|
+
|
|
|
+ public final static String patternyyyyMM = "yyyy-MM";
|
|
|
+
|
|
|
+ public final static String patternyyyyM = "yyyy-M";
|
|
|
+
|
|
|
+ public final static String patternHHmm = "HH:mm";
|
|
|
+
|
|
|
+ public final static String patternyyyyMis = "yyyy-MM-dd HH:mm";
|
|
|
+
|
|
|
+ public static String paresTime(Long time, String pattern) {
|
|
|
+ if (time == null || time <= 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ Date date = new Date(time);
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(pattern);
|
|
|
+ return formatter.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本周的第一天,周一
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentWeekDayStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
|
|
|
+ c.add(Calendar.DATE, -weekday);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得上周的第一天,周一
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getBeforeWeekDayStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ int weekday = c.get(Calendar.DAY_OF_WEEK) - 2 + 7;
|
|
|
+ c.add(Calendar.DATE, -weekday);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得上周的最后一天,周日
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getBeforeWeekDayEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ int weekday = c.get(Calendar.DAY_OF_WEEK);
|
|
|
+ c.add(Calendar.DATE, 8 - weekday - 7);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本周的最后一天,周日
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentWeekDayEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ int weekday = c.get(Calendar.DAY_OF_WEEK);
|
|
|
+ c.add(Calendar.DATE, 8 - weekday);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本天的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getCurrentDayStartTime() {
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDateTime date = LocalDateTime.now();
|
|
|
+ LocalDateTime startOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MIN);
|
|
|
+ Date beginTime = Date.from(startOfTheDay.atZone(zoneId).toInstant());
|
|
|
+ return beginTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本天的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getCurrentDayEndTime() {
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDateTime date = LocalDateTime.now();
|
|
|
+ LocalDateTime startOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MAX);
|
|
|
+ Date beginTime = Date.from(startOfTheDay.atZone(zoneId).toInstant());
|
|
|
+ return beginTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本小时的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentHourStartTime() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ return calendar.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本小时的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentHourEndTime() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ return calendar.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本月的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentMonthStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得上个月的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getBeforeMonthStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.add(Calendar.MONTH, -1);
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本月的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentMonthEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+ c.add(Calendar.MONTH, 1);
|
|
|
+ c.add(Calendar.DATE, -1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getBeforeMonthEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+ c.add(Calendar.DATE, -1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前年的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentYearStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 0);
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前年的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentYearEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date now = null;
|
|
|
+
|
|
|
+ c.set(Calendar.MONTH, 11);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前季度的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentQuarterStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int currentMonth = c.get(Calendar.MONTH) + 1;
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前季度的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getCurrentQuarterEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int currentMonth = c.get(Calendar.MONTH) + 1;
|
|
|
+ Date now = null;
|
|
|
+ try {
|
|
|
+ if (currentMonth >= 1 && currentMonth <= 3) {
|
|
|
+ c.set(Calendar.MONTH, 2);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+ } else if (currentMonth >= 4 && currentMonth <= 6) {
|
|
|
+ c.set(Calendar.MONTH, 5);
|
|
|
+ c.set(Calendar.DATE, 30);
|
|
|
+ } else if (currentMonth >= 7 && currentMonth <= 9) {
|
|
|
+ c.set(Calendar.MONTH, 8);
|
|
|
+ c.set(Calendar.DATE, 30);
|
|
|
+ } else if (currentMonth >= 10 && currentMonth <= 12) {
|
|
|
+ c.set(Calendar.MONTH, 11);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+ }
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return now.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前/后半年的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getHalfYearStartTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int currentMonth = c.get(Calendar.MONTH) + 1;
|
|
|
+
|
|
|
+ if (currentMonth >= 1 && currentMonth <= 6) {
|
|
|
+ c.set(Calendar.MONTH, 0);
|
|
|
+ } else if (currentMonth >= 7 && currentMonth <= 12) {
|
|
|
+ c.set(Calendar.MONTH, 6);
|
|
|
+ }
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前/后半年的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getHalfYearEndTime() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int currentMonth = c.get(Calendar.MONTH) + 1;
|
|
|
+
|
|
|
+ if (currentMonth >= 1 && currentMonth <= 6) {
|
|
|
+ c.set(Calendar.MONTH, 5);
|
|
|
+ c.set(Calendar.DATE, 30);
|
|
|
+ } else if (currentMonth >= 7 && currentMonth <= 12) {
|
|
|
+ c.set(Calendar.MONTH, 11);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+ }
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseStr2Date(String dateStr) {
|
|
|
+ SimpleDateFormat longSdf = new SimpleDateFormat(FORMAT_LONG);
|
|
|
+ try {
|
|
|
+ return longSdf.parse(dateStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getShort2Str(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SimpleDateFormat shortSdf = new SimpleDateFormat(pattern);
|
|
|
+ return shortSdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 毫秒数转时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String millsToDuration(long mills) {
|
|
|
+ long dayBal = mills % (86400000);
|
|
|
+ int days = Long.valueOf(mills / 86400000).intValue();
|
|
|
+
|
|
|
+ long hourBal = dayBal % (3600000);
|
|
|
+ int hours = Long.valueOf(dayBal / (3600000)).intValue();
|
|
|
+
|
|
|
+ long minuteBal = hourBal % (60000);
|
|
|
+ int minutes = Long.valueOf(hourBal / (60000)).intValue();
|
|
|
+
|
|
|
+ int seconds = Long.valueOf(Long.valueOf(minuteBal / 1000).intValue()).intValue();
|
|
|
+ String str = "";
|
|
|
+ if (days > 0) {
|
|
|
+ str += days + "天";
|
|
|
+ }
|
|
|
+ if (hours > 0) {
|
|
|
+ str += hours + "小时";
|
|
|
+ }
|
|
|
+ if (minutes > 0) {
|
|
|
+ str += minutes + "分钟";
|
|
|
+ }
|
|
|
+ if (seconds > 0) {
|
|
|
+ str += seconds + "秒";
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Long getDayStartTime(Long timestamp) {
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zoneId);
|
|
|
+ LocalDateTime startOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MIN);
|
|
|
+ return Timestamp.valueOf(startOfTheDay).getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得本天的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getDayEndTime(Long timestamp) {
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zoneId);
|
|
|
+ LocalDateTime endOfTheDay = LocalDateTime.of(date.toLocalDate(), LocalTime.MAX);
|
|
|
+ return Timestamp.valueOf(endOfTheDay).getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得时间在当年的周数
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer getWeekOfYear(Long timestamp) {
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zoneId);
|
|
|
+ // WeekFields.ISO = 一星期从周一开始算, 其它的请自己摸索.
|
|
|
+ WeekFields weekFields = WeekFields.ISO;
|
|
|
+ int weekNumber = date.get(weekFields.weekOfWeekBasedYear());
|
|
|
+ return weekNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getYearWeekCount(Integer year) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, Calendar.DECEMBER);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+ Integer weekCount = getWeekOfYear(c.getTimeInMillis());
|
|
|
+ //log.info("多少周:{}",weekCount);
|
|
|
+ return weekCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getWeekStartTime(Integer year, Integer week) {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ //获得指定年的第几周的开始日期(dayOfWeek是从周日开始排序的)
|
|
|
+ c.setWeekDate(year, week, 2);
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+ //获得Calendar的时间
|
|
|
+ Date starttime = c.getTime();
|
|
|
+ //log.info("{}年第{}周的开始日期为{}" ,year,week,paresTime(starttime.getTime(),FORMAT_LONG));
|
|
|
+ return starttime.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getWeekEndTime(Integer year, Integer week) {
|
|
|
+ Calendar c = new GregorianCalendar();
|
|
|
+ //获得指定年的第几周的结束日期
|
|
|
+ c.setWeekDate(year, week + 1, 1);
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ Date endtime = c.getTime();
|
|
|
+ //将时间戳格式化为指定格式
|
|
|
+ //log.info("{}年第{}周的结束日期为{}" ,year,week,paresTime(endtime.getTime(),FORMAT_LONG));
|
|
|
+ return endtime.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本月的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getMonthStartTime(Integer year, Integer month) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, month - 1);
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本月的结束时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getMonthEndTime(Integer year, Integer month) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, month - 1);
|
|
|
+ c.add(Calendar.MONTH, 1);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, 0);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本年的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getYearStartTime(Integer year) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, Calendar.JANUARY);
|
|
|
+ c.set(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本年的开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getYearEndTime(Integer year) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+
|
|
|
+ c.set(Calendar.YEAR, year);
|
|
|
+ c.set(Calendar.MONTH, Calendar.DECEMBER);
|
|
|
+ c.set(Calendar.DATE, 31);
|
|
|
+
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
+ c.set(Calendar.SECOND, 59);
|
|
|
+
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到给定时间的 当天开始时间
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getDayStart(Long time) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeInMillis(time);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ return calendar.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到给定时间的 当天最大时间
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getDayEnd(Long time) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeInMillis(time);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ return calendar.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换日期为:2023-10-10 00:00:00 再加上天数
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @param days
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Calendar getCalendarDayAndDays(Long time, int days) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date endDate = new Date(time);
|
|
|
+ calendar.setTime(endDate);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ calendar.add(Calendar.DATE, days);
|
|
|
+ return calendar;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期加减天数,,中间跳过节假日 (不包括自己)
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @param days
|
|
|
+ * @param holiday
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Calendar getCalendarDayAndDays(Long time, int days, List<String> holiday) {
|
|
|
+ if (holiday == null) {
|
|
|
+ holiday = new ArrayList<>();
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date endDate = new Date(time);
|
|
|
+ calendar.setTime(endDate);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ if (days != 0) {
|
|
|
+ boolean isAdd = true;
|
|
|
+ if (days < 0) {
|
|
|
+ isAdd = false;
|
|
|
+ }
|
|
|
+ int count = Math.abs(days);
|
|
|
+ int sumCount = 0;
|
|
|
+ while (true) {
|
|
|
+ if (isAdd) {
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ } else {
|
|
|
+ calendar.add(Calendar.DATE, -1);
|
|
|
+ }
|
|
|
+ String str = DateUtils.paresTime(calendar.getTimeInMillis(), DateUtils.pattern);
|
|
|
+ if (!holiday.contains(str)) {
|
|
|
+ sumCount++;
|
|
|
+ } else {
|
|
|
+ log.info("休息日:{}", str);
|
|
|
+ }
|
|
|
+ if (count == sumCount) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return calendar;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ log.info("本年开始时间:{},结束时间:{}", paresTime(getYearStartTime(2023), FORMAT_LONG), paresTime(getYearEndTime(2023), FORMAT_LONG));
|
|
|
+ log.info("本月开始时间:{},结束时间:{}", paresTime(getMonthStartTime(2023, 9), FORMAT_LONG), paresTime(getMonthEndTime(2023, 9), FORMAT_LONG));
|
|
|
+
|
|
|
+ Integer weekNumber = getWeekOfYear(System.currentTimeMillis());
|
|
|
+ log.info("本年一共{}周,本周是第{}周,本周时间区间:{}-{}", getYearWeekCount(2023), weekNumber, paresTime(getWeekStartTime(2023, weekNumber), FORMAT_LONG), paresTime(getWeekEndTime(2023, weekNumber), FORMAT_LONG));
|
|
|
+ }
|
|
|
+}
|