• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java 8 Date Time API – LocalTime Example

April 10, 2015 //  by Krishna Srinivasan//  Leave a Comment

Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time. In my previous article I have explained about the LocalDate.This example demonstrates how to use one of the new class java.time.LocalTime defined under that date and time package.

If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.

LocalTime

A LocalTime object is an immutable class that represents just a time. The time is normally stored as hours-minutes-seconds. The time is stored in the nanoseconds. For example, the value “11:46.20.4567345” can be stored in a LocalTime object. LocalTime does not store or represent date or time zone. It is just local time seen on the wall clock.

This class defines several methods that are useful for fetching the time related values from the stored object. This example represents few important methods are useful for the programmers in normal scenarios.

  • Class Name : LocalTime
  • Package : java.time
  • Version Released : Java 1.8

LocalTime – Few Methods

  • now() – This static method gets the current time from system clock with the default time zone.
  • now(ZoneId zone) – This static method gets the current time from the system clock with the specified time zone.
  • getHour() – This static method gets the hour field in the current day
  • getMinute() – This static method gets the minute field in the current hour
  • getSecond() – This static method gets the second field in the current minute
  • of(int hour, int minute, int second) – This static method retruns LocalTime instance for the given hour,minute and second
  • isAfter(LocalTime other) – This instance method checks whether the specified time is after the time stored in the LocalTime object
  • plusHours(long hoursToAdd) – This instance method returns the new LocalTime instance by adding the specified hours to the LocalTime object

LocalTime – Example

package javabeat.net;

import java.time.LocalTime;
import java.time.ZoneId;

/**
 * LocalTime Example
 *
 * @author krishna (www.javabeat.net)
 *
 */
public class LocalTimeExample {
	public static void main(String[] args) {
		LocalTime localtimeToday = LocalTime.now();
		LocalTime localTimeZone = LocalTime.now(ZoneId
				.of("America/Los_Angeles"));
		int hour = localtimeToday.getHour();
		int minute = localtimeToday.getMinute();
		int second = localtimeToday.getSecond();
		LocalTime ofMethod = LocalTime.of(10,10,10);
		boolean isAfterMethod = localtimeToday.isAfter(ofMethod);
		LocalTime plusHoursMethod = localtimeToday.plusHours(10);

		System.out.println("LocalTime Example Demo");
		System.out.println("----------------------");
		System.out.println("Current Time : " + localtimeToday);
		System.out.println("Current Time at Zone America/Los_Angeles : "
				+ localTimeZone);
		System.out.println("Hour : " + hour);
		System.out.println("Minute : " + minute);
		System.out.println("Second : " + second);
		System.out.println("Time.of() : " + ofMethod);
		System.out.println("LocalDate().isAfter() : " + isAfterMethod);
		System.out.println("LocalDate().isAfter() : " + isAfterMethod);
		System.out.println("LocalDate().plusHoursMethod() : " + plusHoursMethod);
	}
}

Output for the above program will be:

LocalTime Example Demo
----------------------
Current Time : 07:17:42.435
Current Time at Zone America/Los_Angeles : 18:47:42.443
Hour : 7
Minute : 17
Second : 42
Time.of() : 10:10:10
LocalDate().isAfter() : false
LocalDate().isAfter() : false
LocalDate().plusHoursMethod() : 17:17:42.435

If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.

Category: JavaTag: Java 8

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java 8 Date Time API – LocalDate Example
Next Post: Java 8 Date Time API – Instant Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact