(1)带时区的时间类型 2021-12-16T14:05:09.202+08:00 转换成常用的年月日时分秒 2021-12-16 14:05:09
String oldDateStr = "2021-12-16T14:05:09.202+08:00"; try{ if(!oldDateStr.contains(".")){ oldDateStr = oldDateStr.replace("+",".00+"); } //防止不带毫秒的时间数据导致转换异常 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Date date = df.parse(oldDateStr); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String newDateStr = df2.format(date); System.out.println(newDateStr); }catch(Exception e){ throw new RuntimeException(e); }
(2)转换为带时区的时间类型
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>5.6.5</version> </dependency>
Date date = new Date(); String format = DateUtil.format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); System.out.println(format);