🎓 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
The atYear() method in Java, part of the java.time.MonthDay class, returns a LocalDate object formed from the month-day and the specified year. This method is useful for creating a complete date from a month-day and a year.
Table of Contents
- Introduction
atYear()Method Syntax- Understanding
atYear() - Examples
- Basic Usage
- Using
atYear()in Conditional Statements
- Real-World Use Case
- Conclusion
Introduction
The atYear() method allows you to create a LocalDate instance by combining a MonthDay with a specified year. This is particularly useful when you need to construct a full date from a month-day and a year.
atYear() Method Syntax
The syntax for the atYear() method is as follows:
public LocalDate atYear(int year)
Parameters:
year: The year to use, fromYear.MIN_VALUEtoYear.MAX_VALUE.
Returns:
- A
LocalDateformed from the month-day and the specified year, not null.
Throws:
DateTimeExceptionif the day-of-month is invalid for the specified year.
Understanding atYear()
The atYear() method combines the MonthDay instance with the specified year to create a LocalDate. This is useful for handling scenarios where you need to work with month-day combinations and then convert them into full dates by specifying a year.
Examples
Basic Usage
To demonstrate the basic usage of atYear(), we will create a LocalDate from a MonthDay and a specified year.
Example
import java.time.LocalDate;
import java.time.MonthDay;
public class MonthDayAtYearExample {
public static void main(String[] args) {
MonthDay monthDay = MonthDay.of(6, 15); // June 15
int year = 2023;
LocalDate date = monthDay.atYear(year);
System.out.println("MonthDay: " + monthDay);
System.out.println("Year: " + year);
System.out.println("LocalDate: " + date);
}
}
Output:
MonthDay: --06-15
Year: 2023
LocalDate: 2023-06-15
Using atYear() in Conditional Statements
This example shows how to use the atYear() method in conditional statements to perform actions based on the resulting LocalDate.
Example
import java.time.LocalDate;
import java.time.MonthDay;
public class MonthDayConditionalExample {
public static void main(String[] args) {
MonthDay monthDay = MonthDay.of(2, 29); // February 29
int year = 2024; // Leap year
LocalDate date = monthDay.atYear(year);
if (date.isLeapYear()) {
System.out.println(date + " is in a leap year.");
} else {
System.out.println(date + " is not in a leap year.");
}
}
}
Output:
2024-02-29 is in a leap year.
Real-World Use Case
Scheduling Recurring Events
In real-world applications, the atYear() method can be used to schedule recurring events by combining a MonthDay with different years.
Example
import java.time.LocalDate;
import java.time.MonthDay;
public class EventSchedulerExample {
public static void main(String[] args) {
MonthDay eventMonthDay = MonthDay.of(12, 25); // December 25
int[] years = {2023, 2024, 2025};
System.out.println("Scheduled events:");
for (int year : years) {
LocalDate eventDate = eventMonthDay.atYear(year);
System.out.println(eventDate);
}
}
}
Output:
Scheduled events:
2023-12-25
2024-12-25
2025-12-25
Conclusion
The MonthDay.atYear() method is used to create a LocalDate instance by combining a MonthDay with a specified year. This method is particularly useful for constructing full dates from month-day combinations and years. By understanding and using the atYear() method, you can effectively manage and manipulate date-related data in your Java applications.
Comments
Post a Comment
Leave Comment