要获得临时查询的精度,请使用TemporalQuery接口和TemporalQueries的precision()
方法-
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)
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)); } }
输出结果
TemporalQueries precision... Days Nanos Nanos Months Years