我有一个UTC中的localdatetime对象。我想转换为IST。
我怎样才能做到这一点?

LocalDateTime dateTimeOfAfterFiveDays = LocalDateTime.ofEpochSecond(after5,0,ZoneOffset.UTC);

最佳答案

从Java 8开始,日期/时间API非常易于使用。

在您的情况下:

// your local date/time with no timezone information
LocalDateTime localNow = LocalDateTime.now();
// setting UTC as the timezone
ZonedDateTime zonedUTC = localNow.atZone(ZoneId.of("UTC"));
// converting to IST
ZonedDateTime zonedIST = zonedUTC.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));


您会看到zonedUTCzonedIST之间的时间(可能还有日期)有所不同,反映了两者之间的时区偏移。

请注意此处withZoneSameInstant的用法,例如与withZoneSameLocal相反。

关于java - LocalDateTime到特定时区,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56361337/

10-12 03:52