public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static final String ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final String COMMON_DATE = "EEE,d MMM yyyy HH:mm:ss 'GMT'";
/**
* 时间戳转换某时区的日期格式字符串
* @param dateLong 当前应用的时间戳
* @param zone Asia/Shanghai
* UTC
* GMT
* GMT+08:00
* @return
*/
public static Map<String, Object> getTimeByZone(Long dateLong, String zone) {
Date date = new Date(dateLong);
Map<String, Object> map = new HashMap<>();
SimpleDateFormat formatterISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
formatterISO8601.setTimeZone(TimeZone.getTimeZone(zone));
String formatISO8601 = formatterISO8601.format(date);
map.put("formatISO8601", formatISO8601);
SimpleDateFormat formatterGMT = new SimpleDateFormat("EEE,d MMM yyyy HH:mm:ss 'GMT'");
formatterGMT.setTimeZone(TimeZone.getTimeZone(zone));
String formatGMT = formatterGMT.format(date);
map.put("formatGMT", formatGMT);
return map;
}
/**
* 某时区日期格式字符串转换时间戳
* @param dateStr 2022-06-28T07:36:11.359Z
* 2022-06-28 15:36:11
* @param format yyyy-MM-dd'T'HH:mm:ss.SSSXXX(ISO8601)
* yyyy-MM-dd HH:mm:ss(GMT)
* @param zone Asia/Shanghai
* UTC
* GMT
* GMT+08:00
* @return
*/
public static Long StringToDateLong(String dateStr, String format, String zone) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
formatter.setTimeZone(TimeZone.getTimeZone(zone));
Date date = null;
try {
date = formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
}
java 不同时区转换
最新推荐文章于 2022-11-18 17:25:53 发布