public class FormatDateUtil {
private static Logger logger = LoggerFactory.getLogger(FormatDateUtil.class);
/**
* @desc 中国标准时间转换
* @param stringDate 中国标准时间
* @date 2018年12月4日 上午11:00:39
* @return String 返回String类型数据
*/
public static String getFormatTime(String stringDate) {
try {
stringDate = stringDate.replace("GMT+0800", "GMT+08:00");
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
Date date;
date = sdf.parse(stringDate);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
stringDate = sdf.format(date);
} catch (ParseException e) {
logger.error("转换{}时发生异常", stringDate);
}
return stringDate;
}
/**
* @desc 将 Date 类型日期转成 XMLGregorianCalendar 格式
* @param date 源日期
* @return XMLGregorianCalendar 转换之后的 XMLGregorianCalendar
* @date 2018年12月4日 上午11:00:39
*/
public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
XMLGregorianCalendar gc = null;
try {
gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
} catch (Exception e) {
logger.error("日期转换异常");
e.printStackTrace();
}
return gc;
}
/**
* @desc 将 Date format 成String
* @param date 源日期
* @return 格式化后的字符串
* @date 2018年12月4日 上午11:00:39
*/
public static String formatDateTime(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = null;
try {
dateStr = sdf.format(date);
} catch (Exception e) {
logger.error("转换异常");
e.printStackTrace();
}
return dateStr;
}
/**
* @desc 将 Date format 成String,格式yyyy-MM-dd
* @param date 源日期
* @date 2018年10月1日 下午1:48:45
* @return 格式化后的字符串
*/
public static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = null;
try {
dateStr = sdf.format(date);
} catch (Exception e) {
logger.error("转换异常");
e.printStackTrace();
}
return dateStr;
}
/**
* @desc 将 Date format 成String,格式yyyy-MM-dd
* @param stringDate 源日期字符串
* @date 2018年10月1日 下午1:48:45
* @return 格式化后的字符串
*/
public static Date formatDate(String stringDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(stringDate);
} catch (Exception e) {
logger.error("转换异常");
e.printStackTrace();
}
return date;
}
}