|
|
@@ -7,10 +7,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
import java.lang.management.ManagementFactory;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.time.DayOfWeek;
|
|
|
-import java.time.Instant;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneOffset;
|
|
|
+import java.time.*;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
@@ -27,6 +24,8 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
|
|
|
|
|
+ public static String YYYYMMDD = "yyyyMMdd";
|
|
|
+
|
|
|
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
|
|
|
|
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
|
@@ -121,6 +120,32 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
return DateFormatUtils.format(date, "yyyy/MM/dd");
|
|
|
}
|
|
|
|
|
|
+ public static Date timeToDate(Long times){
|
|
|
+ long t = times.longValue();
|
|
|
+ t = t * 1000;
|
|
|
+ return new Date(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String timestampToDateFormatMonth(Long times){
|
|
|
+ if(Validator.isEmpty(times)){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ long t = times.longValue();
|
|
|
+ t = t * 1000;
|
|
|
+ Date date = new Date(t);
|
|
|
+ return DateFormatUtils.format(date, "MM月dd号");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String timestampToDateFormatMonthTwo(Long times){
|
|
|
+ if(Validator.isEmpty(times)){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ long t = times.longValue();
|
|
|
+ t = t * 1000;
|
|
|
+ Date date = new Date(t);
|
|
|
+ return DateFormatUtils.format(date, "MM.dd");
|
|
|
+ }
|
|
|
+
|
|
|
public static String timestampToDateFormat(Long times,String patternStr){
|
|
|
if(Validator.isEmpty(times)){
|
|
|
return "";
|
|
|
@@ -131,6 +156,32 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
return DateFormatUtils.format(date, patternStr);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将秒转为时分秒格式【01:01:01】
|
|
|
+ * @param second 需要转化的秒数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String secondConvertHourMinSecond(Long second) {
|
|
|
+ String str = "";
|
|
|
+ if (second == null || second < 0) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 得到小时
|
|
|
+ long h = second / 3600;
|
|
|
+ str = h > 0 ? ((h < 10 ? ("0" + h) : h) + "时") : "";
|
|
|
+
|
|
|
+ // 得到分钟
|
|
|
+ long m = (second % 3600) / 60;
|
|
|
+ str += m > 0? (m < 10 ? ("0" + m) : m) + "分":"";
|
|
|
+
|
|
|
+ //得到剩余秒
|
|
|
+ long s = second % 60;
|
|
|
+ str += s > 0?(s < 10 ? ("0" + s) : s)+"秒":"";
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
public static String timestampToDate(Long times){
|
|
|
if(Validator.isEmpty(times)){
|
|
|
return "";
|
|
|
@@ -226,7 +277,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
public static String formatDate(Date time,String str)
|
|
|
{
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(str);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(str);
|
|
|
return sdf.format(time);
|
|
|
}
|
|
|
|
|
|
@@ -329,6 +380,21 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
return "P"+getDateOrderSn();
|
|
|
}
|
|
|
|
|
|
+ public static String getInvoiceOrderSn()
|
|
|
+ {
|
|
|
+ DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
|
+ String localDate = (LocalDateTime.now().format(ofPattern)).substring(2);
|
|
|
+ //随机数
|
|
|
+ String randomNumeric = RandomStringUtils.randomNumeric(4);
|
|
|
+ return "IN"+localDate+randomNumeric;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getTagOrderSn(String tag)
|
|
|
+ {
|
|
|
+ return tag+getDateOrderSn();
|
|
|
+ }
|
|
|
+
|
|
|
public static String secToTime(int time) {
|
|
|
String timeStr = null;
|
|
|
int hour = 0;
|
|
|
@@ -343,8 +409,9 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
|
|
|
} else {
|
|
|
hour = minute / 60;
|
|
|
- if (hour > 99)
|
|
|
+ if (hour > 99){
|
|
|
return "99:59:59";
|
|
|
+ }
|
|
|
minute = minute % 60;
|
|
|
second = time - hour * 3600 - minute * 60;
|
|
|
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
|
|
|
@@ -371,48 +438,84 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
return hh*60*60+mi*60+ss;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 指定时间往前或往后推n天
|
|
|
- *
|
|
|
- * @param dateTime 指定时间
|
|
|
- * @param x 指定天数
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Long getDayBefore(Long dateTime, int x) {
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- Date date = new Date(dateTime*1000);
|
|
|
- c.setTime(date);
|
|
|
- int day = c.get(Calendar.DATE);
|
|
|
- c.set(Calendar.DATE, day - x); //往前推几天
|
|
|
- //c.set(Calendar.DATE, day + x); 往后推几天
|
|
|
- return c.getTime().getTime()/1000;
|
|
|
+ public static Integer dayBetween(Long s1,Long s2) {
|
|
|
+ String date1str = timestampToDateFormat(s1);
|
|
|
+ String date2str = timestampToDateFormat(s2);
|
|
|
+ int count = 0;
|
|
|
+ if("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) {
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ try {
|
|
|
+ Date date1 = format.parse(date1str);
|
|
|
+ Date date2 = format.parse(date2str);
|
|
|
+ count = ((int) ((date1.getTime() - date2.getTime()) / (1000*3600*24)));
|
|
|
+ return count;
|
|
|
+ }catch (Exception e){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 指定时间往前或往后推n天
|
|
|
- *
|
|
|
- * @param dateTime 指定时间
|
|
|
- * @param x 指定天数
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Long getDayAfter(Long dateTime, int x) {
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- Date date = new Date(dateTime*1000);
|
|
|
- c.setTime(date);
|
|
|
- int day = c.get(Calendar.DATE);
|
|
|
-// c.set(Calendar.DATE, day - x); //往前推几天
|
|
|
- c.set(Calendar.DATE, day + x); //往后推几天
|
|
|
- return c.getTime().getTime()/1000;
|
|
|
+ public static Integer getTodayWeek(){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ int weekIdx = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ switch (weekIdx) {
|
|
|
+ case 1:
|
|
|
+ return 1;
|
|
|
+ case 2:
|
|
|
+ return 2;
|
|
|
+ case 3:
|
|
|
+ return 3;
|
|
|
+ case 4:
|
|
|
+ return 4;
|
|
|
+ case 5:
|
|
|
+ return 5;
|
|
|
+ case 6:
|
|
|
+ return 6;
|
|
|
+ default:
|
|
|
+ return 7;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static Date timeToDate(Long times){
|
|
|
- long t = times.longValue();
|
|
|
- t = t * 1000;
|
|
|
- return new Date(t);
|
|
|
+
|
|
|
+ public static LocalDate[] getDateArray() {
|
|
|
+ // 创建一个长度为30的数组
|
|
|
+ LocalDate[] dates = new LocalDate[30];
|
|
|
+ // 获取今天的日期
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ // 用循环给数组赋值
|
|
|
+ for (int i = 0; i < dates.length; i++) {
|
|
|
+ // 用today.plusDays(i)得到第i天的日期
|
|
|
+ dates[i] = today.plusDays(i);
|
|
|
+ }
|
|
|
+ // 返回数组
|
|
|
+ return dates;
|
|
|
}
|
|
|
|
|
|
static List<String> holiday =new ArrayList<>();
|
|
|
static List<String> extraWorkDay =new ArrayList<>();
|
|
|
+ public static Boolean isWorkingDay(long time) {
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
|
|
|
+ String formatTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+ initHoliday();
|
|
|
+ initExtraWorkDay();
|
|
|
+ //是否加班日
|
|
|
+ if(extraWorkDay.contains(formatTime)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ //是否节假日
|
|
|
+ if(holiday.contains(formatTime)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //如果是1-5表示周一到周五 是工作日
|
|
|
+ DayOfWeek week = dateTime.getDayOfWeek();
|
|
|
+ if(week==DayOfWeek.SATURDAY||week==DayOfWeek.SUNDAY){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 初始化节假日
|
|
|
@@ -438,25 +541,61 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
extraWorkDay.add("2023-10-08");
|
|
|
}
|
|
|
|
|
|
- public static Boolean isWorkingDay(long time) {
|
|
|
- LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
|
|
|
- String formatTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
- initHoliday();
|
|
|
- initExtraWorkDay();
|
|
|
- //是否加班日
|
|
|
- if(extraWorkDay.contains(formatTime)){
|
|
|
- return true;
|
|
|
- }
|
|
|
- //是否节假日
|
|
|
- if(holiday.contains(formatTime)){
|
|
|
- return false;
|
|
|
- }
|
|
|
- //如果是1-5表示周一到周五 是工作日
|
|
|
- DayOfWeek week = dateTime.getDayOfWeek();
|
|
|
- if(week==DayOfWeek.SATURDAY||week==DayOfWeek.SUNDAY){
|
|
|
- return false;
|
|
|
+
|
|
|
+ public static Long getAppointTime(Long millisecond, Integer day) {
|
|
|
+ for (Integer i = 0; i < day; i++) {
|
|
|
+ Long dayAfter = getDayBefore(millisecond, 1);
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(timeToDate(dayAfter));
|
|
|
+ int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ String[] weeks = new String[]{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
|
|
|
+
|
|
|
+ if (weeks[index].equals("星期六") || weeks[index].equals("星期天")) {
|
|
|
+ day += 1;
|
|
|
+
|
|
|
+ }
|
|
|
+ //判断当前是否为工作日
|
|
|
+ if (!isWorkingDay(dayAfter)) {
|
|
|
+ day += 1;
|
|
|
+ }
|
|
|
+ millisecond = dayAfter;
|
|
|
}
|
|
|
- return true;
|
|
|
|
|
|
+ return millisecond;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定时间往前或往后推n天
|
|
|
+ *
|
|
|
+ * @param dateTime 指定时间
|
|
|
+ * @param x 指定天数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getDayBefore(Long dateTime, int x) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date date = new Date(dateTime*1000);
|
|
|
+ c.setTime(date);
|
|
|
+ int day = c.get(Calendar.DATE);
|
|
|
+ c.set(Calendar.DATE, day - x); //往前推几天
|
|
|
+ //c.set(Calendar.DATE, day + x); 往后推几天
|
|
|
+ return c.getTime().getTime()/1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定时间往前或往后推n天
|
|
|
+ *
|
|
|
+ * @param dateTime 指定时间
|
|
|
+ * @param x 指定天数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getDayAfter(Long dateTime, int x) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date date = new Date(dateTime*1000);
|
|
|
+ c.setTime(date);
|
|
|
+ int day = c.get(Calendar.DATE);
|
|
|
+// c.set(Calendar.DATE, day - x); //往前推几天
|
|
|
+ c.set(Calendar.DATE, day + x); //往后推几天
|
|
|
+ return c.getTime().getTime()/1000;
|
|
|
}
|
|
|
+
|
|
|
}
|