我有一个祖鲁时间戳,我必须转换成巴黎时区。
ZULU 2022-11-04T06:10:08.606+00:00 --> Paris 2022-11-04T07:10:08.606+01:00
并必须照顾DST,例如:夏季时间小时+2小时冬季时间小时+1小时
我已经编写了下面的代码,它在本地按预期工作,但在服务器(巴黎)上部署时不像预期的那样工作。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Locale;
public class ParisTime {
public static void main(String[] args) throws ParseException {
// String date = "2022-05-31T23:30:12.209+00:00";
String date = "2022-11-04T06:10:08.606+00:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime dateTime = dateFormat.parse(date).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
ZonedDateTime of = ZonedDateTime.of(dateTime, ZoneId.of("Europe/Paris"));
String hourDiff = of.toString().substring(of.toString().indexOf('+') + 1, of.toString().indexOf('+') + 3);
String zonedDateTime = of.plusHours(Integer.valueOf(hourDiff)).toString();
String newDatetime = zonedDateTime.substring(0, of.toString().indexOf('['));
System.out.println(newDatetime);
System.out.println(dateFormat.parse(newDatetime));
}
}
输出
2022-11-04T07:10:08.606+01:00
Fri Nov 04 07:10:08 IST 2022
发布于 2022-11-04 11:43:05
您可以使用普通的java.time
直接在区域和偏移之间切换,没有必要的遗留行李…。
下面是操作步骤:
您的输入示例是一个ISO格式的日期时间,它与UTC的偏移量(0小时和0分钟,因此在UTC中分别是祖鲁时间),这意味着您可以一次将其解析到java.time.OffsetDateTime
OffsetDateTime
,可以转换为ZonedDateTime
ZonedDateTime
可以处理夏令时(DST)
ZonedDateTime
可以切换其ZoneId
,这将尊重DST,但保留底层的即时<>f 216。请参见下面的示例…
public static void main(String[] args) {
// input example
String date = "2022-11-04T06:10:08.606+00:00";
// directly parse it to a java.time.OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(date);
// make the UTC/Zulu datetime zoned
ZonedDateTime zdt = odt.toZonedDateTime();
// print it
System.out.println(zdt);
// switch the zone to the desired one
ZonedDateTime zdtParis = zdt.withZoneSameInstant(ZoneId.of("Europe/Paris"));
// print that, too
System.out.println(zdtParis);
// or print a coversion to OffsetDateTime without explicitly mentioning the zone
System.out.println(zdtParis.toOffsetDateTime());
// the same can be achieved keeping the ZonedDateTime but formatting it as OffsetDateTime
System.out.println(zdtParis.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
以上代码的输出如下
2022-11-04T06:10:08.606Z
2022-11-04T07:10:08.606+01:00[Europe/Paris]
2022-11-04T07:10:08.606+01:00
2022-11-04T07:10:08.606+01:00
尝试使用受DST影响的日期时间,您将看到受DST尊重的…。这是使用输入值"2022-05-31T23:30:12.209+00:00"
的相同代码的输出。
2022-05-31T23:30:12.209Z
2022-06-01T01:30:12.209+02:00[Europe/Paris]
2022-06-01T01:30:12.209+02:00
2022-06-01T01:30:12.209+02:00
发布于 2022-11-04 12:39:38
您的字符串已经采用了类parse
中的静态方法java.time.Instant
所识别的格式。然后,您可以将该瞬间转换为巴黎时区中的ZonedDateTime
。两行代码:
String date = "2022-11-04T06:10:08.606+00:00";
java.time.Instant inst = java.time.Instant.parse(date);
java.time.ZonedDateTime zdt = inst.atZone(java.time.ZoneId.of("Europe/Paris"));
它还将考虑到夏季时间和非夏季时间的变化。
ZULU : 2022-11-04T06:10:08.606Z
Paris: 2022-11-04T07:10:08.606+01:00[Europe/Paris]
https://stackoverflow.com/questions/74316021
复制相似问题