Java gets the system time, java gets the system
1. Date class
1 Date day = new Date (); 2 SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 3 System. out. println (df. format (day); 4 // or 5 Date date = new Date (); 6 String year = String. format ("% tY", date); 7 String month = String. format ("% tB", date); 8 String day = String. format ("% te", date); 9 System. out. println (year + "-" + month + "-" + day );2. System class
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");2 System.out.println(df.format(System.currentTimeMillis()));3. Calendar class
1 Calendar c = Calendar. getInstance (); // you can modify 2 int year = c. get (Calendar. YEAR); 3 int month = c. get (Calendar. MONTH); 4 int date = c. get (Calendar. DATE); 5 int hour = c. get (Calendar. HOUR_OF_DAY); 6 int minute = c. get (Calendar. MINUTE); 7 int second = c. get (Calendar. SECOND); 8 System. out. println (year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second );
Summary:The System time can be obtained through the Date class, the Calendar class, And the currentTimeMillis in the System. The time format can be set by calling the SimpleDateFormat class and the format method in the String.