Open In App

LocalTime toString() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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());
    }
}


Output:

LocalTime : 19:34:50.630

Program 2:




// 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());
    }
}


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()



Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads