🎓 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
In Java development, one common requirement is to find the first day of a month. Whether it's for scheduling tasks, generating reports, or calculating durations, getting the first date of any given month is a fundamental operation. Java's rich set of date and time APIs makes this task straightforward. In this blog post, we'll explore how to get the first date of a month in Java.
Using Java's Date and Time API
Java 8 introduced a new Date and Time API, which is more robust and intuitive compared to the old java.util.Date and java.util.Calendar. This API, located in the java.time package, provides LocalDate and other classes that make date operations more straightforward.
Finding the First Day of the Current Month
To find the first day of the current month, we can use LocalDate and its methods.
Example:
import java.time.LocalDate;
public class FirstDayOfMonth {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate firstDayOfMonth = today.withDayOfMonth(1);
System.out.println("First day of the current month: " + firstDayOfMonth);
}
}
In this code, LocalDate.now() gives us the current date. The withDayOfMonth(1) method changes the day of the month to 1, giving us the first day of the current month. Getting the First Date of Any Specified Month
Example:
import java.time.LocalDate;
public class FirstDayOfMonth {
public static void main(String[] args) {
int year = 2024;
int month = 01; // October
LocalDate firstDayOfJan2024 = LocalDate.of(year, month, 1);
System.out.println("First day of Jan 2024: " + firstDayOfJan2024);
}
}
This function creates a LocalDate object representing the first day of Jan 2024. Handling Edge Cases
Example with Exception Handling:
import java.time.LocalDate;
import java.time.DateTimeException;
public class FirstDayOfMonth {
public static void main(String[] args) {
try {
LocalDate firstDay = getFirstDayOfMonth(2021, 13); // Invalid month
System.out.println("First Day: " + firstDay);
} catch (DateTimeException e) {
System.out.println("Invalid month or year provided: " + e.getMessage());
}
}
private static LocalDate getFirstDayOfMonth(int year, int month) {
return LocalDate.of(year, month, 1);
}
}
In this example, we handle DateTimeException to catch any invalid month or year arguments.Conclusion
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