Java YearMonth class

In Java, the YearMonth class is a part of the java.time package introduced in Java 8. It represents a combination of year and month, without a day or time.

The YearMonth class provides several useful methods for working with year-month combinations. Here are some examples:

  1. Creating a YearMonth object:
YearMonth yearMonth = YearMonth.of(2023, 5); // May 2023

2. Getting the year and month values:

int year = yearMonth.getYear(); // 2023
Month month = yearMonth.getMonth(); // MAY
int monthValue = yearMonth.getMonthValue(); // 5

3. Formatting a YearMonth object:

String formattedYearMonth = yearMonth.format(DateTimeFormatter.ofPattern("MMM yyyy")); // May 2023

4. Adding or subtracting years or months:

YearMonth nextYearMonth = yearMonth.plusYears(1); // June 2024
YearMonth previousYearMonth = yearMonth.minusMonths(3); // February 2023

5.Comparing YearMonth objects:

YearMonth otherYearMonth = YearMonth.of(2022, 5);
boolean isAfter = yearMonth.isAfter(otherYearMonth); // true
boolean isBefore = yearMonth.isBefore(otherYearMonth); // false
boolean isEqual = yearMonth.equals(otherYearMonth); // false

Overall, the YearMonth class is a useful class for dealing with year and month combinations in Java.

Java YearMonth class declaration:

The declaration of the YearMonth class in Java is as follows:

public final class YearMonth
    implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable

The YearMonth class is a final class, which means it cannot be subclassed. It implements several interfaces:

  • Temporal: Provides access to the date and time fields of a temporal object, such as year and month.
  • TemporalAdjuster: Allows adjustments to be made to a temporal object, such as adding or subtracting months.
  • Comparable<YearMonth>: Allows YearMonth objects to be compared to each other.

The YearMonth class also implements the Serializable interface, which allows instances of the class to be serialized and deserialized.

The class provides several constructors and static factory methods for creating YearMonth objects, as well as various methods for working with year and month combinations. Some of the most commonly used methods include:

  • of(int year, int month): Creates a YearMonth object with the specified year and month values.
  • getYear(): Returns the year value of the YearMonth object.
  • getMonth(): Returns the month value of the YearMonth object as a Month enum.
  • getMonthValue(): Returns the month value of the YearMonth object as an integer.
  • plusYears(long years): Returns a new YearMonth object with the specified number of years added.
  • minusMonths(long months): Returns a new YearMonth object with the specified number of months subtracted.
  • isAfter(YearMonth other): Returns true if the YearMonth object is after the specified YearMonth object.
  • isBefore(YearMonth other): Returns true if the YearMonth object is before the specified YearMonth object.
  • equals(Object obj): Returns true if the YearMonth object is equal to the specified object.

Overall, the YearMonth class provides a convenient and efficient way to work with year and month combinations in Java.

Methods of Java YearMonth:

The YearMonth class in Java provides various methods for working with year and month combinations. Here are some of the most commonly used methods:

  1. static YearMonth now(): Returns the current year and month based on the system clock.
  2. static YearMonth now(ZoneId zone): Returns the current year and month based on the specified time zone.
  3. static YearMonth parse(CharSequence text): Parses the specified text in the format “uuuu-MM” to create a YearMonth object.
  4. static YearMonth of(int year, int month): Returns a YearMonth object representing the specified year and month.
  5. int getYear(): Returns the year value of the YearMonth object.
  6. Month getMonth(): Returns the month value of the YearMonth object as a Month enum.
  7. int getMonthValue(): Returns the month value of the YearMonth object as an integer.
  8. YearMonth plusYears(long years): Returns a new YearMonth object with the specified number of years added.
  9. YearMonth plusMonths(long months): Returns a new YearMonth object with the specified number of months added.
  10. YearMonth minusYears(long years): Returns a new YearMonth object with the specified number of years subtracted.
  11. YearMonth minusMonths(long months): Returns a new YearMonth object with the specified number of months subtracted.
  12. boolean isAfter(YearMonth other): Returns true if the YearMonth object is after the specified YearMonth object.
  13. boolean isBefore(YearMonth other): Returns true if the YearMonth object is before the specified YearMonth object.
  14. boolean equals(Object obj): Returns true if the YearMonth object is equal to the specified object.
  15. int compareTo(YearMonth other): Compares this YearMonth object with another YearMonth object, and returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
  16. String toString(): Returns a string representation of the YearMonth object in the format “uuuu-MM”.
  17. String format(DateTimeFormatter formatter): Formats the YearMonth object using the specified formatter, and returns the resulting string.

Overall, these methods provide a convenient and efficient way to work with year and month combinations in Java.

Java YearMonth Example: now()

Sure, here’s an example of using the now() method of the YearMonth class in Java:

import java.time.YearMonth;

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

