Java gets the year, month, day, hour, minute, and number of seconds of the current time.
1 Public Static voidGetDateTime ()throwsparseexception{2Calendar now =calendar.getinstance ();3System.out.println ("Year:" +Now.get (calendar.year));4System.out.println ("Month:" + (Now.get (calendar.month) + 1) + "");5System.out.println ("Day:" +Now.get (Calendar.day_of_month));6System.out.println ("When:" +Now.get (Calendar.hour_of_day));7System.out.println ("min:" +Now.get (Calendar.minute));8System.out.println ("Seconds:" +Now.get (Calendar.second));9System.out.println ("Current time milliseconds:" +Now.gettimeinmillis ());Ten System.out.println (Now.gettime ()); One ADate d =NewDate (); - System.out.println (d); -SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); theString Datenowstr =Sdf.format (d); -SYSTEM.OUT.PRINTLN ("Formatted date:" +datenowstr); - -String str = "2012-1-13 17:26:33";//the same format as the SDF definition above +Date today =sdf.parse (str); -System.out.println ("string turns into date:" +today); +}
Output Result:
2014-12-26
Year: 2014
Month: 12
Day: 26
Hours: 15
Points: 6
Seconds: 35
Current time in milliseconds: 1419577595014
Fri Dec 15:06:35 CST 2014
Fri Dec 15:06:35 CST 2014
Date formatted: 2014-12-26 15:06:35
String converted to date: Fri Jan 17:26:33 CST 2012
Java gets the month and day method of the current time