JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 LocalTime example.


LocalTime class is a time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.

LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision.

This class does not store or represent a date or time-zone. Instead, it is a description of the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of LocalTime may have unpredictable results and should be avoided. The equals method should be used for comparisons.

LocalTime class is immutable and thread-safe.

LocalTimeEx
package com.java2novice.datetime;

import java.time.LocalTime;

public class LocalTimeEx {

	public static void main(String[] args) {

		// get current time
		LocalTime currTime = LocalTime.now();
		System.out.println("current time: "+currTime);

		// create time object with values
		LocalTime givenTime = LocalTime.of(2, 30);
		System.out.println("early morning time: "+givenTime);

		// time with seconds
		LocalTime timeWithSec = LocalTime.of(2, 30, 28);
		System.out.println("early morning time with seconds: "+timeWithSec);

		// post lunch time
		LocalTime postLunch = LocalTime.of(15, 30, 28);
		System.out.println("post lunch time with seconds: "+postLunch);

		// converts no of seconds of the day to time
		LocalTime secOfDay = LocalTime.ofSecondOfDay(10160);
		System.out.println("seconds of the day: "+secOfDay);
	}
}

Output:
current time: 15:39:57.860
early morning time: 02:30
early morning time with seconds: 02:30:28
post lunch time with seconds: 15:30:28
seconds of the day: 02:49:20
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
Difference between super() and this()
super() is used to call super class constructor, whereas this() used to call constructors in the same class, means to call parameterized constructors.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.