In this article, we will learn how to get year, month and day fields from LocalDate and Calendar using different methods provided in Java 1.8 version
1. Get Year, Month and Day fields from LocalDate
- Sometimes, we need specific fields from LocalDate like Year or Month or Day instead of whole Date
- To get those fields separately, we can use below listed methods of LocalDate
- getYear() – returns Year field from LocalDate
- getMonth() – returns Month in words field from LocalDate
- getMonthValue() – returns Month in number field from LocalDate
- getDayOfMonth() – returns Day of Month field from LocalDate
- getDayOfWeek() – returns Day of Week field from LocalDate
- getDayOfYear() – returns Day of Year field from LocalDate
- lengthOfMonth() – returns Number of Days in the current Month from LocalDate
- lengthOfYear() – returns Number of Days in the current Year from LocalDate
- isLeapYear() – returns whether current year is leap year or not from LocalDate
- Finally, print above retrieved values to the console
GetFieldsFromLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
public class GetFieldsFromLocalDate {
public static void main(String[] args) {
// get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is = " + localDate);
// 1. get year
int year = localDate.getYear();
System.out.println("\nYear is = " + year);
// 2. get month in words
Month month = localDate.getMonth();
System.out.println("Month in words is = " + month);
// 3. get month in number
int monthValue = localDate.getMonthValue();
System.out.println("Month in number is = " + monthValue);
// 4. get day of month
int day = localDate.getDayOfMonth();
System.out.println("Day of Month is = " + day);
// 5. get day of week
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println("Day of Week is = " + dayOfWeek);
// 6. get day of year
int dayOfYear = localDate.getDayOfYear();
System.out.println("Day of Year is = " + dayOfYear);
// 7. get length of month
int lengthOfMonth = localDate.lengthOfMonth();
System.out.println("Length of Month is = " + lengthOfMonth);
// 8. get lengthOfMonth
int lengthOfYear = localDate.lengthOfYear();
System.out.println("Length of Year is = " + lengthOfYear);
// 9. check leap year ?
boolean isLeapYear = localDate.isLeapYear();
System.out.println("Leap Year ? = " + isLeapYear);
}
}
Output:
Current System Date is = 2022-07-28
Year is = 2022
Month in words is = JULY
Month in number is = 7
Day of Month is = 28
Day of Week is = THURSDAY
Day of Year is = 209
Length of Month is = 31
Length of Year is = 365
Leap Year ? = false
2. Get Year, Month and Day fields from Calendar
- Prior to Java 1.8 version, we have to deal with Date and Calendar for Date/Time handling
- So if we need specific fields from Calendar like Year or Month or Day instead of whole Date, we can use get() method of Calendar passing different constants as listed below,
- get(Calendar.YEAR) – returns Year field from Calendar
- get(Calendar.MONTH) – returns Month field from Calendar
- get(Calendar.DAY_OF_MONTH) – returns Day of Month field from Calendar
- get(Calendar.DAY_OF_WEEK) – returns Day of Week field from Calendar (Sunday being 1 & Saturday being 7)
- get(Calendar.DAY_OF_YEAR) – returns Day of Year field from Calendar
- Finally, print above retrieved values to the console
GetFieldsFromCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.util.Calendar;
import java.util.Date;
public class GetFieldsFromCalendar {
public static void main(String[] args) {
// get current System Date using Date/Calendar
Date today = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
System.out.println("Current System Date/Time is = " + calendar.getTime());
// 1. get year
int year = calendar.get(Calendar.YEAR);
System.out.println("\nYear is = " + year);
// 2. get month
int month = calendar.get(Calendar.MONTH);
System.out.println("Month is = " + month);
// 3. get day of month
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("Day of Month is = " + day);
// 4. get day of week (Sunday=1 and Saturday=7
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("Day of Week is = " + dayOfWeek);
// 5. get day of year
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("Day of Year is = " + dayOfYear);
}
}
Output:
Current System Date/Time is = Thu Jul 28 09:31:32 IST 2022
Year is = 2022
Month is = 6
Day of Month is = 28
Day of Week is = 5
Day of Year is = 209
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!