ZonedDateTime to UTC with offset applied?

前端 未结 4 660
不知归路
不知归路 2020-12-31 16:31

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

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

I get this value as <

相关标签:
4条回答
  • 2020-12-31 17:04

    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());
    
    0 讨论(0)
  • 2020-12-31 17:08

    @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]
    
    0 讨论(0)
  • 2020-12-31 17:14

    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

    0 讨论(0)
  • 2020-12-31 17:20

    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 = trucatedTime.withZoneSameInstant(ZoneOffset.UTC);
        LocalDateTime truncatedTimeUtcNoZone = truncatedTimeUtc.toLocalDateTime();
    
        System.out.println(time);
        System.out.println(trucatedTime);
        System.out.println(truncatedTimeUtc);
        System.out.println(truncatedTimeUtcNoZone);
      }
    }
    

    Here is a 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
    
    0 讨论(0)
提交回复
热议问题