Método LocalDate getLong() en Java con ejemplos

El método getLong() de la clase LocalDate en Java obtiene la era aplicable en esta fecha.

Sintaxis :

public long getLong(TemporalField field)

Parámetro : este método acepta un solo campo de parámetro obligatorio que especifica el campo que se va a obtener y no nulo.

Valor devuelto : la función devuelve el valor del campo.

Excepciones : el programa arroja tres excepciones que se describen a continuación:

  1. DateTimeException : se lanza si no se puede obtener un valor para el campo o si el valor está fuera del rango de valores válidos para el campo.
  2. UnsupportedTemporalTypeException : se lanza si el campo no es compatible o el rango de valores supera un largo.
  3. ArithmeticException : lanzado si se produce un desbordamiento numérico

Los siguientes programas ilustran el método getLong() de LocalDate en Java:

Programa 1 :

// Program to illustrate the getLong() method
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDate dt = LocalDate.parse("2018-11-27");
  
        // Prints the day number
        System.out.println(dt.getLong(ChronoField.DAY_OF_MONTH));
    }
}
Producción:

27

Programa 2 :

// Program to illustrate the getLong() method
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDate dt = LocalDate.parse("2018-11-27");
  
        // Prints the day number
        System.out.println(dt.getLong(ChronoField.DAY_OF_YEAR));
    }
}
Producción:

331

Programa 3 :

// Program to illustrate the getLong() method
// Exception Program
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GfG {
    public static void main(String[] args)
    {
        try {
            LocalDate dt = LocalDate.parse("2017-01-32");
            System.out.println(dt.getLong(ChronoField.DAY_OF_MONTH));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

java.time.format.DateTimeParseException: Text '2017-01-32' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32

Referencia : https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#getLong(java.time.temporal.TemporalField)

Publicación traducida automáticamente

Artículo escrito por gopaldave y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *