174 lines
4.7 KiB
Java
174 lines
4.7 KiB
Java
package com.wms.utils;
|
||
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.hibernate.validator.constraints.Length;
|
||
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* WMS工具类
|
||
* @author 梁州
|
||
* @date 2023/2/13
|
||
*/
|
||
public class WmsUtils {
|
||
// 日期格式
|
||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||
// 时间格式
|
||
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
// id用的时间格式
|
||
private static final SimpleDateFormat idTimeFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
||
private static volatile Long lastTimestamp = -1L;
|
||
/**
|
||
* 生成唯一的编号
|
||
* @param code 插入字符串
|
||
* @return 唯一id
|
||
*/
|
||
public static String generateId(String code) {
|
||
return code + generateUUIDString();
|
||
}
|
||
|
||
/**
|
||
* 生成UUID
|
||
*/
|
||
public synchronized static String generateUUIDString() {
|
||
long timeStamp = System.currentTimeMillis();
|
||
if (timeStamp == lastTimestamp) {
|
||
try {
|
||
Thread.sleep(1);
|
||
} catch (InterruptedException e){
|
||
timeStamp = System.currentTimeMillis();
|
||
lastTimestamp = timeStamp;
|
||
return idTimeFormat.format(new Date()).concat(String.valueOf(timeStamp));
|
||
}
|
||
timeStamp = System.currentTimeMillis();
|
||
lastTimestamp = timeStamp;
|
||
return idTimeFormat.format(new Date()).concat(String.valueOf(timeStamp));
|
||
} else {
|
||
lastTimestamp = timeStamp;
|
||
return idTimeFormat.format(new Date()).concat(String.valueOf(timeStamp));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 时间字符串转时间格式
|
||
* @param time 时间
|
||
*/
|
||
public static Date timeStringToTime(String time) throws ParseException {
|
||
return timeFormat.parse(time);
|
||
}
|
||
|
||
/**
|
||
* 日期字符串
|
||
* @param date 日期
|
||
*/
|
||
public static Date dateStringToDate(String date) throws ParseException {
|
||
return dateFormat.parse(date);
|
||
}
|
||
|
||
/**
|
||
* 生成时间戳,格式yyyy-MM-dd HH:mm:ss
|
||
* @return 时间戳
|
||
*/
|
||
public static String createCurrentTimeString() {
|
||
return timeFormat.format(new Date());
|
||
}
|
||
|
||
/**
|
||
* 生成日期字符串,格式yyyy-MM-dd
|
||
* @return 时间戳
|
||
*/
|
||
public static String createCurrentDateString() {
|
||
return dateFormat.format(new Date());
|
||
}
|
||
|
||
/**
|
||
* 格式化日期,格式yyyy-MM-dd
|
||
* @param date 日期
|
||
* @return 时间戳
|
||
*/
|
||
public static String formatDateString(Date date) {
|
||
return dateFormat.format(date);
|
||
}
|
||
|
||
/**
|
||
* 格式化时间,格式yyyy-MM-dd HH:mm:ss
|
||
* @param date 日期
|
||
* @return 时间戳
|
||
*/
|
||
public static String formatTimeString(Date date) {
|
||
return timeFormat.format(date);
|
||
}
|
||
|
||
/**
|
||
* 获得指定日期之后指定天数的日期
|
||
* @param beginDay 开始日期
|
||
* @param days 天数
|
||
* @return 日期
|
||
*/
|
||
public static Date calculationDate(Date beginDay, int days){
|
||
Calendar calendar = Calendar.getInstance();
|
||
calendar.setTime(beginDay);
|
||
calendar.add(Calendar.DATE, days);
|
||
return calendar.getTime();
|
||
}
|
||
|
||
/**
|
||
* 获得指定日期之后指定月数的日期
|
||
* @param beginDay 开始日期
|
||
* @param months 月数
|
||
* @return 日期
|
||
*/
|
||
public static Date calculationMonth(Date beginDay, int months){
|
||
Calendar calendar = Calendar.getInstance();
|
||
calendar.setTime(beginDay);
|
||
calendar.add(Calendar.MONTH, months);
|
||
return calendar.getTime();
|
||
}
|
||
|
||
/**
|
||
* 获得指定日期之后指定年数的日期
|
||
* @param beginDay 开始日期
|
||
* @param years 年数
|
||
* @return 日期
|
||
*/
|
||
public static Date calculationYear(Date beginDay, int years){
|
||
Calendar calendar = Calendar.getInstance();
|
||
calendar.setTime(beginDay);
|
||
calendar.add(Calendar.YEAR, years);
|
||
return calendar.getTime();
|
||
}
|
||
|
||
/**
|
||
* 获取指定天数后的日期
|
||
* @param day 天数
|
||
* @return 日期
|
||
*/
|
||
public static Date getDay(int day){
|
||
return calculationDate(new Date(), day);
|
||
}
|
||
|
||
/**
|
||
* 获取任意月后的时间
|
||
* @Params: mon 1表示后一个月 -1表示前一个月
|
||
* @Return
|
||
*/
|
||
public static Date getMon(int mon){
|
||
return calculationMonth(new Date(), mon);
|
||
}
|
||
|
||
/**
|
||
* 获取任意年之后的日期
|
||
* @param year 年
|
||
* @return 日期
|
||
*/
|
||
public static Date getYear(int year){
|
||
return calculationYear(new Date(), year);
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|