広告

Java : YearMonth (年・月) - API使用例

YearMonth (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。


概要

ISO-8601暦体系における年月(2007-12など)。

クラス構成

YearMonth は、日付の年と月を表します。
例えば、

  • 2100年8月
  • 1999年1月

などが表現できます。

オブジェクトの生成には of メソッドを使います。

final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08

System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8

メソッド

Temporal adjustInto (Temporal temporal)

指定された時間的オブジェクトをこの年/月になるように調整します。

final var yearMonth = YearMonth.of(1999, Month.DECEMBER);
System.out.println(yearMonth); // 1999-12

final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03

final var ret = yearMonth.adjustInto(date);
System.out.println(ret); // 1999-12-03

LocalDate atDay (int dayOfMonth)

この年-月を「月の日」と組み合せてLocalDateを作成します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

System.out.println(yearMonth.atDay(2)); // 2100-01-02
System.out.println(yearMonth.atDay(3)); // 2100-01-03
System.out.println(yearMonth.atDay(31)); // 2100-01-31

try {
    final var ret = yearMonth.atDay(32);
} catch (DateTimeException e) {
    System.out.println("DateTimeException! : " + e.getMessage());
}

// 結果
// ↓
//DateTimeException! : Invalid value for DayOfMonth (valid values 1 - 28/31): 32

LocalDate atEndOfMonth ()

その月の終わりのLocalDateを返します。

final var yearMonth = YearMonth.of(2103, Month.JANUARY);
System.out.println(yearMonth); // 2103-01

System.out.println(yearMonth.atEndOfMonth()); // 2103-01-31
System.out.println(yearMonth.lengthOfMonth()); // 31
final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02
System.out.println(yearMonth.isLeapYear()); // false

System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfMonth()); // 28
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true

System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfMonth()); // 29

int compareTo (YearMonth other)

この年-月をもう一方の年-月と比較します。

final var yearMonth1 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth2 = YearMonth.of(2100, Month.JANUARY);

System.out.println(yearMonth1); // 2100-01
System.out.println(yearMonth2); // 2100-01

System.out.println(yearMonth1.compareTo(yearMonth2)); // 0
System.out.println(yearMonth2.compareTo(yearMonth1)); // 0
final var yearMonth1 = YearMonth.of(1999, Month.MAY);
final var yearMonth2 = YearMonth.of(2100, Month.MAY);

System.out.println(yearMonth1); // 1999-05
System.out.println(yearMonth2); // 2100-05

System.out.println(yearMonth1.compareTo(yearMonth2)); // -101
System.out.println(yearMonth2.compareTo(yearMonth1)); // 101
final var yearMonth1 = YearMonth.of(2100, Month.DECEMBER);
final var yearMonth2 = YearMonth.of(2100, Month.APRIL);

System.out.println(yearMonth1); // 2100-12
System.out.println(yearMonth2); // 2100-04

System.out.println(yearMonth1.compareTo(yearMonth2)); // 8
System.out.println(yearMonth2.compareTo(yearMonth1)); // -8

boolean equals (Object obj)

この年-月がもう一方の年-月と等しいかどうかを確認します。

final var yearMonth1 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth2 = YearMonth.of(2100, Month.JANUARY);

System.out.println(yearMonth1); // 2100-01
System.out.println(yearMonth2); // 2100-01

System.out.println(yearMonth1.equals(yearMonth2)); // true
System.out.println(yearMonth2.equals(yearMonth1)); // true
final var yearMonth1 = YearMonth.of(1999, Month.MAY);
final var yearMonth2 = YearMonth.of(2100, Month.MAY);

System.out.println(yearMonth1); // 1999-05
System.out.println(yearMonth2); // 2100-05

System.out.println(yearMonth1.equals(yearMonth2)); // false
System.out.println(yearMonth2.equals(yearMonth1)); // false
final var yearMonth1 = YearMonth.of(2100, Month.DECEMBER);
final var yearMonth2 = YearMonth.of(2100, Month.APRIL);

System.out.println(yearMonth1); // 2100-12
System.out.println(yearMonth2); // 2100-04

System.out.println(yearMonth1.equals(yearMonth2)); // false
System.out.println(yearMonth2.equals(yearMonth1)); // false

String format (DateTimeFormatter formatter)

指定されたフォーマッタを使用してこの年-月を書式設定します。

final var yearMonth = YearMonth.of(2099, Month.JANUARY);
System.out.println(yearMonth); // 2099-01

final var ret1 = yearMonth.format(DateTimeFormatter.ofPattern("yyyy-MM"));
System.out.println(ret1); // 2099-01

final var ret2 = yearMonth.format(DateTimeFormatter.ofPattern("yy-M"));
System.out.println(ret2); // 99-1

static YearMonth from (TemporalAccessor temporal)

時間的オブジェクトからYearMonthのインスタンスを取得します。

final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03

final var ret = YearMonth.from(date);
System.out.println(ret); // 2100-02
final var dateTime = LocalDateTime.of(1999, 1, 1, 12, 30);
System.out.println(dateTime); // 1999-01-01T12:30

final var ret = YearMonth.from(dateTime);
System.out.println(ret); // 1999-01

int get (TemporalField field)

この年/月から指定されたフィールドの値をintとして取得します。

final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08

System.out.println(yearMonth.get(ChronoField.YEAR)); // 2100
System.out.println(yearMonth.get(ChronoField.MONTH_OF_YEAR)); // 8
System.out.println(yearMonth.get(ChronoField.ERA)); // 1
final var yearMonth = YearMonth.of(-100, Month.DECEMBER);
System.out.println(yearMonth); // -0100-12

