0

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]?

softarn
  • 5,327
  • 3
  • 40
  • 54
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • I'm not sure I understand. A `LocalDateTime` doesn't have the concept of timezone. – Tunaki Feb 28 '16 at 21:34
  • sure, I would like the time to move back `8 hours` by applying offset – daydreamer Feb 28 '16 at 21:36
  • What is it that you're trying to convert *to*? Some other time zone? UTC? As currently stated, you're creating a `ZonedDateTime` that is the local time and time zone you provided. (1:51:10 AM in Pacific time), You don't do anything with it from there. You probably want `.toInstant()`, or `.withZoneSameInstant(someOtherZone)`. – Matt Johnson-Pint Feb 28 '16 at 21:48
  • The term "localized" means changing the language and is not related to timezones, but I don't see how is your pattern locale-sensitive nor does it contain any timezone informations. Do you mean by "localized" the display in another timezone with offset -08:00 keeping the same instant? This is quite different and has nothing to do with i18n. – Meno Hochschild Feb 29 '16 at 02:23

4 Answers4

4

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));
softarn
  • 5,327
  • 3
  • 40
  • 54
3

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)
marcospereira
  • 12,045
  • 3
  • 46
  • 52
0

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);
Neero
  • 226
  • 2
  • 7
  • 23
-1

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.

Artemis
  • 4,821
  • 3
  • 21
  • 24
  • 3
    Time Zone != Offset. You wouldn't know the offset in advance, and subtracting it is the wrong approach anyway. Also, the OP is already showing use of Java 8 time API usage, so Joda Time is not appropriate. Save that for Java 7 and lower. – Matt Johnson-Pint Feb 28 '16 at 21:43