package com.ibeetl.admin.console.util;
|
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.lang.Snowflake;
|
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
import cn.hutool.crypto.symmetric.SymmetricCrypto;
|
import com.ibeetl.admin.core.entity.CoreUser;
|
import com.ibeetl.admin.core.service.CorePlatformService;
|
import com.ibeetl.admin.core.util.enums.EvaluationLabelType;
|
import org.apache.poi.ss.formula.functions.T;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.beans.BeanInfo;
|
import java.beans.Introspector;
|
import java.beans.PropertyDescriptor;
|
import java.io.UnsupportedEncodingException;
|
import java.lang.reflect.Method;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.text.ParseException;
|
import java.text.ParsePosition;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@Service
|
public class DateUtil {
|
|
static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
|
static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
final static Base64.Encoder encoder = Base64.getEncoder();
|
static Base64.Decoder decoder = Base64.getDecoder();
|
|
|
public static CoreUser getCurrentUser(CorePlatformService platformService){
|
CoreUser user = platformService.getCurrentUser();
|
return user;
|
}
|
public static String getNickName(String nickName){
|
String text = null;
|
try {
|
text = new String(decoder.decode(nickName), "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return text;
|
}
|
/**
|
* java生成随机数字和字母组合10位数
|
* @param length
|
* @return
|
*/
|
public static String getRandomNickname(int length) {
|
String val = "";
|
Random random = new Random();
|
for (int i = 0; i < length; i++) {
|
// 输出字母还是数字
|
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
|
// 字符串
|
if ("char".equalsIgnoreCase(charOrNum)) {
|
// 取得大写字母还是小写字母
|
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
|
val += (char) (choice + random.nextInt(26));
|
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
|
val += String.valueOf(random.nextInt(10));
|
}
|
}
|
return val;
|
}
|
|
/**
|
* AES对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。
|
* 默认使用 :AES/ECB/PKCS5Padding
|
* @param salt 必须是16位的
|
* @param content 明文内容
|
* @return 加密后的密码
|
*/
|
public static String encrypt(String salt,String content){
|
//构建
|
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, salt.getBytes());
|
//加密
|
String encrypt = aes.encryptBase64(content);
|
return encrypt;
|
}
|
|
public static Date getDate(Date currentTime) {
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String dateString = formatter.format(currentTime);
|
ParsePosition pos = new ParsePosition(8);
|
Date currentTime_2 = formatter.parse(dateString, pos);
|
return currentTime_2;
|
}
|
|
public static List<Map<String,String>> query12WeekStartTimeAndEndTime(){
|
List<Map<String, Date>> list = new ArrayList<>();
|
for(int i = 0, h = 7; i <= 12; i++){
|
list.add(getLastWeek(h * i));
|
}
|
|
List<Map<String, String>> result = new ArrayList<>();
|
for(Map<String, Date> map : list){
|
Map<String,String> resultMap = new HashMap<>();
|
String startTime = sdf2.format(map.get("monday"))+" 00:00:00";
|
String endTime = sdf2.format(map.get("sunday"))+" 23:59:59";
|
resultMap.put("startTime",startTime);
|
resultMap.put("endTime",endTime);
|
result.add(resultMap);
|
}
|
return result;
|
}
|
public static Map<String, Date> getLastWeek(int days) {
|
// TODO Auto-generated method stub
|
Map<String, Date> map = new HashMap<String, Date>();
|
Calendar cal = Calendar.getInstance();
|
int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
if (n == 0) {
|
n = 7;
|
}
|
// 上周一的日期
|
cal.add(Calendar.DATE, -(days + (n - 1)));
|
Date monday = cal.getTime();
|
map.put("monday", monday);
|
|
cal.add(Calendar.DATE, 1);
|
Date tuesday = cal.getTime();
|
map.put("tuesday", tuesday);
|
|
cal.add(Calendar.DATE, 1);
|
Date wednesday = cal.getTime();
|
map.put("wednesday", wednesday);
|
|
cal.add(Calendar.DATE, 1);
|
Date thursday = cal.getTime();
|
map.put("thursday", thursday);
|
|
cal.add(Calendar.DATE, 1);
|
Date friday = cal.getTime();
|
map.put("friday", friday);
|
|
cal.add(Calendar.DATE, 1);
|
Date saturday = cal.getTime();
|
map.put("saturday", saturday);
|
|
cal.add(Calendar.DATE, 1);
|
Date sunday = cal.getTime();
|
map.put("sunday", sunday);
|
return map;
|
}
|
|
|
public static List<Map<String,String>> query12MonthStartTimeAndEndTime() {
|
List<Map<String,String>> list = new ArrayList<>();
|
String[] latest12Month = getLatest12Month();
|
for(String s : latest12Month){
|
Map<String, String> m = new HashMap<>();
|
m.put("startTime", s + "-01 00:00:00");
|
m.put("endTime", s + "-31 23:59:59");
|
list.add(m);
|
}
|
|
|
for(int i=0;i<list.size();i++){
|
for(int j=0;j<list.size()-i-1;j++){
|
if(list.get(j).get("startTime").compareTo(list.get(j+1).get("startTime"))<0){
|
Map<String,String> r=list.get(j);
|
list.set(j, list.get(j+1));
|
list.set(j+1, r);
|
}
|
}
|
}
|
return list;
|
}
|
|
public static String fillZero(int i){
|
String month = "";
|
if(i<10){
|
month = "0" + i;
|
}else{
|
month = String.valueOf(i);
|
}
|
return month;
|
}
|
|
public static String[] getLatest12Month(){
|
String[] latest12Months = new String[12];
|
Calendar cal = Calendar.getInstance();
|
//要先+1,才能把本月的算进去
|
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
|
for(int i=0; i<12; i++){
|
//逐次往前推1个月
|
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
|
latest12Months[11-i] = cal.get(Calendar.YEAR)+ "-" +fillZero(cal.get(Calendar.MONTH)+1);
|
}
|
return latest12Months;
|
}
|
|
|
|
public static List<Map<String,Object>> getMapList(List list,String entityType){
|
List<Map<String,Object>> maplist =new ArrayList<>();
|
for (Object t:list) {
|
Map<String,Object> map= convertBeanToMap(t);
|
maplist.add(map);
|
}
|
return maplist;
|
}
|
/**
|
* 实体类转map
|
* @param obj
|
* @return
|
*/
|
public static Map<String, Object> convertBeanToMap(Object obj) {
|
if (obj == null) {
|
return null;
|
}
|
Map<String, Object> map = new HashMap<String, Object>();
|
try {
|
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
|
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
for (PropertyDescriptor property : propertyDescriptors) {
|
String key = property.getName();
|
// 过滤class属性
|
if (!key.equals("class")) {
|
// 得到property对应的getter方法
|
Method getter = property.getReadMethod();
|
Object value = getter.invoke(obj);
|
if(null==value){
|
map.put(key,"");
|
}else{
|
map.put(key,value);
|
}
|
}
|
|
|
|
|
}
|
} catch (Exception e) {
|
System.out.println("convertBean2Map Error {实体转map}"+e);
|
}
|
return map;
|
}
|
|
/**
|
* 计算两个日期之间相差的天数
|
* @param smdate 较小的时间
|
* @param bdate 较大的时间
|
* @return 相差天数
|
* @throws ParseException
|
*/
|
public static int daysBetween(Date smdate,Date bdate) throws ParseException
|
{
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
smdate=sdf.parse(sdf.format(smdate));
|
bdate=sdf.parse(sdf.format(bdate));
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(smdate);
|
long time1 = cal.getTimeInMillis();
|
cal.setTime(bdate);
|
long time2 = cal.getTimeInMillis();
|
long between_days=(time2-time1)/(1000*3600*24);
|
|
return Integer.parseInt(String.valueOf(between_days));
|
}
|
|
/**
|
*字符串的日期格式的计算
|
*/
|
public static int daysBetween(String smdate,String bdate) throws ParseException{
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(sdf.parse(smdate));
|
long time1 = cal.getTimeInMillis();
|
cal.setTime(sdf.parse(bdate));
|
long time2 = cal.getTimeInMillis();
|
long between_days=(time2-time1)/(1000*3600*24);
|
|
return Integer.parseInt(String.valueOf(between_days));
|
}
|
|
public static boolean isSorted(List<String> listOfStrings, int index) {
|
if (index < 2) {
|
return true;
|
} else if (listOfStrings.get(index - 2).compareTo(listOfStrings.get(index - 1)) > 0) {
|
System.out.println(listOfStrings.get(index - 2)+"============"+listOfStrings.get(index - 1));
|
return false;
|
} else {
|
return isSorted(listOfStrings, index - 1);
|
}
|
}
|
|
public static int getCaseByType(String type){
|
if(EvaluationLabelType.ONE_TYPE.equals(type)){
|
return 1;
|
}else if(EvaluationLabelType.TWO_TYPE.equals(type)){
|
return 2;
|
}else if(EvaluationLabelType.THREE_TYPE.equals(type)){
|
return 3;
|
}else if(EvaluationLabelType.FOUR_TYPE.equals(type)){
|
return 4;
|
}else if(EvaluationLabelType.FIVE_TYPE.equals(type)){
|
return 5;
|
}else if(EvaluationLabelType.SIX_TYPE.equals(type)){
|
return 6;
|
}else if(EvaluationLabelType.SENVEN_TYPE.equals(type)){
|
return 7;
|
}else if(EvaluationLabelType.EIGHT_TYPE.equals(type)){
|
return 8;
|
}else if(EvaluationLabelType.NINE_TYPE.equals(type)){
|
return 9;
|
}else if(EvaluationLabelType.TEN_TYPE.equals(type)){
|
return 10;
|
}else if(EvaluationLabelType.ELE_TYPE.equals(type)){
|
return 11;
|
}else if(EvaluationLabelType.TELF_TYPE.equals(type)){
|
return 12;
|
}else if(EvaluationLabelType.THR_TYPE.equals(type)){
|
return 13;
|
}else if(EvaluationLabelType.FOR_TYPE.equals(type)){
|
return 14;
|
}else if(EvaluationLabelType.FIV_TYPE.equals(type)){
|
return 15;
|
}else if(EvaluationLabelType.SI_TYPE.equals(type)){
|
return 16;
|
}else if(EvaluationLabelType.SEV_TYPE.equals(type)){
|
return 17;
|
}else if(EvaluationLabelType.EIG_TYPE.equals(type)){
|
return 18;
|
}else if(EvaluationLabelType.NIG_TYPE.equals(type)){
|
return 19;
|
}
|
return 0;
|
}
|
|
public static boolean isBase64(String str) {
|
if (str == null || str.trim().length() == 0) {
|
return false;
|
} else {
|
if (str.length() % 4 != 0) {
|
return false;
|
}
|
|
char[] strChars = str.toCharArray();
|
for (char c:strChars) {
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
|| c == '+' || c == '/' || c == '=') {
|
continue;
|
} else {
|
return false;
|
}
|
}
|
return true;
|
}
|
}
|
|
public static String generate(String prefix,long datacenterId){
|
Snowflake snowflake = IdUtil.createSnowflake(Convert.toLong(0), datacenterId);
|
return prefix+snowflake.nextIdStr();
|
}
|
|
/* public static void main(String[] args) {
|
String now = sdf1.format(new Date());
|
System.out.println(now);
|
int b = checkBig(now,"2019-12-31 14:00:00");
|
System.out.println(b);
|
}*/
|
/**
|
*
|
* @param s1 时间字符串 注意时间格式要一样哦,不一样你比个毛线
|
* @param s2 时间字符串
|
* @return 1是s1大 2是s2大 0是俩个一样大 -1是出错了吧
|
*/
|
public static int checkBig(String s1,String s2){
|
try {
|
if(s1.compareTo(s2) >0){
|
return 1;
|
}else if(s1.compareTo(s2) <0){
|
return 2;
|
}else{
|
return 0;
|
}
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
return -1;
|
}
|
}
|
|
public static <T> List<T> castList(Object obj, Class<T> clazz)
|
{
|
List<T> result = new ArrayList<T>();
|
if(obj instanceof List<?>)
|
{
|
for (Object o : (List<?>) obj)
|
{
|
result.add(clazz.cast(o));
|
}
|
return result;
|
}
|
return null;
|
}
|
|
public static String dayForWeek(String pTime) throws Throwable {
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
Date tmpDate = format.parse(pTime);
|
|
Calendar cal = Calendar.getInstance();
|
|
String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
|
|
try {
|
|
cal.setTime(tmpDate);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
|
|
if (w < 0){
|
w = 0;
|
}
|
return weekDays[w];
|
|
}
|
|
public static int getDayHour() throws Throwable {
|
Calendar cal=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
|
int hour=cal.get(Calendar.HOUR_OF_DAY);
|
return hour;
|
}
|
|
}
|