110 lines
3.8 KiB
Java
110 lines
3.8 KiB
Java
package com.ydw.bat.wkflow.util;
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.text.MessageFormat;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.ZoneId;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @Description: TODO
|
|
* @author: super_liu
|
|
* @date: 2021年03月04日 17:13
|
|
*/
|
|
public class Utils {
|
|
|
|
public static String format(String value, Object... paras) {
|
|
return MessageFormat.format(value, paras);
|
|
}
|
|
|
|
public static int subDayNum(String endTime){
|
|
if (endTime.length() >= 10) {
|
|
endTime = endTime.substring(0, 10);
|
|
}
|
|
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
LocalDate startDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()),fmt);
|
|
LocalDate endDate = LocalDate.parse(endTime,fmt);
|
|
int days = (int) startDate.until(endDate, ChronoUnit.DAYS);
|
|
return days;
|
|
}
|
|
|
|
public static String addDay(Integer dayInt) {
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Date today = new Date();
|
|
Calendar theCa = Calendar.getInstance();
|
|
theCa.setTime(today);
|
|
theCa.add(theCa.DATE, +dayInt);//最后一个数字30可改,30天的意思
|
|
Date start = theCa.getTime();
|
|
String startDate = sdf.format(start);//三十天之前日期
|
|
|
|
return startDate;
|
|
}
|
|
|
|
public static String interceptTime(String timeStr) {
|
|
if (StringUtils.isNotBlank(timeStr)) {
|
|
System.out.println(timeStr.length());
|
|
if (timeStr.contains("T")) {
|
|
DateTimeFormatter df20 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
|
|
DateTimeFormatter df24 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
|
|
LocalDateTime ldt = LocalDateTime.parse(timeStr, timeStr.length() > 20 ? df24:df20);
|
|
ZoneId currentZone = ZoneId.of("UTC");
|
|
ZoneId newZone = ZoneId.of("Asia/Shanghai");
|
|
timeStr = ldt.atZone(currentZone).withZoneSameInstant(newZone).toLocalDateTime().toString();
|
|
}
|
|
if (timeStr.length() >= 10) {
|
|
return timeStr.substring(0, 10);
|
|
}
|
|
}
|
|
return timeStr;
|
|
}
|
|
|
|
public static String subDay(String date, Integer dayInt) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Date today = null;
|
|
try {
|
|
today = sdf.parse(date);
|
|
} catch (ParseException e) {
|
|
}
|
|
String endDate = sdf.format(today);//当前日期
|
|
Calendar theCa = Calendar.getInstance();
|
|
theCa.setTime(today);
|
|
theCa.add(theCa.DATE, -dayInt);//最后一个数字30可改,30天的意思
|
|
Date start = theCa.getTime();
|
|
String startDate = sdf.format(start);//三十天之前日期
|
|
|
|
return startDate;
|
|
}
|
|
|
|
public static Map<String, JSONArray> jsonArrGroup(JSONArray arr, String groupName, String sGroupName) {
|
|
Map<String, JSONArray> map = new HashMap<>();
|
|
String tempIdStr = "";
|
|
JSONArray list;
|
|
for(Object obj : arr){
|
|
JSONObject jsonObject = (JSONObject) obj;
|
|
tempIdStr = jsonObject.getString(groupName);
|
|
if(tempIdStr.equals("")){
|
|
continue;
|
|
}
|
|
if(map.containsKey(tempIdStr)){
|
|
list = map.get(tempIdStr);
|
|
list.add(obj);
|
|
}else{
|
|
list = new JSONArray();
|
|
list.add(obj);
|
|
map.put(tempIdStr, list);
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
}
|