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.
Use the java.text.SimpleDateFormat
Date date = new Date();
DateFormat format = new SimpleDateFormat("HHmm");
System.out.println(format.format(date));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With