Java Data Type How to - Format time as '9:46'








Question

We would like to know how to format time as '9:46'.

Answer

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
//from w  w w. ja  va2s.com
public class Main {

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

  }

}

The code above generates the following result.