通用型时间格式转换:
/*
*2020-05-31T13:20:00+08:00转成2020-05-3113:20:00 yyyy-MM-dd'T'HH:mm:ssXXX>>yyyy-MM-ddHH:mm:ss
*2020-12-1610:10:15转成2020-12-16T10:10:15 yyyy-MM-ddHH:mm:ss>>yyyy-MM-dd'T'HH:mm:ss
*2020-12-1610:10:15AM转成2020-12-16T10:10:15 yyyy-MM-ddh:mm:ssaa>>yyyy-MM-dd'T'HH:mm:ss
*2020-12-1610:10:15-->2020-12-1610:10AM yyyy-MM-ddHH:mm:ss>>yyyy-MM-ddh:mm:ssaa
*yyyy-MM-dd HH:mm:ss ——>EEE,dd MMM yyyy h:mm aa 2021-01-19 10:23:12——>Tue,19 Jan 2021 10:23 AM
**/
public static String dateTimeFormat(String oldFormatStr,String newFormateStr,String dateStr){
Date date = null;
try {
date = new SimpleDateFormat(oldFormatStr, Locale.ENGLISH).parse(dateStr);
} catch (ParseException e) {
}
SimpleDateFormat sf = new SimpleDateFormat(newFormateStr, Locale.ENGLISH);
String formatDate = sf.format(date);
return formatDate;
}
===============================================
测试例子:
//2020-12-16 10:10:15 --> 2020-12-16 10:10 AM
//yyyy-MM-dd HH:mm:ss --> yyyy-MM-dd h:mm aa
public static String dateTimeFormatAmPm(String formatStr,String dateStr){
Date date = DateUtil.parseDate("yyyy-MM-dd HH:mm:ss", dateStr);
SimpleDateFormat sf = new SimpleDateFormat(formatStr, Locale.ENGLISH);
String time = sf.format(date);
return time;
}
//2020-12-16 10:10:15 转成 2020-12-16T10:10:15
//yyyy-MM-dd HH:mm:ss ——> yyyy-MM-dd'T'HH:mm:ss
public static String formateToTdate(String dateStr){
Date date = DateUtil.parseDate("yyyy-MM-dd HH:mm:ss", dateStr);
String formatDate = DateUtil.formatDate1("yyyy-MM-dd'T'HH:mm:ss", date);
return formatDate;
}
// 2020-12-16T10:10:15 转成 2020-12-16 10:10:15
//yyyy-MM-dd'T'HH:mm:ss ——> yyyy-MM-dd HH:mm:ss
public static String fomateToNoTdate(String dateStr){
Date date = DateUtil.parseDate("yyyy-MM-dd'T'HH:mm:ss", dateStr);
String formatDate = DateUtil.formatDate1("yyyy-MM-dd HH:mm:ss", date);
return formatDate;
}
//2020-12-16 10:10 AM 转成 2020-12-16T10:10:00
//yyyy-MM-dd h:mm aa ——> yyyy-MM-dd'T'HH:mm:ss
public static String formateAmPmToTdate(String dateStr){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd h:mm aa", Locale.ENGLISH);
Date date = null;
try {
date = sf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
String formatDate = DateUtil.formatDate1("yyyy-MM-dd'T'HH:mm:ss", date);
return formatDate;
}
//2020-05-31T13:20:00+08:00 转成 2020-05-31 13:20:00
//yyyy-MM-dd'T'HH:mm:ssXXX ——> yyyy-MM-dd HH:mm:ss
public static String formateToUTCdate(String dateStr){
OrderIdGenerateUtil.dateTimeFormatUTCStr(dateStr);
Date date = DateUtil.parseDate("yyyy-MM-dd'T'HH:mm:ssXXX", dateStr);
String formatDate = DateUtil.formatDate1("yyyy-MM-dd HH:mm:ss", date);
return formatDate;
}