Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Date and Time Temporal Field

I navigation across Java 8 Date and Time new API and i couldn't go any further and i cannot find any good resource in the web i hope you be kindle with this question.

My simple source code is

final LocalDate date = LocalDate.now();
date.get(TemporalField field)

And i get the source code and i see.

public int get(TemporalField field)
  Gets the value of the specified field from this date as an int.

My question is how i can get a TemporalField or which is the easiest way to get this code to work..

final int value = date.get(????);

This works thanks to JB Nizet

date.get(java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);

API.

http://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html
like image 898
chiperortiz Avatar asked Feb 05 '15 20:02

chiperortiz


2 Answers

There are a number of ways to get instances of TemporalField.

Firstly, the enum ChronoField implements TemporalField and contains various constants.

ChronoField.ALIGNED_WEEK_OF_YEAR;

Secondly, the classes IsoFields and JulianFields contain static TemporalField objects.

IsoFields.QUARTER_OF_YEAR;
JulianFields.JULIAN_DAY;

Thirdly, the class WeekFields contains static TemporalField objects and methods to create TemporalField objects based off WeekFields.

WeekFields.WEEK_BASED_YEARS;
WeekFields.SUNDAY_START.weekOfMonth();
like image 55
Stuart Evans Avatar answered Oct 09 '22 17:10

Stuart Evans


The Javadoc of TemporalField says, right there:

"The most commonly used units are defined in ChronoField. Further fields are supplied in IsoFields, WeekFields and JulianFields. Fields can also be written by application code by implementing this interface."

Pick what you want to get out from those fields.

like image 44
Louis Wasserman Avatar answered Oct 09 '22 17:10

Louis Wasserman