Friday, October 24, 2025
HomeLanguagesJavaLocalTime toString() method in Java with Examples

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:




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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS