beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java LocalTime

By Chaitanya Singh | Filed Under: JavaDateTime

The class LocalTime represents the time without time zone information such as 11:20:45. LocalTime can be used to represent time upto nanosecond precision. For example, 15:40.25.123456789 can be represented by an instance of LocalTime. This class is immutable and thread-safe.

1. A Simple Example of LocalTime class in Java

In this example we are creating the instances of LocalTime by calling now() and of() methods. The now() method returns the current system time while the of() method returns the instance of LocalTime that represents the time for the given hour, minutes, seconds and nanoseconds values.

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
	//now() method is used for getting the current time 
    LocalTime currentTime = LocalTime.now();  
    System.out.println(currentTime);  
    
    /*of() method is used for creating an instance of LocalTime
     * by using the given hour, minutes and seconds values
     */
    LocalTime localTime = LocalTime.of(15,40,55);  
    System.out.println(localTime);  
  }  
}

Output:

21:22:57.555
15:40:55

2. LocalTime Example: Getting hour, minutes, seconds and nanoseconds

In this example, we are demonstrating the use of getHour(), getMinute(), getSecond() and getNano() methods of LocalTime class.

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
    LocalTime localTime = LocalTime.of(15,40,55, 1234);  
    //getting hour from the LocalTime
    System.out.println("Hour: "+localTime.getHour());  
    
    //getting minutes from the LocalTime
    System.out.println("Minute: "+localTime.getMinute());
    
    //getting seconds from the LocalTime
    System.out.println("Second: "+localTime.getSecond());
    
    //getting nano seconds from the LocalTime
    System.out.println("Nano Second: "+localTime.getNano());
  }  
}

Output:

Hour: 15
Minute: 40
Second: 55
Nano Second: 1234

3. LocalTime – plusHours(), plusMinutes(), plusSeconds() and plusNanos()

These methods are used for adding the hours, minutes, seconds and nanoseconds to the local time.

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
	  //time 16:45:55:222222146
	  LocalTime localTime = LocalTime.of(16, 45, 55, 222222146);
	  System.out.println("Given Time: "+localTime);
	  
	  //plusHours() method adds the specified hours to the given local time
	  LocalTime localTime2 = localTime.plusHours(5);
	  System.out.println("Adding 5 hours to the given time: "+ localTime2);
	  
	  //plusMinutes() method adds the specified minutes to the given local time
	  LocalTime localTime3 = localTime.plusMinutes(7);
	  System.out.println("Adding 7 minutes to the given time: "+localTime3);
	  
	  //plusSeconds() method adds the specified seconds to the given local time
	  LocalTime localTime4 = localTime.plusSeconds(2);
	  System.out.println("Adding 2 seconds to the given time : "+localTime4);
	  
	  //plusNanos() method adds the specified nanoseconds to the given local time
	  LocalTime localTime5 = localTime.plusNanos(14);
	  System.out.println("Adding 14 nanoseconds to the given time: "+localTime5);
  }  
}

Output:

Given Time: 16:45:55.222222146
Adding 5 hours to the given time: 21:45:55.222222146
Adding 7 minutes to the given time: 16:52:55.222222146
Adding 2 seconds to the given time : 16:45:57.222222146
Adding 14 nanoseconds to the given time: 16:45:55.222222160

4. LocalTime – minusHours(), minusMinutes(), minusSeconds() and minusNanos()

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
	  //time 16:45:55:222222146
	  LocalTime localTime = LocalTime.of(16, 45, 55, 222222146);
	  System.out.println("Given Time: "+localTime);
	  
	  //minusHours() method subtracts the specified hours from the given time
	  LocalTime localTime2 = localTime.minusHours(3);
	  System.out.println("Subtracting 3 hours from the given time: "+ localTime2);
	  
	  //minusMinutes() method subtracts the specified minutes from the given time
	  LocalTime localTime3 = localTime.minusMinutes(5);
	  System.out.println("Subtracting 5 minutes from the given time: "+localTime3);
	  
	  //minusSeconds() method subtracts the specified seconds from the given time
	  LocalTime localTime4 = localTime.minusSeconds(10);
	  System.out.println("Subtracting 10 seconds from the given time : "+localTime4);
	  
	  //minusNanos() method subtracts the specified nanoseconds from the given time
	  LocalTime localTime5 = localTime.minusNanos(16);
	  System.out.println("Subtracting 16 nanoseconds from the given time: "+localTime5);
  }  
}

