LocalTime toString() method in Java with Examples

The toString() method of LocalTime class is used to represents this time as a String, such as 20:15:34.111.

Following ISO-8601 formats are used for representation:

  • HH:mm
  • HH:mm:ss
  • HH:mm:ss.SSS
  • HH:mm:ss.SSSSSS
  • HH:mm:ss.SSSSSSSSS

This method is derived from the Object Class and behaves in a similar way.

Syntax:

public String toString()

Parameters: This method accepts no parameter.



Return value: This method returns a String representation of this LocalTime.

Below programs illustrate the toString() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalTime.toString() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("19:34:50.63");
  
        // print LocalTime
        System.out.println("LocalTime : "
                           + time.toString());
    }
}

chevron_right


Output:

LocalTime : 19:34:50.630

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalTime.toString() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("01:00:01");
  
        // print LocalTime
        System.out.println("Old LocalTime: "
                           + time.toString());
  
        // add 600 Minutes using plusMinutes()
        LocalTime value = time.plusMinutes(600);
  
        // print result
        System.out.println("New LocalTime: "
                           + value.toString());
    }
}

chevron_right


Output:

Old LocalTime: 01:00:01
New LocalTime: 11:00:01

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#toString()

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.