Java MonthDay class

Java MonthDay class

Java MonthDay class is an immutable date-time object that represents the combination of a month and day-of-month. It inherits Object class and implements the Comparable interface.


Java MonthDay class declaration

Let’s see the declaration of java.time.MonthDay class.

  1. public final class MonthDay extends Object
  2. implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable

Methods of Java MonthDay

Method Description
LocalDate atYear(int year) It is used to combine this month-day with a year to create a LocalDate.
String format(DateTimeFormatter formatter) It is used to format this month-day using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this month-day as an int.
boolean isValidYear(int year) It is used to check if the year is valid for this month-day.
static MonthDay now() It is used to obtain the current month-day from the system clock in the default time-zone.
static MonthDay of(int month, int dayOfMonth) It is used to obtain an instance of MonthDay.
ValueRange range(TemporalField field) It is used to get the range of valid values for the specified field.

 

Java MonthDay class Example

  1. import java.time.*;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     MonthDay month = MonthDay.now();
  5.     LocalDate date = month.atYear(1994);
  6.     System.out.println(date);
  7.   }
  8. }

Output:

1994-01-17

Java MonthDay class Example: isValidYear()

  1. import java.time.*;
  2. public class LocalDateExample {
  3.   public static void main(String[] args) {
  4.     MonthDay month = MonthDay.now();
  5.     boolean b = month.isValidYear(2012);
  6.     System.out.println(b);
  7.   }
  8. }

Output:

true

Java MonthDay class Example: get()

  1. import java.time.*;
  2. import java.time.temporal.*;
  3. public class LocalDateExample{
  4.   public static void main(String[] args) {
  5.     MonthDay month = MonthDay.now();
  6.     long n = month.get(ChronoField.MONTH_OF_YEAR);
  7.     System.out.println(n);
  8.   }
  9. }

Output:

1

Java MonthDay class Example: range()

  1. import java.time.*;
  2. import java.time.temporal.*;
  3. public class MonthDayExample {
  4.   public static void main(String[] args) {
  5.     MonthDay month = MonthDay.now();
  6.     ValueRange r1 = month.range(ChronoField.MONTH_OF_YEAR);
  7.     System.out.println(r1);
  8.     ValueRange r2 = month.range(ChronoField.DAY_OF_MONTH);
  9.     System.out.println(r2);
  10.   }
  11. }

Output:

1 - 12
1 - 31
Next TopicJava OffsetTime
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java MonthDay class Java MonthDay class is an immutable date-time object that represents the combination of a month and day-of-month. It inherits Object class and implements the Comparable interface. Java MonthDay class declaration Let’s see the declaration of java.time.MonthDay class. public final class MonthDay extends Object implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable Methods of Java MonthDay Method Description LocalDate atYear(int year) It is used to […]

Tags:

....
...