public static void main(String args[]) throws ParseException {标签:UTC,ss,dd,yyyy,MM,HH,date,mm From: https://www.cnblogs.com/liang2023/p/17099355.html
castUTC("2023-02-07T12:16:03.944Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
}
public static void castUTC(String UTCStr, String format) throws ParseException {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(UTCStr);
System.out.println("UTC时间转换+8前: " + date);
date = hourBefore2Object(8, "", date);
System.out.println("UTC时间转换+8后: " + date);
System.out.println("UTC时间转换+8后格式话: " + formatDate(date,"yyyy-MM-dd HH:mm:ss"));
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, String pattern) {
if (date == null || StringUtils.isBlank(pattern)) {
return null;
}
return DateFormatUtils.format(date, pattern, Locale.SIMPLIFIED_CHINESE);
}
/**
* 相对于当前日期的前几小时钟
*
* @param hours
* @param format
* @param instance
*/
public static <T> T hourBefore2Object(int hours, String format, T instance) {
Calendar calendar = Calendar.getInstance();
if (format == null || format.equals("")) {
format = "yyyy-MM-dd HH:mm:ss";
}
if (instance instanceof Date) {
calendar.setTime((Date) instance);
calendar.add(Calendar.HOUR, hours);// 增加x小时
Date date = calendar.getTime();
return (T) date;
}
return null;
}
/**
* 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
return parseDate(str.toString(), "yyyy-MM-dd HH:mm:ss");
}
public static Date parseDate(Object str, String parsePattern) {
if (str == null) {
return null;
}
if (StringUtils.isBlank(parsePattern)) {
parsePattern = "yyyy-MM-dd";
}
try {
return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePattern);
} catch (ParseException e) {
return null;
}
}