Java YearMonth

Introduction

YearMonth in Java, part of the java.time package, represents a combination of a year and a month without day or time information. It is useful for handling scenarios where only the year and month are relevant.

Table of Contents

  1. What is YearMonth?
  2. Creating YearMonth Instances
  3. Common Methods
  4. Examples of YearMonth
  5. Conclusion

1. What is YearMonth?

YearMonth is an immutable class that represents a year and month in the ISO-8601 calendar system. It is ideal for use cases like billing cycles, financial periods, or recurring events.

2. Creating YearMonth Instances

You can create YearMonth instances in several ways:

  • YearMonth.now(): Obtains the current year and month from the system clock.
  • YearMonth.of(int year, int month): Creates an instance with the specified year and month.
  • YearMonth.parse(CharSequence text): Parses a string to a YearMonth using the ISO-8601 format.

3. Common Methods

  • getYear(): Returns the year part of this YearMonth.
  • getMonthValue(): Returns the month part as an integer (1-12).
  • lengthOfMonth(): Returns the length of the month in days, accounting for leap years.
  • plusMonths(long monthsToAdd): Returns a copy of this YearMonth with the specified number of months added.
  • minusMonths(long monthsToSubtract): Returns a copy of this YearMonth with the specified number of months subtracted.
  • atDay(int dayOfMonth): Combines this YearMonth with a day to create a LocalDate.

4. Examples of YearMonth

Example 1: Getting the Current Year and Month

This example demonstrates how to get the current year and month using YearMonth.now().

import java.time.YearMonth;

public class CurrentYearMonthExample {
    public static void main(String[] args) {
        YearMonth current = YearMonth.now();
        System.out.println("Current Year and Month: " + current);
    }
}

Output:

Current Year and Month: 2024-06

Example 2: Creating a Specific Year and Month

Here, we create a specific YearMonth using YearMonth.of(int year, int month).

import java.time.YearMonth;

public class SpecificYearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.of(2023, 6);
        System.out.println("Specific Year and Month: " + yearMonth);
    }
}

Output:

Specific Year and Month: 2023-06

Example 3: Parsing a YearMonth String

This example shows how to parse a string into a YearMonth using YearMonth.parse(CharSequence text).

import java.time.YearMonth;

public class ParseYearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.parse("2023-06");
        System.out.println("Parsed Year and Month: " + yearMonth);
    }
}

Output:

Parsed Year and Month: 2023-06

Example 4: Adding and Subtracting Months

In this example, we demonstrate how to add and subtract months from a YearMonth.

import java.time.YearMonth;

public class AddSubtractMonthsExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.of(2023, 6);
        YearMonth nextMonth = yearMonth.plusMonths(1);
        YearMonth previousMonth = yearMonth.minusMonths(1);
        System.out.println("Current Year and Month: " + yearMonth);
        System.out.println("Next Month: " + nextMonth);
        System.out.println("Previous Month: " + previousMonth);
    }
}

Output:

Current Year and Month: 2023-06
Next Month: 2023-07
Previous Month: 2023-05

Example 5: Getting the Length of the Month

This example shows how to retrieve the length of the month in days, accounting for leap years.

import java.time.YearMonth;

public class LengthOfMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.of(2023, 2);
        int length = yearMonth.lengthOfMonth();
        System.out.println("Length of Month: " + length + " days");
    }
}

Output:

Length of Month: 28 days

Example 6: Combining YearMonth with a Day

This example demonstrates how to combine a YearMonth with a day to create a LocalDate.

import java.time.LocalDate;
import java.time.YearMonth;

public class CombineWithDayExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.of(2023, 6);
        LocalDate date = yearMonth.atDay(15);
        System.out.println("Combined Date: " + date);
    }
}

Output:

Combined Date: 2023-06-15

Conclusion

The YearMonth class in Java is used for handling year and month combinations without day or time information. It provides methods to manipulate and retrieve information about the year and month, making it ideal for use cases such as financial periods and recurring events. Using YearMonth can lead to more effective and clear handling of date-related data in your Java applications.

Comments