System.out.println(yearMonth.get(ChronoField.YEAR)); // -100
System.out.println(yearMonth.get(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(yearMonth.get(ChronoField.ERA)); // 0

long getLong (TemporalField field)

この年/月から指定されたフィールドの値をlongとして取得します。

final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08

System.out.println(yearMonth.getLong(ChronoField.YEAR)); // 2100
System.out.println(yearMonth.getLong(ChronoField.MONTH_OF_YEAR)); // 8
System.out.println(yearMonth.getLong(ChronoField.ERA)); // 1
final var yearMonth = YearMonth.of(-100, Month.DECEMBER);
System.out.println(yearMonth); // -0100-12

System.out.println(yearMonth.getLong(ChronoField.YEAR)); // -100
System.out.println(yearMonth.getLong(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(yearMonth.getLong(ChronoField.ERA)); // 0

Month getMonth ()

列挙型Monthを使用して、月フィールドを取得します。

final var yearMonth = YearMonth.of(2100, Month.AUGUST);
System.out.println(yearMonth); // 2100-08

System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // AUGUST
System.out.println(yearMonth.getMonthValue()); // 8

int getMonthValue ()

月フィールドを取得します(1-12)。

このメソッドの使用例は、getMonth() にまとめて記載しました。
そちらのAPI使用例をご参照ください。

int getYear ()

年フィールドを取得します。

このメソッドの使用例は、getMonth() にまとめて記載しました。
そちらのAPI使用例をご参照ください。

int hashCode ()

この年-月のハッシュ・コードです。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01
System.out.println(yearMonth.hashCode()); // 134219828
final var yearMonth = YearMonth.of(-100, Month.AUGUST);
System.out.println(yearMonth); // -0100-08
System.out.println(yearMonth.hashCode()); // -1073741924

boolean isAfter (YearMonth other)

この年/月が、指定された年/月より後にあるかどうかをチェックします。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var yearMonth2 = YearMonth.of(2099, Month.DECEMBER);
final var yearMonth3 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth4 = YearMonth.of(2100, Month.FEBRUARY);

System.out.println(yearMonth2); // 2099-12
System.out.println(yearMonth3); // 2100-01
System.out.println(yearMonth4); // 2100-02

System.out.println(yearMonth.isAfter(yearMonth2)); // true
System.out.println(yearMonth.isAfter(yearMonth3)); // false
System.out.println(yearMonth.isAfter(yearMonth4)); // false

boolean isBefore (YearMonth other)

この年/月が、指定された年/月より前にあるかどうかをチェックします。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var yearMonth2 = YearMonth.of(2099, Month.DECEMBER);
final var yearMonth3 = YearMonth.of(2100, Month.JANUARY);
final var yearMonth4 = YearMonth.of(2100, Month.FEBRUARY);

System.out.println(yearMonth2); // 2099-12
System.out.println(yearMonth3); // 2100-01
System.out.println(yearMonth4); // 2100-02

System.out.println(yearMonth.isBefore(yearMonth2)); // false
System.out.println(yearMonth.isBefore(yearMonth3)); // false
System.out.println(yearMonth.isBefore(yearMonth4)); // true

boolean isLeapYear ()

ISO先発暦体系のルールに従って、年がうるう年であるかどうかをチェックします。

final var yearMonth = YearMonth.of(2103, Month.FEBRUARY);
System.out.println(yearMonth); // 2103-02

System.out.println(yearMonth.isLeapYear()); // false
System.out.println(yearMonth.atEndOfMonth()); // 2103-02-28
System.out.println(yearMonth.lengthOfYear()); // 365
final var yearMonth = YearMonth.of(2104, Month.FEBRUARY);
System.out.println(yearMonth); // 2104-02
System.out.println(yearMonth.isLeapYear()); // true
System.out.println(yearMonth.atEndOfMonth()); // 2104-02-29
System.out.println(yearMonth.lengthOfYear()); // 366

boolean isSupported (TemporalField field)

指定されたフィールドがサポートされているかどうかをチェックします。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

System.out.println(yearMonth.isSupported(ChronoField.YEAR)); // true
System.out.println(yearMonth.isSupported(ChronoField.YEAR_OF_ERA)); // true
System.out.println(yearMonth.isSupported(ChronoField.ERA)); // true
System.out.println(yearMonth.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(yearMonth.isSupported(ChronoField.PROLEPTIC_MONTH)); // true

System.out.println(yearMonth.isSupported(ChronoField.DAY_OF_MONTH)); // false

boolean isSupported (TemporalUnit unit)

指定された単位がサポートされているかどうかをチェックします。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

System.out.println(yearMonth.isSupported(ChronoUnit.YEARS)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.MONTHS)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.DECADES)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.CENTURIES)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.MILLENNIA)); // true
System.out.println(yearMonth.isSupported(ChronoUnit.ERAS)); // true

System.out.println(yearMonth.isSupported(ChronoUnit.DAYS)); // false

boolean isValidDay (int dayOfMonth)

「月の日」がこの年-月に対して有効であるかどうかを確認します。

final var yearMonth = YearMonth.of(2100, Month.APRIL);
System.out.println(yearMonth); // 2100-04
System.out.println(yearMonth.atEndOfMonth()); // 2100-04-30

System.out.println(yearMonth.isValidDay(1)); // true
System.out.println(yearMonth.isValidDay(15)); // true
System.out.println(yearMonth.isValidDay(30)); // true
System.out.println(yearMonth.isValidDay(31)); // false

int lengthOfMonth ()

年を考慮に入れて、月の長さを返します。

このメソッドの使用例は、atEndOfMonth() にまとめて記載しました。
そちらのAPI使用例をご参照ください。

int lengthOfYear ()

年の長さを返します。

このメソッドの使用例は、isLeapYear() にまとめて記載しました。
そちらのAPI使用例をご参照ください。

YearMonth minus (long amountToSubtract, TemporalUnit unit)

指定された量を減算して、この年/月のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02

final var ret1 = yearMonth.minus(10, ChronoUnit.YEARS);
System.out.println(ret1); // 2090-02

final var ret2 = yearMonth.minus(20, ChronoUnit.YEARS);
System.out.println(ret2); // 2080-02

final var ret3 = yearMonth.minus(1, ChronoUnit.MONTHS);
System.out.println(ret3); // 2100-01

final var ret4 = yearMonth.minus(2, ChronoUnit.MONTHS);
System.out.println(ret4); // 2099-12

YearMonth minus (TemporalAmount amountToSubtract)

指定された量を減算して、この年/月のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02

final var ret1 = yearMonth.minus(Period.ofYears(10));
System.out.println(ret1); // 2090-02

final var ret2 = yearMonth.minus(Period.ofYears(20));
System.out.println(ret2); // 2080-02

final var ret3 = yearMonth.minus(Period.ofMonths(1));
System.out.println(ret3); // 2100-01

final var ret4 = yearMonth.minus(Period.ofMonths(2));
System.out.println(ret4); // 2099-12

YearMonth minusMonths (long monthsToSubtract)

指定された月数を減算した、このYearMonthのコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02

final var ret1 = yearMonth.minusMonths(1);
System.out.println(ret1); // 2100-01

final var ret2 = yearMonth.minusMonths(2);
System.out.println(ret2); // 2099-12

YearMonth minusYears (long yearsToSubtract)

指定された年数を減算した、このYearMonthのコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.FEBRUARY);
System.out.println(yearMonth); // 2100-02

