Java format LocalTime to HH:mm:ss

In this source code example, we will you how to format a LocalTime into HH:mm:ss format in Java.

Java format LocalTime to HH:mm:ss


import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {  
        
        // HH:mm:ss
        LocalTime localTime = LocalTime.now();
        DateTimeFormatter formatterLocalTime = DateTimeFormatter.ofPattern("HH:mm:ss");
        String lt1 = formatterLocalTime.format(localTime);

        // or shortly
        String lt2 = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));

        System.out.println(lt1);
        System.out.println(lt2);
    }

}

Output:

17:33:28
17:33:28

Comments