In this example, we import the YearMonth class from the java.time package. We then create a YearMonth object using the now() method, which returns the current year and month based on the system clock. We print the resulting YearMonth object using the println() method of the System.out object.

When we run this code, we get output similar to the following:

Current Year-Month: 2023-05

This output shows the current year and month in the format “uuuu-MM”, where “uuuu” represents the year and “MM” represents the month.

Java YearMonth Example: format()

Sure, here’s an example of using the format() method of the YearMonth class in Java:

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class YearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.now();
        String formatted = yearMonth.format(DateTimeFormatter.ofPattern("MMM uuuu"));
        System.out.println("Current Year-Month (Formatted): " + formatted);
    }
}

In this example, we import the YearMonth class from the java.time package, as well as the DateTimeFormatter class. We then create a YearMonth object using the now() method, which returns the current year and month based on the system clock.

We create a DateTimeFormatter object using the ofPattern() method, and specify the pattern “MMM uuuu”, which represents the month abbreviation and year in the format “Jan 2023”. We then call the format() method on the YearMonth object, passing in the DateTimeFormatter object to format the YearMonth object.

Finally, we print the resulting formatted string using the println() method of the System.out object.

When we run this code, we get output similar to the following:

Current Year-Month (Formatted): May 2023

This output shows the current year and month in the format “MMM uuuu”, where “MMM” represents the month abbreviation and “uuuu” represents the year.

Java YearMonth Example: get()

Sure, here’s an example of using the get() method of the YearMonth class in Java:

import java.time.YearMonth;

public class YearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.now();
        int year = yearMonth.getYear();
        int monthValue = yearMonth.getMonthValue();
        System.out.println("Current Year: " + year);
        System.out.println("Current Month Value: " + monthValue);
    }
}

In this example, we import the YearMonth class from the java.time package. We then create a YearMonth object using the now() method, which returns the current year and month based on the system clock.

We call the getYear() method on the YearMonth object to retrieve the year value as an integer, and assign it to a variable named year. We also call the getMonthValue() method on the YearMonth object to retrieve the month value as an integer, and assign it to a variable named monthValue.

Finally, we print the resulting year and month value using the println() method of the System.out object.

When we run this code, we get output similar to the following:

Current Year: 2023
Current Month Value: 5

This output shows the current year and month value as integers.

Java YearMonth Example: plus()

Sure, here’s an example of using the plus() method of the YearMonth class in Java:

import java.time.YearMonth;

public class YearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.now();
        System.out.println("Current Year-Month: " + yearMonth);
        YearMonth plusOneMonth = yearMonth.plusMonths(1);
        System.out.println("Year-Month in One Month: " + plusOneMonth);
        YearMonth plusOneYear = yearMonth.plusYears(1);
        System.out.println("Year-Month in One Year: " + plusOneYear);
    }
}

In this example, we import the YearMonth class from the java.time package. We then create a YearMonth object using the now() method, which returns the current year and month based on the system clock.

We print the resulting YearMonth object using the println() method of the System.out object. We then create a new YearMonth object named plusOneMonth, which is the YearMonth one month after the current YearMonth object, using the plusMonths() method. We print this resulting YearMonth object as well.

Next, we create a new YearMonth object named plusOneYear, which is the YearMonth one year after the current YearMonth object, using the plusYears() method. We print this resulting YearMonth object as well.

When we run this code, we get output similar to the following:

Current Year-Month: 2023-05
Year-Month in One Month: 2023-06
Year-Month in One Year: 2024-05

This output shows the current year and month, as well as the YearMonth one month and one year after the current YearMonth object.

Java YearMonth Example: minus()

Sure, here’s an example of using the minus() method of the YearMonth class in Java:

import java.time.YearMonth;

public class YearMonthExample {
    public static void main(String[] args) {
        YearMonth yearMonth = YearMonth.now();
        System.out.println("Current Year-Month: " + yearMonth);
        YearMonth minusOneMonth = yearMonth.minusMonths(1);
        System.out.println("Year-Month One Month Ago: " + minusOneMonth);
        YearMonth minusOneYear = yearMonth.minusYears(1);
        System.out.println("Year-Month One Year Ago: " + minusOneYear);
    }
}

In this example, we import the YearMonth class from the java.time package. We then create a YearMonth object using the now() method, which returns the current year and month based on the system clock.

We print the resulting YearMonth object using the println() method of the System.out object. We then create a new YearMonth object named minusOneMonth, which is the YearMonth one month before the current YearMonth object, using the minusMonths() method. We print this resulting YearMonth object as well.

Next, we create a new YearMonth object named minusOneYear, which is the YearMonth one year before the current YearMonth object, using the minusYears() method. We print this resulting YearMonth object as well.

When we run this code, we get output similar to the following:

Current Year-Month: 2023-05
Year-Month One Month Ago: 2023-04
Year-Month One Year Ago: 2022-05

This output shows the current year and month, as well as the YearMonth one month and one year before the current YearMonth object.