场景:
选择需要将日期:2023-03-31 13:24:51 转换为 UTC零时区格式的日期数据
思路:
采用joda.time 日期处理工具类
代码:
@Test public void redd111(){ System.out.println(toTimeFormatZone0("2023-03-31 13:24:51")); } public String toTimeFormatZone0(String timeString) { if (StringUtils.isBlank(timeString)) {return "";} if (timeString.indexOf("T") >0 || timeString.indexOf("Z") >0) {return timeString;} //原数据本身就是零时区,直接返回 Date date = null; try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); date = formatter.parse(timeString, pos); SimpleDateFormat sdf8 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf8.setTimeZone(TimeZone.getTimeZone("GMT"));//设置时区为0区 timeString = sdf8.format(date); } catch (Exception e) { e.printStackTrace(); } return timeString; }
运行结果
2023-03-31T05:24:51Z