Java 8 – How to get Hour, Minute and Second fields from LocalTime ?

In this article, we will learn how to get hour, minute, second, nanosecond and millisecond fields from LocalTime and Calendar using different methods provided in Java 1.8 version

1. Get Hour, Minute, Second and Nano fields from LocalTime :

  • Sometimes, we need specific fields from LocalTime like Hour, Minute, Second and Nanosecond instead of whole Time
  • To get those fields separately, we can use below listed methods of LocalTime
    • getHour() – returns Hour field from LocalTime
    • getMinute() – returns Minute field from LocalTime
    • getSecond() – returns Second field from LocalTime
    • getNano() – returns Nanosecond field from LocalTime
  • Finally, print above retrieved values to the console

GetFieldsFromLocalTime.java

package in.bench.resources.java8.current.date.time.fields;

import java.time.LocalTime;

public class GetFieldsFromLocalTime {

	public static void main(String[] args) {

		// get current System Date
		LocalTime localTime = LocalTime.now();
		System.out.println("Current System Time is = " + localTime);


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


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


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


		// 4. get nano
		int nano = localTime.getNano();
		System.out.println("Nano is = " + nano);
	}
}

Output:

Current System Time is = 19:28:12.974272100

Hour is = 19
Minute is = 28
Second is = 12
Nano is = 974272100

2. Get Hour, Minute, Second and Milli 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 Hour, Minute, Second and Millisecond instead of whole Time, we can use get() method of Calendar passing different constants as listed below,
    • 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

GetTimeFieldsFromCalendar.java

package in.bench.resources.java8.current.date.time.fields;

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

public class GetTimeFieldsFromCalendar {

	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 hour
		int hour = calendar.get(Calendar.HOUR);
		System.out.println("\nHour is = " + hour);


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


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


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


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

Output:

Current System Date/Time is = Wed Jul 27 19:28:31 IST 2022

Hour is = 7
Minute is = 28
Second is = 31
MilliSecond is = 905
AM or PM ? = PM

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
Java 8 – How to compare two LocalDate instances ?