java.time.LocalDateTime.ofEpochSecond() Method Example



Description

The java.time.LocalDateTime.ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) method obtains an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z.

Declaration

Following is the declaration for java.time.LocalDateTime.ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) method.

public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)

Parameters

  • epochSecond − the number of seconds from the epoch of 1970-01-01T00:00:00Z

  • nanoOfSecond − the nanosecond within the second, from 0 to 999,999,999

  • offset − the zone offset, not null

Return Value

the local date-time, not null.

Exceptions

DateTimeException − if the result exceeds the supported range, or if the nano-of-second is invalid.

Example

The following example shows the usage of java.time.LocalDateTime.ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) method.

package com.tutorialspoint;

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

public class LocalDateTimeDemo {
   public static void main(String[] args) {
 
      LocalDateTime date = LocalDateTime.ofEpochSecond(50000,50000,ZoneOffset.UTC);
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

1970-01-01T13:53:20.000050
Advertisements