Java ZonedDateTime Calculate toUTCZonedDateTime(String dateString)

Here you can find the source of toUTCZonedDateTime(String dateString)

Description

Converts any valid ZonedDateTime String (ISO_8601) representation to a UTC ZonedDateTime

e.g.

License

Open Source License

Parameter

Parameter Description
dateString e.g.

Return

@ZonedDateTime instance represented by dateString in UTC ("Z") or if the dateString does not represent a valid ISO_8601 zone string

Declaration

public static Optional<ZonedDateTime> toUTCZonedDateTime(String dateString) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.ZoneId;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Optional;

public class Main {
    private static final ZoneId UTC = ZoneId.of("Z");
    private static DateTimeFormatter dateTimeFormatterAny = DateTimeFormatter.ISO_ZONED_DATE_TIME;

    /**// ww w  . ja  va 2s .  c  om
     * Converts any valid ZonedDateTime String (ISO_8601) representation to a UTC ZonedDateTime
     * <p>
     * e.g. <br/>
     * 1. 2010-01-01T12:00:00+01:00[Europe/Paris] ==>  ZonedDateTime("2010-12-31T22:59:59.132Z") <br/>
     * 2. 2010-12-31T22:59:59.132Z ==>  ZonedDateTime("2010-12-31T22:59:59.132Z") <br/>
     * </p>
     *
     * @param dateString e.g.
     * @return @ZonedDateTime instance represented by dateString in UTC ("Z") or
     * @Optional.empty() if the dateString does not represent a valid ISO_8601 zone string
     * @see "https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html"
     */
    public static Optional<ZonedDateTime> toUTCZonedDateTime(String dateString) {
        try {
            ZonedDateTime utcDateTime = ZonedDateTime.parse(dateString, dateTimeFormatterAny)
                    .withZoneSameInstant(UTC);
            return Optional.of(utcDateTime);
        } catch (DateTimeParseException ex) {
            return Optional.empty();
        }
    }
}

Related

  1. stringToZonedDateTimeUTC(String dateAsString)
  2. timestampToZonedDateTime(Long timestamp)
  3. toEpochMillSeconds(ZonedDateTime time)
  4. toEpochTime(ZonedDateTime input)
  5. toLocalDateString(ZonedDateTime zonedDateTime)
  6. toZeroMSN(ZonedDateTime dateTime)
  7. uaDateTimeFromTime(ZonedDateTime time)
  8. Youngest(ZonedDateTime ZDT1, ZonedDateTime ZDT2)
  9. zoneDateTime2long(final ZonedDateTime zdt)