您需要将值从2015-06-16T04:35:00.000Z
UTC 解析为java.util.Date
SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
from.setTimeZone(TimeZone.getTimeZone("UTC"));
Date start_date_time = from.parse("2015-06-16T04:35:00.000Z");
这给了我们一个java.util.Date
(Tue Jun 16 14:35:00 EST 2015
对我来说)。
然后,您需要在 IST 中对其进行格式化
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
outFormat.setTimeZone(TimeZone.getTimeZone("IST"));
String formatted = outFormat.format(start_date_time);
System.out.println(formatted);
哪个输出20150616T100500
Java 8 时间 API
只是因为这是一个很好的练习...
// No Time Zone
String from = "2015-06-16T04:35:00.000Z";
LocalDateTime ldt = LocalDateTime.parse(from, DateTimeFormatter.ISO_ZONED_DATE_TIME);
// Convert it to UTC
ZonedDateTime zdtUTC = ZonedDateTime.of(ldt, ZoneId.systemDefault()).withZoneSameInstant(ZoneId.of("UTC"));
// Convert it to IST
ZonedDateTime zdtITC = zdtUTC.withZoneSameInstant(ZoneId.of("Indian/Cocos"));
String timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss").format(zdtITC);
System.out.println(timestamp);
nb:如果我没有将值解析为LocalDateTime
,则将其转换为UTC
,我已经过了一个小时,但我愿意了解更好的方法