Java 是否将ZoneDateTime设置为UTC并应用偏移量?
我正在使用Java 是否将ZoneDateTime设置为UTC并应用偏移量?,java,datetime,java-8,utc,zoneddatetime,Java,Datetime,Java 8,Utc,Zoneddatetime,我正在使用Java8 这就是我的ZoneDateTime的样子 2013-07-10T02:52:49+12:00 我得到这个值作为 z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) 其中z1是一个zoneDateTime 我想将此值转换为2013-07-10T14:52:49 我怎么做?这是你想要的吗? 通过将ZonedDateTime转换为Instant之前的ZonedDateTime,可以将您的ZonedDateTime转换为具有给定Z
Java8
这就是我的
ZoneDateTime
的样子
2013-07-10T02:52:49+12:00
我得到这个值作为
z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
其中z1
是一个zoneDateTime
我想将此值转换为2013-07-10T14:52:49
我怎么做?这是你想要的吗?
通过将ZonedDateTime
转换为Instant
之前的ZonedDateTime,可以将您的ZonedDateTime
转换为具有给定ZoneId
的LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC);
或者,您可能希望用户使用系统时区,而不是硬编码的UTC:
LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());
@SimMac感谢您的澄清。我也面临同样的问题,并且能够根据他的建议找到答案
public static void main(String[] args) {
try {
String dateTime = "MM/dd/yyyy HH:mm:ss";
String date = "09/17/2017 20:53:31";
Integer gmtPSTOffset = -8;
ZoneOffset offset = ZoneOffset.ofHours(gmtPSTOffset);
// String to LocalDateTime
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(dateTime));
// Set the generated LocalDateTime's TimeZone. In this case I set it to UTC
ZonedDateTime ldtUTC = ldt.atZone(ZoneOffset.UTC);
System.out.println("UTC time with Timezone : "+ldtUTC);
// Convert above UTC to PST. You can pass ZoneOffset or Zone for 2nd parameter
LocalDateTime ldtPST = LocalDateTime.ofInstant(ldtUTC.toInstant(), offset);
System.out.println("PST time without offset : "+ldtPST);
// If you want UTC time with timezone
ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
ZonedDateTime zdtPST = ldtUTC.toLocalDateTime().atZone(zoneId);
System.out.println("PST time with Offset and TimeZone : "+zdtPST);
} catch (Exception e) {
}
}
输出:
UTC time with Timezone : 2017-09-17T20:53:31Z
PST time without offset : 2017-09-17T12:53:31
PST time with Offset and TimeZone : 2017-09-17T20:53:31-08:00[America/Los_Angeles]
在将其发送到格式化程序之前,似乎需要将其转换为所需的时区(UTC)
z1.withZoneSameInstant( ZoneId.of("UTC") )
.format( DateTimeFormatter.ISO_OFFSET_DATE_TIME )
如果z1
是ZonedDateTime
的一个实例,那么表达式
z1.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()
计算为具有OP请求的字符串表示形式的LocalDateTime
实例。以下程序对此进行了说明:
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
ZonedDateTime time = ZonedDateTime.now();
ZonedDateTime truncatedTime = time.truncatedTo(ChronoUnit.SECONDS);
ZonedDateTime truncatedTimeUtc = trucatedTime.withZoneSameInstant(ZoneOffset.UTC);
LocalDateTime truncatedTimeUtcNoZone = truncatedTimeUtc.toLocalDateTime();
System.out.println(time);
System.out.println(trucatedTime);
System.out.println(truncatedTimeUtc);
System.out.println(truncatedTimeUtcNoZone);
}
}
以下是一个示例输出:
2020-10-26T16:45:21.735836-03:00[America/Sao_Paulo]
2020-10-26T16:45:21-03:00[America/Sao_Paulo]
2020-10-26T19:45:21Z
2020-10-26T19:45:21
我仍然不明白:D。它和其他的有什么不同?“转换为2013-07-10T14:52:49
”是什么意思?因此您希望在未来12小时内使用相同的分区偏移量?但在您想要的输出中,不再有分区偏移量。另外,如果偏移量是半小时(它存在…)怎么办?如果删除分区信息,这不再是分区日期时间…我不明白。考虑到你的输入和偏移量,输出不是应该提前12小时吗。。。我觉得很好。。。您可以尝试使用其他浏览器检查是否存在相同问题。无需硬编码UTC
,您只需使用预定义的区域偏移量:ZoneOffset.UTC
谢谢您的提示@Andriabramov,我编辑了我的答案!这对我很有用:LocalDateTime _2018_3_13=LocalDateTime.of(2018,Month.MARCH,13,0,0);ZonedDateTime z1=ZonedDateTime.of(_2018_3_13,ZoneOffset.UTC);字符串last=z1.withZoneSameInstant(ZoneId.of(“欧洲/巴黎”)).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);