final var ret1 = yearMonth.minusYears(10);
System.out.println(ret1); // 2090-02

final var ret2 = yearMonth.minusYears(20);
System.out.println(ret2); // 2080-02

static YearMonth now ()

デフォルトのタイムゾーンのシステム・クロックから現在の年-月を取得します。

final var now = YearMonth.now();
System.out.println(now); // 2023-10

static YearMonth now (Clock clock)

指定されたクロックから現在の年-月を取得します。

// 意図的に1年進めたClock
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(365));

System.out.println(YearMonth.now()); // 2023-10
System.out.println(YearMonth.now(clock)); // 2024-10

static YearMonth now (ZoneId zone)

指定されたタイムゾーンのシステム・クロックから現在の年-月を取得します。

final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z

final var now = YearMonth.now(zone);
System.out.println(now); // 2023-10

static YearMonth of (int year, int month)

年および月からYearMonthのインスタンスを取得します。

final var yearMonth = YearMonth.of(2100, 1);
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // JANUARY
System.out.println(yearMonth.getMonthValue()); // 1
final var yearMonth = YearMonth.of(1999, 11);
System.out.println(yearMonth.getYear()); // 1999
System.out.println(yearMonth.getMonth()); // NOVEMBER
System.out.println(yearMonth.getMonthValue()); // 11

static YearMonth of (int year, Month month)

年および月からYearMonthのインスタンスを取得します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth.getYear()); // 2100
System.out.println(yearMonth.getMonth()); // JANUARY
System.out.println(yearMonth.getMonthValue()); // 1
final var yearMonth = YearMonth.of(1999, Month.NOVEMBER);
System.out.println(yearMonth.getYear()); // 1999
System.out.println(yearMonth.getMonth()); // NOVEMBER
System.out.println(yearMonth.getMonthValue()); // 11

static YearMonth parse (CharSequence text)

2007-12などのテキスト文字列からYearMonthのインスタンスを取得します。

final var yearMonth1 = YearMonth.parse("2100-01");
System.out.println(yearMonth1); // 2100-01

final var yearMonth2 = YearMonth.parse("1999-12");
System.out.println(yearMonth2); // 1999-12

static YearMonth parse (CharSequence text, DateTimeFormatter formatter)

特定のフォーマッタを使用して、テキスト文字列からYearMonthのインスタンスを取得します。

final var yearMonth1 = YearMonth.parse(
        "2100-01", DateTimeFormatter.ofPattern("yyyy-MM"));
System.out.println(yearMonth1); // 2100-01

final var yearMonth2 = YearMonth.parse(
        "99-5", DateTimeFormatter.ofPattern("yy-M"));
System.out.println(yearMonth2); // 2099-05

YearMonth plus (long amountToAdd, TemporalUnit unit)

指定された量を加算して、この年/月のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11

final var ret1 = yearMonth.plus(50, ChronoUnit.YEARS);
System.out.println(ret1); // 2150-11

final var ret2 = yearMonth.plus(60, ChronoUnit.YEARS);
System.out.println(ret2); // 2160-11

final var ret3 = yearMonth.plus(1, ChronoUnit.MONTHS);
System.out.println(ret3); // 2100-12

