🎓 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
Month in Java, part of the java.time package, is an enum representing the 12 months of the year. It provides various methods for manipulating and retrieving information about months.
Table of Contents
- What is
Month? - Using
Month - Common Methods
- Examples of
Month - Conclusion
1. What is Month?
Month is an enum that represents the months of the year, from January to December. It is useful for working with dates and performing month-based calculations.
2. Using Month
You can use Month to access specific months, perform arithmetic operations, and retrieve information about them.
3. Common Methods
Month.of(int month): Obtains an instance ofMonthfrom an integer (1-12).plus(long monthsToAdd): Returns the month that is a specified number of months after the current one.minus(long monthsToSubtract): Returns the month that is a specified number of months before the current one.length(boolean leapYear): Returns the length of the month in days, accounting for leap years.getValue(): Returns the numeric value of the month (1-12).
4. Examples of Month
Example 1: Accessing a Specific Month
This example demonstrates how to access a specific month using Month.of(int month).
import java.time.Month;
public class SpecificMonthExample {
public static void main(String[] args) {
Month month = Month.of(6); // June
System.out.println("Month: " + month);
}
}
Output:
Month: JUNE
Example 2: Adding and Subtracting Months
Here, we add and subtract months from a specific Month instance using plus(long monthsToAdd) and minus(long monthsToSubtract).
import java.time.Month;
public class AddSubtractMonthsExample {
public static void main(String[] args) {
Month month = Month.JUNE;
Month nextMonth = month.plus(1);
Month previousMonth = month.minus(1);
System.out.println("Current Month: " + month);
System.out.println("Next Month: " + nextMonth);
System.out.println("Previous Month: " + previousMonth);
}
}
Output:
Current Month: JUNE
Next Month: JULY
Previous Month: MAY
Example 3: Getting the Length of a Month
This example shows how to retrieve the length of a month in days, considering leap years.
import java.time.Month;
public class LengthOfMonthExample {
public static void main(String[] args) {
Month month = Month.FEBRUARY;
int lengthInLeapYear = month.length(true);
int lengthInNonLeapYear = month.length(false);
System.out.println("Length in Leap Year: " + lengthInLeapYear);
System.out.println("Length in Non-Leap Year: " + lengthInNonLeapYear);
}
}
Output:
Length in Leap Year: 29
Length in Non-Leap Year: 28
Example 4: Getting the Numeric Value of a Month
In this example, we retrieve the numeric value of a month using getValue().
import java.time.Month;
public class MonthValueExample {
public static void main(String[] args) {
Month month = Month.DECEMBER;
int value = month.getValue();
System.out.println("Numeric Value of Month: " + value);
}
}
Output:
Numeric Value of Month: 12
Conclusion
The Month enum in Java is used for working with months in a year. It provides methods for accessing specific months, performing arithmetic operations, and retrieving month-related information. Using Month can lead to more readable and efficient code when dealing with date and time operations involving months.
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