How to apply timezone when formatting DateTime?

限于喜欢 提交于 2019-12-12 05:16:11

问题


I have a datetime as 2011-01-11 01:51:10 and timezone as America/Los_Angeles

I want to get a localised date time for this value. This is what I do

val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("y-M-d H:m:s");
val m1: LocalDateTime = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
println("DateTime: " + m1.atZone(ZoneId.of("America/Los_Angeles")))

The value that I get is

DateTime: 2011-01-11T01:51:10-08:00[America/Los_Angeles]

How do I convert it into localized datetime with -08:00 offset applied to it and no [America/Los_Angeles]?


回答1:


You first have to specify which timezone that the time which you have parsed is in. Then specify an other one to convert into.

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("y-M-d H:m:s");
LocalDateTime m1 = LocalDateTime.parse("2011-01-11 01:51:10", formatter1);
ZonedDateTime z1 = m1.atZone(ZoneId.of("UTC"));
ZonedDateTime z2 = z1.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

System.out.println(z2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));



回答2:


Looks like you are using java.time API which has a ZonedDateTime. You should probably use it instead of LocalDateTime, since that LocalDateTime does not have a time zone. From the docs:

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

And then, ZonedDateTime docs states that:

A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

This class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by a ZoneOffset.

Using a ZonedDateTime, your code would be like:

import java.time._
import java.time.format._

val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)

The result you will see at the console will be:

zdt: java.time.ZonedDateTime = 2011-01-11T01:51:10-08:00[America/Los_Angeles]

That happens because you are using the default toString method of ZonedDateTime and looks like the DateTimeFormatter.ISO_OFFSET_DATE_TIME is exactly what you want. So your code should be:

import java.time._
import java.time.format._

val zoneId = ZoneId.of("America/Los_Angeles")
val formatter = DateTimeFormatter.ofPattern("y-M-d H:m:s").withZone(zoneId)
val zdt = ZonedDateTime.parse("2011-01-11 01:51:10", formatter)
val formatted: String = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)



回答3:


Please look into my complete answer for this. Answer

    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 ZoneId 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);



回答4:


probably what you want is to get UTC time and then apply timezone offset to it. It's quite easy to do with Joda time. For example:

DateTime.now().minus(timezoneOffset) 

where timezoneOffset is int that will represent time shift at your location. Correct me if I'm wrong.



来源:https://stackoverflow.com/questions/35688559/how-to-apply-timezone-when-formatting-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!