简体   繁体   中英

How to get ZoneId, LocalDateTime and Instant from TemporalAccessor

I'm using the below code to parse a date string:

    String time = "2 Jun 2019 03:51:17 PM ACST";
    String pattern = "d MMM yyyy hh:mm:ss a z"; // z detects the time zone (ACST here)
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

 // ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).atZone(ZoneId.of("Australia/Adelaide"));
 // localDateTime.toInstant().toEpochMilli();

The formatter.parse(time) already has both epoch time and time zone when I inspect it through debugging:

// formatter.parse(time) :
{InstantSeconds=1559456477},ISO,Australia/Adelaide resolved to 2019-06-02T15:51:17

My question is how to extract time zone (here Australia/Adelaide) from formatter.parse(time) which is of Parsed type to use the time zone dynamically in atZone() at this line? :

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).
                                     atZone(ZoneId.of("Australia/Adelaide"));

Even Furthur, how to extract the InstantSeconds=1559456477 from the formatter.parse(time) response to avoid taking the more steps below to get epoch timestamp? :

ZonedDateTime localDateTime = LocalDateTime.from(formatter.parse(time)).
                                     atZone(ZoneId.of("Australia/Adelaide"));

localDateTime.toInstant().toEpochMilli();

The DateTimeFormatter.parse method return the TemporalAccessor which contains date, time and some other information and you can get that information by querying

default <R> R query(TemporalQuery<R> query)

Framework-level interface defining read-only access to a temporal object, such as a date, time, offset or some combination of these.

This is the base interface type for date, time and offset objects. It is implemented by those classes that can provide information as fields or queries.

TemporalQuery is functional interface

The most common implementations are method references, such as LocalDate::from and ZoneId::from. Additional common queries are provided as static methods in TemporalQueries.

String time = "2 Jun 2019 03:51:17 PM ACST";
String pattern = "d MMM yyyy hh:mm:ss a z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

TemporalAccessor parsedTime = formatter.parse(time);

ZoneId zone = parsedTime.query(ZoneId::from);

LocalDateTime localDateTime = parsedTime.query(LocalDateTime::from);

Instant instant = parsedTime.query(Instant::from);

System.out.println(zone);
System.out.println(localDateTime);
System.out.println(instant);

You cannot extract the seconds or a zone id from a DateTimeFormatter , but you can do it using ZonedDateTime objects. Have a look at the following code and wonder about the fact that your time String seems to be in "America/Manaus" , which doesn't appear to be equal to Australian Central Standard Time

public static void main(String args[]) throws Exception {
    String time = "2 Jun 2019 03:51:17 PM ACST";
    String pattern = "d MMM yyyy hh:mm:ss a z"; // z detects the time zone (ACST here)
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);

    // parse a time object using the formatter and the time String
    ZonedDateTime zdt = ZonedDateTime.parse(time, formatter);
    // print it using a standard formatter
    System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // extract the zone id
    ZoneId zoneId = zdt.getZone();
    // print the zone id
    System.out.println("This time is of zone " + zoneId);

    // retrieve the instant seconds
    Instant instant = zdt.toInstant();
    // print the epoch seconds of another time zone
    System.out.println("Epoch seconds in Australia/Adelaide are " 
            + instant.atZone(ZoneId.of("Australia/Adelaide")).toEpochSecond());
}

When I run this, it outputs

2019-06-02T15:51:17-04:00[America/Manaus]
This time is of zone America/Manaus
Epoch seconds in Australia/Adelaide are 1559505077

which shows how to extract the epoch seconds and the time zone, but somehow does not recognize ACST as what it is supposed to mean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM