Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time in HHMM format

Tags:

java

date

I want to use time as a HHMM format, like in the military's 1600 hrs for 4 PM. I was curious: Is there a way of getting it so by some dateformatter?

Feel free to recommend any third-party library or pure Java API solution.

like image 332
Sachin Verma Avatar asked Dec 01 '22 17:12

Sachin Verma


2 Answers

Use the java.text.SimpleDateFormat

Date date = new Date();
DateFormat format = new SimpleDateFormat("HHmm");
System.out.println(format.format(date));
like image 92
René Link Avatar answered Dec 12 '22 06:12

René Link


LocalTime

Current time using java.time.LocalTime and formatting using java.time.format.DateTimeFormatter ( java 8)

LocalTime time = LocalTime.now();
String t = time.format(DateTimeFormatter.ofPattern("HH:mm"));
System.out.println(t);

//output
05:41
like image 31
Ayush Chaurasia Avatar answered Dec 12 '22 06:12

Ayush Chaurasia