java.time.LocalDateTime.getLong() Method Example



Description

The java.time.LocalDateTime.getLong(TemporalField field) method gets the value of the specified field from this date-time as an long.

Declaration

Following is the declaration for java.time.LocalDateTime.getLong(TemporalField field) method.

public long getLong(TemporalField field)

Parameters

field − the field to get, not null.

Return Value

the value for the field.

Exceptions

  • DateTimeException − if a value for the field cannot be obtained or the value is outside the range of valid values for the field.

  • UnsupportedTemporalTypeException − if the field is not supported or the range of values exceeds an long.

  • ArithmeticException − if numeric overflow occurs

Example

The following example shows the usage of java.time.LocalDateTime.getLong(TemporalField field) method.

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LocalDateTimeDemo {
   public static void main(String[] args) {
 
      LocalDateTime date = LocalDateTime.parse("2017-02-03T12:30:30");
      System.out.println(date.getLong(ChronoField.CLOCK_HOUR_OF_DAY));  
   }
}

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

12
Advertisements