Java : Month (月) - API使用例
Month (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
Month は日付の 月 を表す列挙型です。
final var month = Month.JANUARY;
System.out.println(month); // JANUARY
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 1月
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.US)); // January
System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.US)); // Jan
final var date = LocalDate.of(2100, 7, 1);
System.out.println(date); // 2100-07-01
final var month = date.getMonth();
System.out.println(month); // JULY
列挙型定数
APRIL
System.out.println(Month.APRIL); // APRIL
System.out.println(Month.APRIL.getValue()); // 4
AUGUST
System.out.println(Month.AUGUST); // AUGUST
System.out.println(Month.AUGUST.getValue()); // 8
DECEMBER
System.out.println(Month.DECEMBER); // DECEMBER
System.out.println(Month.DECEMBER.getValue()); // 12
FEBRUARY
System.out.println(Month.FEBRUARY); // FEBRUARY
System.out.println(Month.FEBRUARY.getValue()); // 2
JANUARY
System.out.println(Month.JANUARY); // JANUARY
System.out.println(Month.JANUARY.getValue()); // 1
JULY
System.out.println(Month.JULY); // JULY
System.out.println(Month.JULY.getValue()); // 7
JUNE
System.out.println(Month.JUNE); // JUNE
System.out.println(Month.JUNE.getValue()); // 6
MARCH
System.out.println(Month.MARCH); // MARCH
System.out.println(Month.MARCH.getValue()); // 3
MAY
System.out.println(Month.MAY); // MAY
System.out.println(Month.MAY.getValue()); // 5
NOVEMBER
System.out.println(Month.NOVEMBER); // NOVEMBER
System.out.println(Month.NOVEMBER.getValue()); // 11
OCTOBER
System.out.println(Month.OCTOBER); // OCTOBER
System.out.println(Month.OCTOBER.getValue()); // 10
SEPTEMBER
System.out.println(Month.SEPTEMBER); // SEPTEMBER
System.out.println(Month.SEPTEMBER.getValue()); // 9
メソッド
Temporal adjustInto (Temporal temporal)
final var temporal = LocalDate.of(2100, 3, 15);
System.out.println(temporal); // 2100-03-15
final var ret1 = Month.FEBRUARY.adjustInto(temporal);
System.out.println(ret1); // 2100-02-15
final var ret2 = Month.MARCH.adjustInto(temporal);
System.out.println(ret2); // 2100-03-15
final var ret3 = Month.APRIL.adjustInto(temporal);
System.out.println(ret3); // 2100-04-15
int firstDayOfYear (boolean leapYear)
System.out.println(Month.JANUARY.firstDayOfYear(false)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(false)); // 32
System.out.println(Month.MARCH.firstDayOfYear(false)); // 60
System.out.println(Month.APRIL.firstDayOfYear(false)); // 91
System.out.println(Month.JANUARY.firstDayOfYear(true)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(true)); // 32
System.out.println(Month.MARCH.firstDayOfYear(true)); // 61
System.out.println(Month.APRIL.firstDayOfYear(true)); // 92
Month firstMonthOfQuarter ()
System.out.println(Month.JANUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.FEBRUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.MARCH.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.APRIL.firstMonthOfQuarter()); // APRIL
System.out.println(Month.MAY.firstMonthOfQuarter()); // APRIL
System.out.println(Month.JUNE.firstMonthOfQuarter()); // APRIL
System.out.println(Month.JULY.firstMonthOfQuarter()); // JULY
System.out.println(Month.AUGUST.firstMonthOfQuarter()); // JULY
System.out.println(Month.SEPTEMBER.firstMonthOfQuarter()); // JULY
System.out.println(Month.OCTOBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.NOVEMBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.DECEMBER.firstMonthOfQuarter()); // OCTOBER
static Month from (TemporalAccessor temporal)
final var temporal = LocalDate.of(2100, 1, 15);
System.out.println(temporal); // 2100-01-15
System.out.println(Month.from(temporal)); // JANUARY
final var temporal = LocalDate.of(1999, 2, 28);
System.out.println(temporal); // 1999-02-28
System.out.println(Month.from(temporal)); // FEBRUARY
int get (TemporalField field)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
String getDisplayName (TextStyle style, Locale locale)
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // ja-JP
System.out.println(Month.JANUARY.getDisplayName(TextStyle.FULL, locale)); // 1月
System.out.println(Month.JANUARY.getDisplayName(TextStyle.SHORT, locale)); // 1月
System.out.println(Month.JANUARY.getDisplayName(TextStyle.NARROW, locale)); // 1
long getLong (TemporalField field)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.getLong(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
int getValue ()
System.out.println(Month.JANUARY.getValue()); // 1
System.out.println(Month.FEBRUARY.getValue()); // 2
System.out.println(Month.MARCH.getValue()); // 3
System.out.println(Month.APRIL.getValue()); // 4
System.out.println(Month.MAY.getValue()); // 5
System.out.println(Month.JUNE.getValue()); // 6
System.out.println(Month.JULY.getValue()); // 7
System.out.println(Month.AUGUST.getValue()); // 8
System.out.println(Month.SEPTEMBER.getValue()); // 9
System.out.println(Month.OCTOBER.getValue()); // 10
System.out.println(Month.NOVEMBER.getValue()); // 11
System.out.println(Month.DECEMBER.getValue()); // 12
boolean isSupported (TemporalField field)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
int length (boolean leapYear)
System.out.println(Month.JANUARY.length(false)); // 31
System.out.println(Month.FEBRUARY.length(false)); // 28
System.out.println(Month.MARCH.length(false)); // 31
System.out.println(Month.APRIL.length(false)); // 30
System.out.println(Month.JANUARY.length(true)); // 31
System.out.println(Month.FEBRUARY.length(true)); // 29
System.out.println(Month.MARCH.length(true)); // 31
System.out.println(Month.APRIL.length(true)); // 30
int maxLength ()
System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31
System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29
System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31
System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30
int minLength ()
System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31
System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29
System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31
System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30
Month minus (long months)
final var month = Month.FEBRUARY;
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 2月
final var ret1 = month.minus(1);
System.out.println(ret1.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 1月
final var ret2 = month.minus(2);
System.out.println(ret2.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 12月
final var ret3 = month.minus(11);
System.out.println(ret3.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 3月
final var ret4 = month.minus(12);
System.out.println(ret4.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 2月
static Month of (int month)
System.out.println(Month.of(1)); // JANUARY
System.out.println(Month.of(2)); // FEBRUARY
System.out.println(Month.of(3)); // MARCH
System.out.println(Month.of(4)); // APRIL
System.out.println(Month.of(5)); // MAY
System.out.println(Month.of(6)); // JUNE
System.out.println(Month.of(7)); // JULY
System.out.println(Month.of(8)); // AUGUST
System.out.println(Month.of(9)); // SEPTEMBER
System.out.println(Month.of(10)); // OCTOBER
System.out.println(Month.of(11)); // NOVEMBER
System.out.println(Month.of(12)); // DECEMBER
Month plus (long months)
final var month = Month.NOVEMBER;
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 11月
final var ret1 = month.plus(1);
System.out.println(ret1.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 12月
final var ret2 = month.plus(2);
System.out.println(ret2.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 1月
final var ret3 = month.plus(11);
System.out.println(ret3.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 10月
final var ret4 = month.plus(12);
System.out.println(ret4.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 11月
<R> R query (TemporalQuery<R> query)
System.out.println(Month.JANUARY.query(TemporalQueries.precision())); // Months
ValueRange range (TemporalField field)
System.out.println(Month.JANUARY.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
static Month valueOf (String name)
System.out.println(Month.valueOf("JANUARY")); // JANUARY
System.out.println(Month.valueOf("FEBRUARY")); // FEBRUARY
System.out.println(Month.valueOf("MARCH")); // MARCH
static Month[] values ()
for (final var month : Month.values()) {
System.out.println(month);
}
// 結果
// ↓
//JANUARY
//FEBRUARY
//MARCH
//APRIL
//MAY
//JUNE
//JULY
//AUGUST
//SEPTEMBER
//OCTOBER
//NOVEMBER
//DECEMBER
Enumで宣言されたメソッド
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
「Java API 使用例 : Enum」をご参照ください。
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- Calendar (カレンダー)
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock (時計)
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- DayOfWeek (曜日)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalDateTime (日時・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)