How to format time hours-minutes-seconds in Java

1 Answer

0 votes
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Create a LocalDateTime object
        LocalDateTime date = LocalDateTime.of(2025, 7, 18, 15, 31, 24);

        // Format the time as HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String x = date.format(formatter);

        System.out.println(x);
    }
}



/*
run:

15:31:24

*/

 



answered Jul 22, 2025 by avibootz

Related questions

1 answer 149 views
1 answer 94 views
1 answer 87 views
1 answer 95 views
1 answer 104 views
1 answer 114 views
1 answer 88 views
...