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:
- 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>
: AllowsYearMonth
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 aYearMonth
object with the specified year and month values.getYear()
: Returns the year value of theYearMonth
object.getMonth()
: Returns the month value of theYearMonth
object as aMonth
enum.getMonthValue()
: Returns the month value of theYearMonth
object as an integer.plusYears(long years)
: Returns a newYearMonth
object with the specified number of years added.minusMonths(long months)
: Returns a newYearMonth
object with the specified number of months subtracted.isAfter(YearMonth other)
: Returns true if theYearMonth
object is after the specifiedYearMonth
object.isBefore(YearMonth other)
: Returns true if theYearMonth
object is before the specifiedYearMonth
object.equals(Object obj)
: Returns true if theYearMonth
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:
static YearMonth now()
: Returns the current year and month based on the system clock.static YearMonth now(ZoneId zone)
: Returns the current year and month based on the specified time zone.static YearMonth parse(CharSequence text)
: Parses the specified text in the format “uuuu-MM” to create aYearMonth
object.static YearMonth of(int year, int month)
: Returns aYearMonth
object representing the specified year and month.int getYear()
: Returns the year value of theYearMonth
object.Month getMonth()
: Returns the month value of theYearMonth
object as aMonth
enum.int getMonthValue()
: Returns the month value of theYearMonth
object as an integer.YearMonth plusYears(long years)
: Returns a newYearMonth
object with the specified number of years added.YearMonth plusMonths(long months)
: Returns a newYearMonth
object with the specified number of months added.YearMonth minusYears(long years)
: Returns a newYearMonth
object with the specified number of years subtracted.YearMonth minusMonths(long months)
: Returns a newYearMonth
object with the specified number of months subtracted.boolean isAfter(YearMonth other)
: Returns true if theYearMonth
object is after the specifiedYearMonth
object.boolean isBefore(YearMonth other)
: Returns true if theYearMonth
object is before the specifiedYearMonth
object.boolean equals(Object obj)
: Returns true if theYearMonth
object is equal to the specified object.int compareTo(YearMonth other)
: Compares thisYearMonth
object with anotherYearMonth
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.String toString()
: Returns a string representation of theYearMonth
object in the format “uuuu-MM”.String format(DateTimeFormatter formatter)
: Formats theYearMonth
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.