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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.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 !!