-2

I get the datetime content in the below string format with offset time value from the source system.

2019-02-16T10:00:00+08:00

Where i want to convert that into local date time using the offset value.I tried the below, but not getting the expected result.

DateTime date = new DateTime("2019-02-16T10:00:00+08:00");

-->output == 2019-02-16T02:00:00.000Z (the hour is decreased instead of increasing)

DateTime date = new DateTime("2019-02-16T10:00:00-08:00");

-->output == 2019-02-16T18:00:00.000Z (the hour is increased instead of decreasing).

is there any simple way to the expected output? Note: I am using Java 1.7

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
furisuri
  • 1
  • 1
  • Can't you use `java.time`, especially `LocalDateTime` and `OffsetDateTime`? – deHaar Mar 01 '19 at 12:48
  • Call the method that returns time as milliseconds since EPOCH, then apply your offset to the milliseconds and then convert the milliseconds back to a date. – SPlatten Mar 01 '19 at 12:49
  • 3
    `10:00` in zone `+08:00` is `02:00` in zone `Z (+00:00)`. So what is the problem? – TiiJ7 Mar 01 '19 at 12:50
  • 2
    Are you using Joda Time? – Jesper Mar 01 '19 at 12:52
  • 5
    That `+08:00` etc. is _not_ the offset to be added but the time zone's offset that _has already been added_, so as TiiJ7 already stated `10:00+08:00` is the same as `02:00+00:00` and `10:00-08:00` is the same as `18:00+00:00`. (`00:00` is the UTC offset which `Z` also refers to). – Thomas Mar 01 '19 at 12:53
  • When using Java 1.7, all other things being equal most would still prefer [ThreeTen Backport](https://www.threeten.org/threetenbp/) over Joda-Time. – Ole V.V. Mar 01 '19 at 13:20

2 Answers2

2

What you are doing is correct. To get the time in your local time zone:

    DateTime date = new DateTime("2019-02-16T10:00:00+08:00");
    DateTime dateTimeInLocalTimeZone = date.withZone(DateTimeZone.getDefault());
    System.out.println(dateTimeInLocalTimeZone);

On my computer in Europe/Copenhagen time zone I got

2019-02-16T03:00:00.000+01:00

As has been said in the comments, +08:00 is the offset that has already been added compared to UTC time. So your string denoted the same point in time as 2019-02-16T02:00:00+00:00. It may also be written as 2019-02-16T02:00:00Z since Z (pronounced “Zulu”) means UTC.

java.time and ThreeTen Backport

If you are not already tied to Joda-Time, you may prefer to use java.time, the modern Java date and time API. The code is similar:

    OffsetDateTime sourceDateTime = OffsetDateTime.parse("2019-02-16T10:00:00+08:00");
    ZonedDateTime dateTimeInLocalTimeZone = sourceDateTime.atZoneSameInstant(ZoneId.systemDefault());

2019-02-16T03:00+01:00[Europe/Copenhagen]

Question: Can I use java.time on Java 1.7?

Note: I am using Java 1.7

No big problem, java.time just requires at least Java 6. I have run the above code on jdk1.7.0_79.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
-1

Another way to do that :

String dt =  "2019-02-16T10:00:00+08:00";
ZonedDateTime zd = ZonedDateTime.parse("2019-02-16T10:00:00+08:00");
System.out.println(zd.toLocalDateTime().plusSeconds(zd.getOffset().getTotalSeconds()));

Output

2019-02-16T18:00
s4r4z1n
  • 7
  • 3