Java Program to get Temporal Queries precision



To get Temporal Queries precision, use the TemporalQuery interface with the precision() method of the TemporalQueries −

TemporalQuery<TemporalUnit>precision = TemporalQueries.precision();
Get the precision for LocalDate:
LocalDate.now().query(precision)
Get the precision for LocalTime:
LocalTime.now().query(precision)
Get the precision for YearMonth:
YearMonth.now().query(precision)
Get the precision for Year:
Year.now().query(precision)

Example

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
public class Demo {
   public static void main(String[] args) {
      TemporalQuery<TemporalUnit>precision = TemporalQueries.precision();
      System.out.println("TemporalQueries precision...");
      System.out.println(LocalDate.now().query(precision));
      System.out.println(LocalTime.now().query(precision));
      System.out.println(LocalDateTime.now().query(precision));
      System.out.println(YearMonth.now().query(precision));
      System.out.println(Year.now().query(precision));
   }
}

Output

TemporalQueries precision...
Days
Nanos
Nanos
Months
Years
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements