Java Data Type How to - Format time as '09:45'








Question

We would like to know how to format time as '09:45'.

Answer

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/*from w w  w .  j  a va  2s  .com*/
public class Main {

  public static void main(String[] args) {
    LocalTime d = LocalTime.now();
    DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
    
    System.out.println(TIME_FORMATTER.format(d));

  }

}

The code above generates the following result.