Java 8 – How to get Date and Time fields from LocalDateTime ?

In this article, we will learn how to get Date and Time fields from LocalDateTime and Calendar using different methods provided in Java 1.8 version

1. Get Date & Time fields from LocalDateTime :

  • Sometimes, we need specific Date & Time fields from LocalDateTime like Year or Month or Day or Hour or Minute or Second or Nanosecond instead of whole LocalDateTime
  • To get those fields separately, we can use below listed methods of LocalDateTime
    • getYear() – returns Year field from LocalDateTime
    • getMonth() – returns Month in words field from LocalDateTime
    • getMonthValue() – returns Month in number field from LocalDateTime as Month enum
    • getDayOfMonth() – returns Day of Month field from LocalDateTime
    • getDayOfWeek() – returns Day of Week field from LocalDateTime
    • getDayOfYear() – returns Day of Year field from LocalDateTime
    • getHour() – returns Hour field from LocalDateTime
    • getMinute() – returns Minute field from LocalDateTime
    • getSecond() – returns Second field from LocalDateTime
    • getNano() – returns Nanosecond field from LocalDateTime
  • Finally, print above retrieved values to the console

GetDareAndTimeFieldsFromLocalDateTime.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;

public class GetDareAndTimeFieldsFromLocalDateTime {

	public static void main(String[] args) {

		// get current System Date
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current System Date/time is = " + localDateTime);


		// 1. Date part
		System.out.println("\n1. Date fields = " + localDateTime.toLocalDate());


		// 1.1 get year
		int year = localDateTime.getYear();
		System.out.println("\nYear is = " + year);


		// 1.2 get month in words
		Month month = localDateTime.getMonth();
		System.out.println("Month in words is = " + month);


		// 1.3 get month in number
		int monthValue = localDateTime.getMonthValue();
		System.out.println("Month in number is = " + monthValue);


		// 1.4 get day of month
		int day = localDateTime.getDayOfMonth();
		System.out.println("Day of Month is = " + day);


		// 1.5 get day of week
		DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
		System.out.println("Day of Week is = " + dayOfWeek);


		// 1.6 get day of year
		int dayOfYear = localDateTime.getDayOfYear();
		System.out.println("Day of Year is = " + dayOfYear);


		// 2. Time part
		System.out.println("\n2. Time fields = " + localDateTime.toLocalTime());


		// 2.1 get hour
		int hour = localDateTime.getHour();
		System.out.println("\nHour is = " + hour);


		// 2.2 get minute
		int minute = localDateTime.getMinute();
		System.out.println("Minute is = " + minute);


		// 2.3  get second
		int second = localDateTime.getSecond();
		System.out.println("Second is = " + second);


		// 2.4 get nanosecond
		int nano = localDateTime.getNano();
		System.out.print("Nano is = " + nano);
	}
}

Output:

Current System Date/time is = 2022-08-08T15:22:37.956029300

1. Date fields = 2022-08-08

Year is = 2022
Month in words is = AUGUST
Month in number is = 8
Day of Month is = 8
Day of Week is = MONDAY
Day of Year is = 220

2. Time fields = 15:22:37.956029300

Hour is = 15
Minute is = 22
Second is = 37
Nano is = 956029300

2. Get Date & Time fields from Date/Calendar :

  • Prior to Java 1.8 version, we have to deal with Date and Calendar for Date/Time handling
  • So if we need specific Date & Time fields like Year or Month or Day or Hour or Minute or Second or Millisecond 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
    • get(Calendar.HOUR) – returns Hour field from Calendar
    • get(Calendar.MINUTE) – returns Minute field from Calendar
    • get(Calendar.SECOND) – returns Second field from Calendar
    • get(Calendar.MILLISECOND) – returns Millisecond field from Calendar
    • get(Calendar.AM_PM) – returns 0 (AM) or 1 (PM) field from Calendar
  • Finally, print above retrieved values to the console

GetDareAndTimeFieldsFromCalendar.java

package in.bench.resources.java8.localdatetime.examples;

import java.util.Calendar;
import java.util.Date;

public class GetDareAndTimeFieldsFromCalendar {

	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. Date part
		System.out.println("\n1. Date fields :-");


		// 1.1 get year
		int year = calendar.get(Calendar.YEAR);
		System.out.println("\nYear is = " + year);


		// 1.2 get month
		int month = calendar.get(Calendar.MONTH);
		System.out.println("Month is = " + month);


		// 1.3 get day of month
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		System.out.println("Day of Month is = " + day);


		// 1.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);


		// 1.5 get day of year
		int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
		System.out.println("Day of Year is = " + dayOfYear);


		// 2. Time part
		System.out.println("\n2. Time fields :-");


		// 2.1 get hour
		int hour = calendar.get(Calendar.HOUR);
		System.out.println("\nHour is = " + hour);


		// 2.2 get minute
		int minute = calendar.get(Calendar.MINUTE);
		System.out.println("Minute is = " + minute);


		// 2.3 get second
		int second = calendar.get(Calendar.SECOND);
		System.out.println("Second is = " + second);


		// 2.4 get millisecond
		int millisecond = calendar.get(Calendar.MILLISECOND);
		System.out.println("MilliSecond is = " + millisecond);


		// 2.5 get AM or PM
		int amOrPm = calendar.get(Calendar.AM_PM);
		System.out.print("AM or PM ? = " + (amOrPm==1?"PM":"AM"));
	}
}

Output:

Current System Date/Time is = Mon Aug 08 15:22:58 IST 2022

1. Date fields :-

Year is = 2022
Month is = 7
Day of Month is = 8
Day of Week is = 2
Day of Year is = 220

2. Time fields :-

Hour is = 3
Minute is = 22
Second is = 58
MilliSecond is = 477
AM or PM ? = PM

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form LocalDateTime passing Date and Time fields ?
Java 8 – What are all the Temporal Units supported by LocalDate ?