23

I am using Java 8
This is what my ZonedDateTime looks like

2013-07-10T02:52:49+12:00

I get this value as

z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

where z1 is a ZonedDateTime.

I wanted to convert this value as 2013-07-10T14:52:49

How can I do that?

daydreamer
  • 87,243
  • 191
  • 450
  • 722

5 Answers5

24

It looks like you need to convert to the desired time zone (UTC) before sending it to the formatter.

z1.withZoneSameInstant( ZoneId.of("UTC") )
  .format( DateTimeFormatter.ISO_OFFSET_DATE_TIME )

should give you something like 2018-08-28T17:41:38.213Z

DaveTPhD
  • 241
  • 2
  • 2
  • 1
    This worked for me: LocalDateTime _2018_3_13 = LocalDateTime.of(2018, Month.MARCH, 13, 0, 0); ZonedDateTime z1 = ZonedDateTime.of(_2018_3_13, ZoneOffset.UTC); String last = z1.withZoneSameInstant(ZoneId.of("Europe/Paris")) .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); – rjdkolb Dec 20 '18 at 07:37
23

Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before.

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC);

Or maybe you want the users system-timezone instead of hardcoded UTC:

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());
SimMac
  • 479
  • 5
  • 16
5

@SimMac Thanks for the clarity. I also faced the same issue and able to find the answer based on his suggestion.

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) {
    }
}

Output:

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]
Neero
  • 226
  • 2
  • 7
  • 23
2

If z1 is an instance of ZonedDateTime, then the expression

z1.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()

evaluates to an instance of LocalDateTime with the string representation requested by the OP. This is illustrated by the following program:

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 = truncatedTime.withZoneSameInstant(ZoneOffset.UTC);
    LocalDateTime truncatedTimeUtcNoZone = truncatedTimeUtc.toLocalDateTime();

    System.out.println(time);
    System.out.println(truncatedTime);
    System.out.println(truncatedTimeUtc);
    System.out.println(truncatedTimeUtcNoZone);
  }
}

Sample output:

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
Bram
  • 16
  • 4
Lucas Braune
  • 249
  • 4
  • 5
1

I wanted to convert this value as 2013-07-10T14:52:49

2013-07-10T02:52:49+12:00 ≠ 2013-07-10T14:52:49 at UTC

2013-07-10T02:52:49+12:00 = 2013-06-09T14:52:49 at UTC (which is obtained by subtracting 12:00 hours of the offset from 2013-07-10T02:52:49).

Demo:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

class Main {
    public static void main(String[] args) {
        ZoneOffset zoneOffset = ZoneOffset.of("+12:00");
        OffsetDateTime odtGiven = OffsetDateTime.of(LocalDateTime.of(2013, 7, 10, 2, 52, 49), zoneOffset);
        System.out.println(odtGiven);

        OffsetDateTime odtUtc = odtGiven.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);
        System.out.println(odtUtc.toLocalDateTime());
    }
}

Output:

2013-07-10T02:52:49+12:00
2013-07-09T14:52:49Z
2013-07-09T14:52:49

Learn about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110