Output:

Given Time: 16:45:55.222222146
Subtracting 3 hours from the given time: 13:45:55.222222146
Subtracting 5 minutes from the given time: 16:40:55.222222146
Subtracting 10 seconds from the given time : 16:45:45.222222146
Subtracting 16 nanoseconds from the given time: 16:45:55.222222130

5. LocalTime methods – isBefore() and isAfter() examples

These methods check whether the time is before or after the specified time.

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
	  //time1 16:45:55:222222146
	  LocalTime localTime = LocalTime.of(16, 45, 55, 222222146);
	  System.out.println("Given Time: "+localTime);
	  
	  //time2 current time
	  LocalTime localTime2 = LocalTime.now();
	  System.out.println("Current Time: "+localTime2);
	  
	  /* public boolean isBefore(LocalTime other): Checks if 
	   * this time is before the specified time.
	   */
	  System.out.println("current time before given time? "+
	  localTime2.isBefore(localTime));
	  
	  /* public boolean isAfter(LocalTime other): Checks if 
	   * this time is after the specified time.
	   */
	  System.out.println("current time after given time? "+
	  localTime2.isAfter(localTime));
  }  
}

Output:

Given Time: 16:45:55.222222146
Current Time: 22:15:48.842
current time before given time? false
current time after given time? true

6. LocalTime class methods – withHour(), withMinute(), withSecond() and withNano()

These methods are used for altering the hours, minutes, seconds and nanoseconds values in the returned copy of the given local time.

import java.time.LocalTime;  
public class Example {  
  public static void main(String[] args) { 
	  //time 19:25:44:111122223
	  LocalTime localTime = LocalTime.of(19, 25, 44, 111122223);
	  System.out.println("Given Time: "+localTime);

	  /* public LocalTime withHour(int hour): This method returns the copy
	   * of the local time with the hour altered
	   */
	  LocalTime localTime2 = localTime.withHour(11);
	  System.out.println("LocalTime with hours altered: "+localTime2);
	  
	  /* public LocalTime withMinute(int minute): This method returns the copy
	   * of the local time with the minutes altered
	   */
	  LocalTime localTime3 = localTime.withMinute(30);
	  System.out.println("LocalTime with minutes altered: "+localTime3);
	  
	  /* public LocalTime withSecond(int second): This method returns the copy
	   * of the local time with the seconds altered
	   */
	  LocalTime localTime4 = localTime.withSecond(55);
	  System.out.println("LocalTime with seconds altered: "+localTime4);
	  
	  /* public LocalTime withNano(int nanosecond): This method returns the copy
	   * of the local time with the nanoSeconds altered
	   */
	  LocalTime localTime5 = localTime.withNano(999);
	  System.out.println("LocalTime with nanoseconds altered: "+localTime5);
  }  
}

Output:

Given Time: 19:25:44.111122223
LocalTime with hours altered: 11:25:44.111122223
LocalTime with minutes altered: 19:30:44.111122223
LocalTime with seconds altered: 19:25:55.111122223
LocalTime with nanoseconds altered: 19:25:44.000000999

Reference:

LocalTime – JavaDoc

Leave a Reply Cancel reply

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

Java Date and Time

  • Java Date Time
  • Java LocalDate
  • Java LocalTime
  • Java LocalDateTime
  • Java ZonedDateTime
  • Java DateTimeFormatter
  • current date time

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap