Date dNow = new Date( );
SimpleDateFormat ft1 = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");//hh是12小时制;
SimpleDateFormat ft2 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");//HH是24小时制;
System.out.println("当前时间为:"+ft1.format(dNow));
System.out.println("当前时间为:"+ft2.format(new Date()));
The output is:
当前时间为:2023-03-21 04:09:25
当前时间为:2023-03-21 16:09:25
- This line of code establishes the format of the conversion, where yyyy is the complete AD year, MM is the month, dd is the date, and HH:mm:ss is the hour, minute, and second.
Note: Some formats are uppercase, and some are lowercase. For example, MM is the month, mm is the minute; HH is the 24-hour format, and hh is the 12-hour format.