We would like to know how to format date time as '[14:30:53]'.
import java.time.LocalTime; //from www. j a v a 2 s. com public class Main { public static void main(String[] args) { System.out.println(getTime()); } public static String getTime() { return String.format("[%s:%s:%s]", zeroOne(LocalTime.now().getHour()), zeroOne(LocalTime.now().getMinute()), zeroOne(LocalTime.now() .getSecond())); } public static String zeroOne(int second) { if (second < 10) { return "0" + second; } return String.valueOf(second); } }
The code above generates the following result.