🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
MonthDay in Java, part of the java.time package, represents a combination of a month and a day without a year. It is useful for handling recurring dates like birthdays or anniversaries.
Table of Contents
- What is
MonthDay? - Creating
MonthDayInstances - Common Methods
- Examples of
MonthDay - Conclusion
1. What is MonthDay?
MonthDay is an immutable class that combines a month and a day. It does not store or represent a year, making it ideal for recurring events that occur annually.
2. Creating MonthDay Instances
You can create MonthDay instances in several ways:
MonthDay.now(): Obtains the current month and day from the system clock.MonthDay.of(Month month, int dayOfMonth): Creates an instance with the specified month and day.MonthDay.parse(CharSequence text): Parses a string to aMonthDayusing the ISO-8601 format.
3. Common Methods
getMonth(): Returns the month part of thisMonthDay.getDayOfMonth(): Returns the day of the month.isValidYear(int year): Checks if thisMonthDayis valid for the specified year.withMonth(int month): Returns a copy of thisMonthDaywith the specified month.withDayOfMonth(int dayOfMonth): Returns a copy of thisMonthDaywith the specified day.
4. Examples of MonthDay
Example 1: Getting the Current Month and Day
This example demonstrates how to get the current month and day using MonthDay.now().
import java.time.MonthDay;
public class CurrentMonthDayExample {
public static void main(String[] args) {
MonthDay today = MonthDay.now();
System.out.println("Today's Month and Day: " + today);
}
}
Output:
Today's Month and Day: --06-30
Example 2: Creating a Specific Month and Day
Here, we create a specific MonthDay using MonthDay.of(Month month, int dayOfMonth).
import java.time.Month;
import java.time.MonthDay;
public class SpecificMonthDayExample {
public static void main(String[] args) {
MonthDay birthday = MonthDay.of(Month.JUNE, 15);
System.out.println("Specific Month and Day: " + birthday);
}
}
Output:
Specific Month and Day: --06-15
Example 3: Parsing a MonthDay String
This example shows how to parse a string into a MonthDay using MonthDay.parse(CharSequence text).
import java.time.MonthDay;
public class ParseMonthDayExample {
public static void main(String[] args) {
MonthDay anniversary = MonthDay.parse("--06-15");
System.out.println("Parsed Month and Day: " + anniversary);
}
}
Output:
Parsed Month and Day: --06-15
Example 4: Checking Validity for a Year
In this example, we demonstrate how to check if a MonthDay is valid for a given year using isValidYear(int year).
import java.time.MonthDay;
public class ValidityCheckExample {
public static void main(String[] args) {
MonthDay date = MonthDay.of(2, 29);
boolean isValidIn2023 = date.isValidYear(2023);
boolean isValidIn2024 = date.isValidYear(2024);
System.out.println("Is valid in 2023: " + isValidIn2023);
System.out.println("Is valid in 2024: " + isValidIn2024);
}
}
Output:
Is valid in 2023: false
Is valid in 2024: true
Example 5: Modifying Month and Day
This example shows how to modify the month and day of a MonthDay using withMonth(int month) and withDayOfMonth(int dayOfMonth).
import java.time.MonthDay;
public class ModifyMonthDayExample {
public static void main(String[] args) {
MonthDay original = MonthDay.of(5, 10);
MonthDay modifiedMonth = original.withMonth(8);
MonthDay modifiedDay = original.withDayOfMonth(20);
System.out.println("Original: " + original);
System.out.println("Modified Month: " + modifiedMonth);
System.out.println("Modified Day: " + modifiedDay);
}
}
Output:
Original: --05-10
Modified Month: --08-10
Modified Day: --05-20
Conclusion
The MonthDay class in Java is used for handling dates that recur annually, such as birthdays or anniversaries. It provides methods to manipulate and retrieve information about months and days without the need for a year component. Using MonthDay can simplify date management in applications dealing with recurring events.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment