package com.changhong.epc.parsing.service.loop.tools.impl;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.math.BigDecimal;
|
import java.text.DateFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
@Slf4j
|
public class YMDTools {
|
|
|
public static final DateFormat uid1 = new SimpleDateFormat("yyyyMMdd");
|
public static final DateFormat uid2 = new SimpleDateFormat("yyMMddHHmmssSS");
|
public static final DateFormat uid3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
public static final DateFormat uid4 = new SimpleDateFormat("yyyy-MM-dd");
|
public static final DateFormat uid5 = new SimpleDateFormat("HH:mm:ss");
|
public static final DateFormat uid6 = new SimpleDateFormat("yyyy-MM");
|
public static final DateFormat uid7 = new SimpleDateFormat("HH:mm");
|
|
/**
|
* [^0-9]
|
*/
|
public static final String NOT_NUMBER_REGEXP = "\\D";
|
|
/**
|
* 日期各位数量
|
* yyyy MM dd HH mm ss
|
*/
|
private static final String[] DATE_EACH_TYPE = {"%03d","%02d","%02d","%02d","%02d","%02d"};
|
|
/**
|
* 时间各位数量
|
* HH mm
|
*/
|
private static final String[] TIME_EACH_TYPE = {"%02d","%02d"};
|
|
/**
|
* 时间类型
|
*/
|
public static final String DATE_TYPE = "yyyyMMddHHmmss";
|
|
/**
|
* 时分类型
|
*/
|
public static final String TIME_TYPE = "HHmm";
|
|
/**
|
* 计算小时数
|
*/
|
public static final double HOUR_COUNT = 60D * 60D * 1000D;
|
|
/**
|
* 计算天数
|
*/
|
public static final double DAYS_COUNT = 24 * HOUR_COUNT;
|
|
private YMDTools(){
|
|
}
|
|
public static String dateYYYYMMDD(){
|
return uid1.format(Calendar.getInstance().getTime());
|
}
|
|
public static String currentTimestamp(){
|
return uid2.format(Calendar.getInstance().getTime());
|
}
|
|
public static String currentDateTime(){
|
return uid3.format(Calendar.getInstance().getTime());
|
}
|
|
public static String currentDate(){
|
return uid4.format(Calendar.getInstance().getTime());
|
}
|
public static String currentMonth(){
|
return uid6.format(Calendar.getInstance().getTime());
|
}
|
|
/**
|
*
|
* @Title: getDate
|
* @param @param date
|
* @param @return 设定文件
|
* @return Date 返回类型
|
* @throws
|
*/
|
public static Date getDate(String date){
|
try{
|
if(StringUtils.isBlank(date))
|
return null;
|
String formatDate = formatDate(date);
|
DateFormat df = new SimpleDateFormat(DATE_TYPE.substring(0, formatDate.length()));
|
return df.parse(formatDate);
|
}catch(Exception e){
|
log.error(e.getMessage(), e);
|
return null;
|
}
|
}
|
|
public static String getTime(String time){
|
if(StringUtils.isBlank(time)) {
|
return null;
|
}
|
try {
|
return formatTime(time);
|
} catch (Exception e) {
|
log.error(e.getMessage(), e);
|
}
|
return null;
|
}
|
|
/**
|
* 格式日期
|
* @Title: formatDate
|
* @param @param date
|
* @param @return 设定文件
|
* @return String 返回类型
|
* @throws
|
*/
|
public static String formatDate(String date) {
|
|
String[] dateEach = null;
|
try {
|
if(StringUtils.isBlank(date))
|
return "";
|
dateEach = date.split(NOT_NUMBER_REGEXP);
|
for (int i = 0; i < dateEach.length; i++) {
|
if(i == 0){
|
String year = dateEach[i];
|
if(year.length() < "yyyy".length()){
|
dateEach[i] = String.format(
|
String.format("%s%s", String.valueOf(getYearIndex1()), DATE_EACH_TYPE[i])
|
, Integer.parseInt(dateEach[i]));
|
}
|
}else{
|
dateEach[i] = String.format(DATE_EACH_TYPE[i], Integer.parseInt(dateEach[i]));
|
}
|
}
|
} catch (Exception e) {
|
}
|
return toString(dateEach);
|
}
|
|
public static String formatTime(String time){
|
if(StringUtils.isBlank(time))
|
return "";
|
String [] timeEach = time.split(NOT_NUMBER_REGEXP);
|
for (int i = 0; i < timeEach.length; i++) {
|
timeEach[i] = String.format(TIME_EACH_TYPE[i], Integer.parseInt(timeEach[i]));
|
}
|
return toString(timeEach);
|
}
|
|
private static char getYearIndex1(){
|
String date = uid6.format(new Date());
|
return date.toCharArray()[0];
|
}
|
|
private static String toString(String... strs){
|
if(strs == null){
|
return "";
|
}
|
StringBuilder sb = new StringBuilder();
|
for (String string : strs) {
|
sb.append(string);
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 该日期的前一天
|
* @Title: dateRightOneDays
|
* @param @param dateType
|
* @param @return 设定文件
|
* @return Date 返回类型
|
* @throws
|
*/
|
public static Date dateRightOneDays(String date){
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(getDate(date));
|
calendar.add(Calendar.DAY_OF_MONTH, -1);
|
return calendar.getTime();
|
}
|
|
public static String dateRightOneDays(String date, DateFormat df){
|
return df.format(dateRightOneDays(date));
|
}
|
|
/**
|
* 字符串转DATE类型
|
* @Title: strToDate
|
* @param @param str
|
* @param @return 设定文件
|
* @return Date 返回类型
|
* @throws
|
*/
|
public static Date strToDate(){
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
try {
|
return sdf.parse(YMDTools.currentDateTime());
|
} catch (Exception e) {
|
log.error(e.getMessage(), e);
|
}
|
return null;
|
}
|
|
/**
|
* date类型转到 字符串
|
* @Title: dateToStr
|
* @param @param sdf
|
* @param @param date
|
* @param @return 设定文件
|
* @return String 返回类型
|
* @throws
|
*/
|
public static String dateToStr(SimpleDateFormat sdf,Date date){
|
return sdf.format(date);
|
}
|
|
/**
|
* 获得天数
|
* @Title: getDays
|
* @param @param beginDate
|
* @param @param endDate
|
* @param @return 设定文件
|
* @return int 返回类型
|
* @throws
|
*/
|
public static double getDays(Date beginDate, Date endDate){
|
return count(beginDate, endDate, DAYS_COUNT);
|
}
|
|
/**
|
* 获得小时数
|
* @Title: getHour
|
* @param @param beginDate
|
* @param @param endDate
|
* @param @return 设定文件
|
* @return double 返回类型
|
* @throws
|
*/
|
public static double getHour(Date beginDate, Date endDate){
|
return count(beginDate, endDate, HOUR_COUNT);
|
}
|
|
/**
|
* 计算时间
|
* @Title: count
|
* @param @param beginDate
|
* @param @param endDate
|
* @param @param countType
|
* @param @return 设定文件
|
* @return double 返回类型
|
* @throws
|
*/
|
public static double count(Date beginDate, Date endDate, double countType){
|
if(beginDate == null || endDate == null)
|
return 0;
|
BigDecimal bd = BigDecimal.valueOf((endDate.getTime() - beginDate.getTime()) / countType);
|
return bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
}
|