There are two main ways, of which the use of Date is better to control, the code is as follows:
//Use Calendar Calendar now = Calendar.getInstance(); System.out.println("年:" + now.get(Calendar.YEAR)); System.out.println("月:" + (now.get(Calendar.MONTH) + 1)); System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + now.get(Calendar.MINUTE)); System.out.println("Seconds:" + now.get(Calendar.SECOND));
Output:
Year: 2018 -
Month: 5
:4
:22
:48
Seconds:28
// use Date Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current time: " + sdf.format(d));
Output:
Current time: 2018-05-04 22:48:28