Java date Tool

Source: Internet
Author: User

Here I summarize the date processing methods in java.

 

/*** Format the date according to yyyy-MM-dd HH: mm: ss * you can customize * @ param Date * @ return */public static String format (date) as needed) {sdf. applyPattern ("yyyy-MM-dd HH: mm: ss"); return sdf. format (date );} /*** add days by date ** @ param Date * @ param hour * hour to be added * @ return */public static date addDays (date Date, int days) {Calendar c = Calendar. getInstance (); c. setTime (date); c. add (Calendar. DATE, days); date = c. getTime (); return date ;} /*** add hours by date ** @ param Date * @ param hour * hour to be added * @ return */public static date addHour (date Date, int hour) {Calendar c = Calendar. getInstance (); c. setTime (date); c. add (Calendar. HOUR, hour); date = c. getTime (); return date ;} /*** Add the given number of months on the given date ** @ param Date * @ param month * number of months to be added * @ return */public static date addMonth (date Date, int month) {Calendar c = Calendar. getInstance (); c. setTime (date); c. add (Calendar. MONTH, month); date = c. getTime (); return date;}/*** get the day before or after the specified date ** @ param specifiedDay * @ return */public static String getSpecifiedDayAfter (String specifiedDay, int dates) {Calendar c = Calendar. getInstance (); Date date = null; try {date = new SimpleDateFormat ("yy-MM-dd "). parse (specifiedDay);} catch (ParseException e) {e. printStackTrace ();} c. setTime (date); int day = c. get (Calendar. DATE); c. set (Calendar. DATE, day-dates); String dayAfter = new SimpleDateFormat ("yyyy-MM-dd "). format (c. getTime (); return dayAfter ;}


 

 

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.