It returns the value of the specified field from 'this' instance. If the provided TemporalField is not supported, UnsupportedTemporalTypeException is thrown.
EPOCH_DAY and PROLEPTIC_MONTH are too large to fit in an int, there we should use getLong()
method instead.
package com.logicbig.example.localdate;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
public class GetExample {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.AUGUST, 20);
int i = d.get(ChronoField.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + i);
i = d.get(ChronoField.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR = " + i);
i = d.get(ChronoField.DAY_OF_WEEK);
System.out.print("DAY_OF_WEEK = " + i);
System.out.println(", DayOfWeek = "+ DayOfWeek.of(i));
i = d.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);
System.out.print("ALIGNED_DAY_OF_WEEK_IN_MONTH = " + i);
System.out.println(", DayOfWeek = "+ DayOfWeek.of(i));
i = d.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR);
System.out.print("ALIGNED_DAY_OF_WEEK_IN_YEAR = " + i);
System.out.println(", DayOfWeek = "+ DayOfWeek.of(i));
i = d.get(ChronoField.ALIGNED_WEEK_OF_MONTH);
System.out.println("ALIGNED_WEEK_OF_MONTH = " + i);
i = d.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
System.out.println("ALIGNED_WEEK_OF_YEAR = " + i);
i = d.get(ChronoField.MONTH_OF_YEAR);
System.out.println("MONTH_OF_YEAR = " + i);
i = d.get(ChronoField.YEAR_OF_ERA);
System.out.println("YEAR_OF_ERA = " + i);
i = d.get(ChronoField.YEAR);
System.out.println("YEAR = " + i);
i = d.get(ChronoField.ERA);
System.out.println("ERA = " + i);
}
}
Output
DAY_OF_MONTH = 20
DAY_OF_YEAR = 232
DAY_OF_WEEK = 5, DayOfWeek = FRIDAY
ALIGNED_DAY_OF_WEEK_IN_MONTH = 6, DayOfWeek = SATURDAY
ALIGNED_DAY_OF_WEEK_IN_YEAR = 1, DayOfWeek = MONDAY
ALIGNED_WEEK_OF_MONTH = 3
ALIGNED_WEEK_OF_YEAR = 34
MONTH_OF_YEAR = 8
YEAR_OF_ERA = 2010
YEAR = 2010
ERA = 1
package com.logicbig.example.localdate;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
public class GetExample2 {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.AUGUST, 20);
int i = d.get(ChronoField.MINUTE_OF_DAY);
System.out.println("MINUTE_OF_DAY = " + i);
}
}
Output
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfDay
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.get(LocalDate.java:622)
at com.logicbig.example.localdate.GetExample2.main(GetExample2.java:16)
... 6 more
package com.logicbig.example.localdate;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
public class GetExample3 {
public static void main (String[] args) {
LocalDate d = LocalDate.of(2010, Month.AUGUST, 20);
int i = d.get(ChronoField.EPOCH_DAY);
System.out.println("EPOCH_DAY = " + i);
}
}
Output
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Invalid field 'EpochDay' for get() method, use getLong() instead
at java.time.LocalDate.get0(LocalDate.java:671)
at java.time.LocalDate.get(LocalDate.java:622)
at com.logicbig.example.localdate.GetExample3.main(GetExample3.java:15)
... 6 more