final var ret4 = yearMonth.plus(2, ChronoUnit.MONTHS);
System.out.println(ret4); // 2101-01

YearMonth plus (TemporalAmount amountToAdd)

指定された量を加算して、この年/月のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11

final var ret1 = yearMonth.plus(Period.ofYears(50));
System.out.println(ret1); // 2150-11

final var ret2 = yearMonth.plus(Period.ofYears(60));
System.out.println(ret2); // 2160-11

final var ret3 = yearMonth.plus(Period.ofMonths(1));
System.out.println(ret3); // 2100-12

final var ret4 = yearMonth.plus(Period.ofMonths(2));
System.out.println(ret4); // 2101-01

YearMonth plusMonths (long monthsToAdd)

指定された月数を加算した、このYearMonthのコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11

final var ret1 = yearMonth.plusMonths(1);
System.out.println(ret1); // 2100-12

final var ret2 = yearMonth.plusMonths(2);
System.out.println(ret2); // 2101-01

YearMonth plusYears (long yearsToAdd)

指定された年数を加算した、このYearMonthのコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.NOVEMBER);
System.out.println(yearMonth); // 2100-11

final var ret1 = yearMonth.plusYears(50);
System.out.println(ret1); // 2150-11

final var ret2 = yearMonth.plusYears(60);
System.out.println(ret2); // 2160-11

<R> R query (TemporalQuery<R> query)

指定された問合せを使用してこの年/月を問い合せます。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var ret = yearMonth.query(TemporalQueries.precision());
System.out.println(ret); // Months

ValueRange range (TemporalField field)

指定されたフィールドの有効な値の範囲を取得します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

System.out.println(yearMonth.range(ChronoField.YEAR)); // -999999999 - 999999999
System.out.println(yearMonth.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12

String toString ()

この年-月をStringとして出力します(2007-12など)。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
final var str = yearMonth.toString();
System.out.println(str); // 2100-01
final var yearMonth = YearMonth.of(1999, Month.OCTOBER);
final var str = yearMonth.toString();
System.out.println(str); // 1999-10

long until (Temporal endExclusive, TemporalUnit unit)

別の年/月までの時間量を指定された単位で計算します。

final var start = YearMonth.of(2100, Month.JANUARY);
System.out.println(start); // 2100-01

final var end = YearMonth.of(2150, Month.JANUARY);
System.out.println(end); // 2150-01

final var ret = start.until(end, ChronoUnit.YEARS);
System.out.println(ret); // 50
final var start = YearMonth.of(2100, Month.JANUARY);
System.out.println(start); // 2100-01

final var end = YearMonth.of(2100, Month.AUGUST);
System.out.println(end); // 2100-08

final var ret = start.until(end, ChronoUnit.MONTHS);
System.out.println(ret); // 7

YearMonth with (TemporalAdjuster adjuster)

この年/月の調整済のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var ret1 = yearMonth.with(Year.of(1999));
System.out.println(ret1); // 1999-01

final var ret2 = yearMonth.with(Month.AUGUST);
System.out.println(ret2); // 2100-08

YearMonth with (TemporalField field, long newValue)

指定されたフィールドを新しい値に設定して、この年/月のコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var ret1 = yearMonth.with(ChronoField.YEAR, 1999);
System.out.println(ret1); // 1999-01

final var ret2 = yearMonth.with(ChronoField.MONTH_OF_YEAR, 8);
System.out.println(ret2); // 2100-08

YearMonth withMonth (int month)

月を変更して、このYearMonthのコピーを返します。

final var yearMonth = YearMonth.of(2100, Month.JANUARY);
System.out.println(yearMonth); // 2100-01

final var ret1 = yearMonth.withYear(1999);
System.out.println(ret1); // 1999-01

final var ret2 = yearMonth.withMonth(8);
System.out.println(ret2); // 2100-08

YearMonth withYear (int year)

年を変更して、このYearMonthのコピーを返します。

このメソッドの使用例は、withMonth(int month) にまとめて記載しました。
そちらのAPI使用例をご参照ください。


関連記事

ページの先頭へ