Java time-related operations

Source: Internet
Author: User

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 ();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.