1.日期时间工具类
public class DateUtils {
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss");
/**
* @Title: formatDateTime
* @Description: 格式化日期时间(yyyyMMddHHmmss)
* @param: [dateTime]
* @return: java.lang.String
* @throws
*/
public static String formatDateTime(Date dateTime) {
return DATE_TIME_FORMAT.format(dateTime);
}
/**
* @Title: formatDate
* @Description: 格式化日期(yyyyMMdd)
* @param: [date]
* @return: java.lang.String
* @throws
*/
public static String formatDate(Date date) {
return DATE_FORMAT.format(date);
}
/**
* @Title: formatTime
* @Description: 格式化时间(HHmmss)
* @param: [time]
* @return: java.lang.String
* @throws
*/
public static String formatTime(Date time) {
return TIME_FORMAT.format(time);
}
/**
* @Title: getCurrentTime
* @Description: 获取当前时间(HHmmss)
* @param: []
* @return: java.lang.String
* @throws
*/
public static String getCurrentTime() {
return TIME_FORMAT.format(System.currentTimeMillis());
}
/**
* @Title: getTodayDate
* @Description: 获取今天日期(yyyyMMdd)
* @param: []
* @return: java.lang.String
* @throws
*/
public static String getTodayDate() {
return DATE_FORMAT.format(System.currentTimeMillis());
}
/**
* @Title: getYesterdayDate
* @Description: 获取昨天日期(yyyyMMdd)
* @param: []
* @return: java.lang.String
* @throws
*/
public static String getYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date date = calendar.getTime();
return DATE_FORMAT.format(date);
}
/**
* @throws
* @Title: getYesterdayDate
* @Description: 获取下一天日期(yyyyMMdd)
* @param: []
* @return: java.lang.String
*/
public static String getNextDate(Date date, int nextDays) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date == null ? new Date() : date);
calendar.add(Calendar.DAY_OF_YEAR, nextDays);
Date nextDate = calendar.getTime();
return DATE_FORMAT.format(nextDate);
}
/**
* @throws
* @Title: before
* @Description: 判断一个时间是否在另一个时间之前
* @param: [dateTime1, dateTime2]
* @return: boolean
*/
public static boolean before(String dateTime1, String dateTime2) {
try {
Date dt1 = DATE_TIME_FORMAT.parse(dateTime1);
Date dt2 = DATE_TIME_FORMAT.parse(dateTime2);
if (dt1.before(dt2)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* @throws
* @Title: after
* @Description: 判断一个时间是否在另一个时间之后
* @param: [dateTime1, dateTime2]
* @return: boolean
*/
public static boolean after(String dateTime1, String dateTime2) {
try {
Date dt1 = DATE_TIME_FORMAT.parse(dateTime1);
Date dt2 = DATE_TIME_FORMAT.parse(dateTime2);
if (dt1.after(dt2)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* @throws
* @Title: parseDateTime
* @Description: 解析日期时间字符串
* @param: [dateTime]
* @return: java.util.Date
*/
public static Date parseDateTime(String dateTime) {
try {
return DATE_TIME_FORMAT.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* @throws
* @Title: parseDate
* @Description: 解析日期时间字符串
* @param: [dateTime]
* @return: java.util.Date
*/
public static Date parseDate(String dateTime) {
try {
return DATE_FORMAT.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2.字符串工具类
public class StringUtils {
/**
* @Title: isEmpty
* @Description: 判断字符串是否为空
* @param: [str]
* @return: boolean
* @throws
*/
public static boolean isEmpty(String str) {
return str == null || "".equals(str);
}
/**
* @Title: isNotEmpty
* @Description: 判断字符串是否不为空
* @param: [str]
* @return: boolean
* @throws
*/
public static boolean isNotEmpty(String str) {
return str != null && !"".equals(str);
}
/**
* @Title: replaceCharacter
* @Description: 正则替换特殊字符,例如:"\\s*|\t|\r|\n"
* @param: [str]
* @return: java.lang.String
* @throws
*/
public static String replaceCharacter(String str) {
String resultStr = "";
if (str != null && str.length() >0) {
Pattern pattern = Pattern.compile("\r|\n|\t");
Matcher matcher = pattern.matcher(str.trim());
resultStr = matcher.replaceAll("");
}
return resultStr;
}
}
3.数值工具类
4.Spring工具类
@Slf4j
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
log.info("ApplicationContext Set Success!", applicationContext);
}
}
/**
* @Title: getApplicationContext
* @Description: get ApplicationContext
* @param: []
* @return: org.springframework.context.ApplicationContext
* @throws
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* @Title: getBean
* @Description: 通过name获取Bean
* @param: [name]
* @return: java.lang.Object
* @throws
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* @Title: getBean
* @Description: 通过class获取Bean
* @param: [clazz]
* @return: T
* @throws
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* @Title: getBean
* @Description: 通过name、class获取Bean
* @param: [name, clazz]
* @return: T
* @throws
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
/**
* @Title: getProperty
* @Description: 通过name获取Property
* @param: [key]
* @return: java.lang.String
* @throws
*/
public static String getProperty(String key) {
return getApplicationContext().getEnvironment().getProperty(key);
}
}