日期格式转换
@Test
public void calendarTest() {
Calendar calendar = Calendar.getInstance();
System.out.println("calendar.getTime :" + calendar.getTime());
System.out.println("calendar.getTime.getTime :" + calendar.getTime().getTime());
System.out.println("new Date :" + new Date());
System.out.println("calendar.getTimeZone :" + calendar.getTimeZone());
System.out.println("system.currentTimeMillis :" + System.currentTimeMillis());
try {
/** Sat Oct 12 2019 14:19:40 GMT 0800 (中国标准时间),该时间是格林尼治时间格式的时间*/
String dataTime = "Sat Oct 12 2019 14:19:40 GMT+0800 (中国标准时间)";
// 该pattern中的 E 标识星期,MMM标识月份
String data = dataTime.replace("GMT", "").replaceAll("\\(.*\\)", "");
// 将字符串转化为date类型,格式2016-10-12
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
System.out.println("中国标准时间(GMT): " + data);
Date dateTrans = format.parse(data);
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将其转化解析可的日期为:2019-10-12 14:19:40
String beijingTimeStr = formatDate.format(dateTrans);
System.out.println("北京时间 :" + beijingTimeStr);
dataTime = dataTime.substring(0, dataTime.indexOf('('));
data1 = dataTime.replace("GMT+08", "GMT+08:");
SimpleDateFormat format1 = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.US);
Date dateTrans1 = format1.parse(data1);
String beijingTimeStr1 = formatDate.format(dateTrans1);
System.out.println(dateTrans1);
System.out.println(beijingTimeStr1);
} catch (ParseException e) {
e.printStackTrace();
}
}
打印出来的结果:
calendar.getTime :Tue Sep 13 11:42:28 CST 2022
calendar.getTime.getTime :1663040548065
new Date :Tue Sep 13 11:42:28 CST 2022
calendar.getTimeZone :sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
system.currentTimeMillis :1663040548066
中国标准时间(GMT): Sat Oct 12 2019 14:19:40 +0800
北京时间 :2019-10-12 14:19:40
中国标准时间(GMT): Sat Oct 12 2019 14:19:40 GMT+08:00
Sat Oct 12 14:19:40 CST 2019
2019-10-12 14:19:40
有一种带T的时间格式 2020-01-01T21:30:03+08:00 和军事上带Z的格式 2019-10-14T13:41:45.223Z
参考:
https://www.cnblogs.com/zjdxr-up/p/12518209.html

浙公网安备 33010602011771号