Instant from(TemporalAccessor temporal) example

Description

Instant from(TemporalAccessor temporal) gets an instance of Instant from a temporal object.

This obtains an instant based on the specified temporal.

The conversion extracts the INSTANT_SECONDS and NANO_OF_SECOND fields.

Syntax

from has the following syntax.


public static Instant from(TemporalAccessor temporal)

Example

The following example shows how to use from.


import java.time.Instant;
import java.time.ZonedDateTime;
//  w ww  .  ja  va2s  .co m
public class Main {
  public static void main(String[] args) {
    Instant instant = Instant.from(ZonedDateTime.now());
    System.out.println(instant.getEpochSecond());
    
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    java.time Reference »




Clock
DayOfWeek
Duration
Instant
LocalDate
LocalDateTime
LocalTime
Month
MonthDay
OffsetDateTime
OffsetTime
Period
Year
YearMonth
ZonedDateTime
ZoneId
ZoneOffset