Java time-related operations
1. Time to format // control time format. HH indicates that the time is in the 24-hour format, and hh indicates that the 12-hour SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss: SSS "); Date date = new Date (). // Convert Date to StringString begin = sdf. format (date); // String to DateDate abc = sdf. parse (begin); 2. modification time: Calendar cal = Calendar. getInstance (); cal. setTime (new Date (); // obtain the current time; cal. add (Calendar. YEAR, + 2); // YEAR plus 2cal. add (Calendar. MONTH, + 1); // Add 1cal to the MONTH. add (Calendar. DAY_OF_MONTH, + 7); cal. add (Calendar. HOUR, + 23); cal. add (Calendar. MINUTE, + 59); cal. add (Calendar. SECOND, + 59); Date d = cal. getTime (); 3. Calculate the Date difference: SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date begin = null; // Date end = null; // if compared with the current time: cal. getTime () try {begin = df. parse ("11:30:24"); end = df. parse ("11:31:25");} catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();} long between = (end. getTime ()-begin. getTime ()/1000; // divide by 1000 to convert to the second int day = (int) (between/(24*3600 )); // The total number of days of the difference int hour = (int) (between % (24*3600)/3600); // The remaining hour int minute = (int) (between % 3600/60 ); // The remaining score is 4. The default value of the set method is 23: 59: 59 public void setEndtimeTw0 (Date endtime) {Calendar cal = Calendar. getInstance (); cal. setTime (endtime); cal. add (Calendar. HOUR, + 23); cal. add (Calendar. MINUTE, + 59); cal. add (Calendar. SECOND, + 59); this. endtime = cal. getTime ();}