public class DateUtil {
private final static Logger log = LoggerFactory.getLogger(FvLogType.FV_COMMON.logName);
private static final String DATE_FORMATE_14 = "yyyyMMddHHmmss";
private static final String TIME_FORMATE_6 = "HHmmss";
private static final String DATE_FORMATE_FULL = "yyyyMMddHHmmssSSS";
private static final String DATE_FORMATE_8 = "yyyyMMdd";
private static final String DATE_FULL_FORMATE_14 = "yyyyMMdd HH:mm:ss";
private static final String REG_DATE_FORMATE_8 = "^([1-2]\\d{3})((0[1-9])|10|11|12)([1-2][0-9]|0[1-9]|30|31)$";//yyyymmdd
public static String getCurrentFullDatetime() {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FULL_FORMATE_14);
return sdDateTime.format(new Date());
}
/**
* 获取当前日期时间
*
* @return
*/
public static String getCurrentDatetime() {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FORMATE_14);
return sdDateTime.format(new Date());
}
/**
* 获取当前时间
*
* @return
*/
public static String getCurrentTime() {
SimpleDateFormat sdDateTime = new SimpleDateFormat(TIME_FORMATE_6);
return sdDateTime.format(new Date());
}
/**
* 获取当前日期时间
*
* @return
*/
public static String getCurrentUTC() {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FORMATE_FULL);
return sdDateTime.format(new Date());
}
/**
* 获取当前日期
*
* @return
*/
public static String getCurrentDate() {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FORMATE_8);
return sdDateTime.format(new Date());
}
/**
* 转换时间格式
* 1、时间格式为:yyyyMMddHHmmss的字符串转换成Date型
* 2、转换失败,返回当前日期
*
* @param date
* @return
*/
public static Date convertStringToDateTime(String date) {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FORMATE_14);
Date newdate = new Date();
try {
newdate = sdDateTime.parse(date);
} catch (Exception e) {
}
return newdate;
}
/**
* 转换时间格式
* 1、时间格式为:yyyyMMdd的字符串转换成Date型
* 2、转换失败,返回当前日期
*
* @param date
* @return
*/
public static Date convertStringToDate(String date) {
SimpleDateFormat sdDateTime = new SimpleDateFormat(DATE_FORMATE_8);
Date newdate = new Date();
try {
newdate = sdDateTime.parse(date);
} catch (Exception e) {
}
return newdate;
}
/**
* 转换时间对象
* 参数:
* 时间对象 date、时间格式:yyyyMMdd
*
* @param date
* @return
*/
public static String convertDateToString(Date date) {
return DateFormatUtils.format(date, DATE_FORMATE_8);
}
/**
* 转换时间对象
* 参数:
* 时间对象 date、时间格式:format(yyyyMMdd...)
*
* @param date
* @return
*/
public static String convertDateToString(Date date, String format) {
return DateFormatUtils.format(date, StringUtils.trimToNull(format));
}
/**
* 转换时间对象
* 参数:
* 时间对象 date、时间格式:format(yyyyMMdd->...)
*
* @param date
* @return
* @throws ParseException
*/
public static String convertDateStringToDateTimeString(String date, String targetPatten, String sourcePatten) throws ParseException {
SimpleDateFormat sourceDateTime = new SimpleDateFormat(sourcePatten);
SimpleDateFormat tagetDateTime = new SimpleDateFormat(targetPatten);
return tagetDateTime.format(sourceDateTime.parse(date));
}
/**
* 截取日期时间字符串的日期字符串
* 时间格式:yyyyMMdd
*
* @param dateTimeString
* @return
*/
public static String convertDateString8(String dateTimeString) {
dateTimeString = dateTimeString.replaceAll("-", "");
dateTimeString = dateTimeString.replaceAll(":", "");
dateTimeString = dateTimeString.replaceAll(" ", "");
return dateTimeString.substring(0, 8);
}
public static String convertToDateTimeString14(String dateTimeString) {
dateTimeString = dateTimeString.replaceAll("-", "");
dateTimeString = dateTimeString.replaceAll(":", "");
dateTimeString = dateTimeString.replaceAll(" ", "");
return dateTimeString;
}
/**
* 截取日期时间字符串的日期字符串
* 时间格式:yyyyMMddHHmmss
*
* @param dateTimeString
* @return
*/
public static String convertDateStringToDateTimeString(String dateString) {
return dateString+"000000";
}
/**
* 八位数字类型日期转化为十位
* 20131109转换为2013-11-09
* @param numDate
* @return
*/
public static String getRodDate(String numDate) {
numDate = convertToDateTimeString14(StringUtils.trimToEmpty(numDate));
if (numDate.isEmpty() || numDate.length() != 8) {
return "0000-00-00";
}
StringBuffer sb = new StringBuffer();
sb.append(numDate.substring(0, 4));
sb.append("-");
sb.append(numDate.substring(4, 6));
sb.append("-");
sb.append(numDate.substring(6, 8));
return sb.toString();
}
/**
* String(yyyyMMddHHmmss) -> Date
*
* @param dateTime
* 时间字符串(yyyyMMddHHmmss)
* @return
*/
public static Date parseBackendDateTime(String dateTime) {
if (dateTime == null) {
return null;
}
SimpleDateFormat FORMAT_DATETIME_BACKEND = new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date date=FORMAT_DATETIME_BACKEND.parse(dateTime);
return date;
} catch (ParseException e) {
throw new RuntimeException("将字符串" + dateTime + "解析为" + FORMAT_DATETIME_BACKEND.toPattern() + "格式的日期时发生异常:", e);
}
}
/**
* 校验YYYYMMDD日期格式
* @param date
* @return
*/
public static boolean isFormatYYYYMMDD(String date){
Pattern pattern = Pattern.compile(REG_DATE_FORMATE_8);
Matcher matcher = pattern.matcher(date);
return matcher.matches();
}
/**
* 计算两个日期间相隔天数
* @param startDate
* @param endDate
* @return
*/
public static int daysBetween(String startDate,String endDate){
int days = 0;
try {
SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMATE_8);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(startDate));
long startTime = cal.getTimeInMillis();
cal.setTime(sdf.parse(endDate));
long endTime = cal.getTimeInMillis();
long betweenDays=(endTime-startTime)/(1000*3600*24);
days = Integer.parseInt(String.valueOf(betweenDays));
} catch (NumberFormatException e){
log.error("daysBetween(),NumberFormatException", e); // Compliant
} catch (ParseException e) {
log.error("daysBetween(),ParseException", e); // Compliant
}
return days;
}
//============================4.时间加减=====================================
/**
* 为一个日期加上指定年数
* @param aDate
* @param amount 年数
* @return
*/
public static final Date addYears(Date aDate, int amount) {
return addTime(aDate,Calendar.YEAR,amount);
}
/**
* 为一个日期加上指定月数
* @param aDate
* @param amount 月数
* @return
*/
public static final Date addMonths(Date aDate, int amount) {
return addTime(aDate,Calendar.MONTH,amount);
}
/**
* 为一个日期加上指定天数
* @param aDate
* @param amount 天数
* @return
*/
public static final Date addDays(Date aDate, int amount) {
return addTime(aDate,Calendar.DAY_OF_YEAR,amount);
}
/**
* 为一个String类型日期加上指定天数并返回yyyyMMdd字符串
* @param aDate
* @param amount 天数
* @return
*/
public static final String addDaysForString(String aDate, int amount) {
return convertDateToString(addDays(convertStringToDate(aDate), amount));
}
private static final Date addTime(Date aDate,int timeType,int amount){
if (aDate == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
cal.add(timeType, amount);
return cal.getTime();
}
/**
* 获取当前日期类型时间
*/
public static Date getNow(){
return new Date();
}
public static void main(String[] args) {
String a = addDaysForString("20170112",-1);
System.out.println(a);
}
}