Java LocalDateTime get field values via ChronoField

Description

Java LocalDateTime get field values via ChronoField

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

public class Main {
  public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Current Date Time: " + now);
    System.out.println("Year: " + now.get(ChronoField.YEAR));
    System.out.println("Month: " + now.get(ChronoField.MONTH_OF_YEAR));
    System.out.println("Day: " + now.get(ChronoField.DAY_OF_MONTH));
    System.out.println("Hour-of-day: " + now.get(ChronoField.HOUR_OF_DAY));
    System.out.println("Hour-of-AMPM: " + now.get(ChronoField.HOUR_OF_AMPM));
    System.out.println("AMPM-of-day: " + now.get(ChronoField.AMPM_OF_DAY));

  }//from  w  w  w.  ja  v  a 2  s .  c  o m
}



PreviousNext

Related