📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The withYear()
method in Java, part of the java.time.LocalDate
class, is used to return a copy of the LocalDate
instance with the year altered. This method is useful for creating a new date with a specific year while keeping the month and day unchanged.
Table of Contents
- Introduction
withYear()
Method Syntax- Understanding
withYear()
- Examples
- Basic Usage
- Using
withYear()
for Validations
- Real-World Use Case
- Conclusion
Introduction
The withYear()
method allows you to create a new LocalDate
instance with the year altered. This is particularly useful when you need to adjust the year while keeping the month and day unchanged.
withYear() Method Syntax
The syntax for the withYear()
method is as follows:
public LocalDate withYear(int year)
Parameters:
year
: The year to set in the resultingLocalDate
.
Returns:
- A
LocalDate
representing the specified year.
Throws:
DateTimeException
if the year value is invalid.
Understanding withYear()
The withYear()
method returns a new LocalDate
instance with the year altered to the specified value, while the month and day remain the same. The method ensures that the year value is valid.
Examples
Basic Usage
To demonstrate the basic usage of withYear()
, we will change the year of a LocalDate
instance.
Example
import java.time.LocalDate; public class LocalDateWithYearExample { public static void main(String[] args) { LocalDate date = LocalDate.of(2024, 6, 27); LocalDate newDate = date.withYear(2025); // Change the year to 2025 System.out.println("Original Date: " + date); System.out.println("New Date: " + newDate); } }
Output:
Original Date: 2024-06-27 New Date: 2025-06-27
Using withYear()
for Validations
This example shows how to use the withYear()
method to ensure that a date is set to a specific year, such as the current year.
Example
import java.time.LocalDate; public class SpecificYearExample { public static void main(String[] args) { LocalDate date = LocalDate.of(2024, 6, 27); LocalDate currentYearDate = date.withYear(LocalDate.now().getYear()); // Set to the current year System.out.println("Original Date: " + date); System.out.println("Current Year Date: " + currentYearDate); } }
Output:
Original Date: 2024-06-27 Current Year Date: 2024-06-27
Real-World Use Case
Adjusting Dates for Annual Events
In real-world applications, the withYear()
method can be used to adjust dates for annual events, such as setting all event dates to a specific year.
Example
import java.time.LocalDate; public class AnnualEventExample { public static void main(String[] args) { LocalDate eventDate = LocalDate.of(2024, 6, 27); LocalDate nextYearEventDate = eventDate.withYear(2025); // Adjust to the next year System.out.println("Original Event Date: " + eventDate); System.out.println("Next Year Event Date: " + nextYearEventDate); } }
Output:
Original Event Date: 2024-06-27 Next Year Event Date: 2025-06-27
Conclusion
The LocalDate.withYear()
method is used to create a new LocalDate
instance with a specific year. This method is particularly useful for adjusting the year while keeping the month and day unchanged. By understanding and using this method, you can effectively manage and manipulate date-based data in your Java applications.
Comments
Post a Comment
Leave Comment