Close

Java Date Time - YearMonth.get() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

java.lang.Objectjava.lang.Objectjava.time.YearMonthjava.time.YearMonthjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Method:

public int get(TemporalField field)

This method returns the value of the specified field from this year-month as an int.


Examples


package com.logicbig.example.yearmonth;

import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.time.temporal.UnsupportedTemporalTypeException;

public class GetExample {

public static void main(String... args) {

YearMonth y = YearMonth.of(2010, 1);
System.out.println(y);

for (ChronoField field : ChronoField.values()) {
try {
int i = y.get(field);
System.out.printf("%12s > %s%n", field, i);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf(" -- %s not supported%n", field);
} catch (Exception e){
System.out.printf(" -- %s error:%s%n",field, e.getMessage());
}
}
}
}

Output

2010-01
-- NanoOfSecond not supported
-- NanoOfDay not supported
-- MicroOfSecond not supported
-- MicroOfDay not supported
-- MilliOfSecond not supported
-- MilliOfDay not supported
-- SecondOfMinute not supported
-- SecondOfDay not supported
-- MinuteOfHour not supported
-- MinuteOfDay not supported
-- HourOfAmPm not supported
-- ClockHourOfAmPm not supported
-- HourOfDay not supported
-- ClockHourOfDay not supported
-- AmPmOfDay not supported
-- DayOfWeek not supported
-- AlignedDayOfWeekInMonth not supported
-- AlignedDayOfWeekInYear not supported
-- DayOfMonth not supported
-- DayOfYear not supported
-- EpochDay not supported
-- AlignedWeekOfMonth not supported
-- AlignedWeekOfYear not supported
MonthOfYear > 1
-- ProlepticMonth error:Invalid value for ProlepticMonth (valid values -11999999988 - 11999999999): 24120
YearOfEra > 2010
Year > 2010
Era > 1
-- InstantSeconds not supported
-- OffsetSeconds not supported




See Also