Java date operations helper class

Source: Internet
Author: User

Formatdate. Java

 

Package COM. ucit. CA. webapp. tool; import Java. util. *; import Java. text. *; public class formatdate {public formatdate () {}// format the date as the string "yyyy-mm-dd hh: mm" Public String formatdatetime (date basicdate, string strformat) {simpledateformat df = new simpledateformat (strformat); Return DF. format (basicdate);} // format the date as the string "yyyy-mm-dd hh: mm" Public String formatdatetime (string basicdate, string strformat) {simpledateformat df = new simpledateformat (strformat); Date tmpdate = NULL; try {tmpdate = DF. parse (basicdate);} catch (exception e) {// Date string format error} return DF. format (tmpdate);} // date after the current date plus or minus n days, returns string (yyyy-mm-dd) Public String ndaysaftertoday (int n) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Calendar rightnow = calendar. getinstance (); // rightnow. add (calendar. day_of_month,-1); rightnow. add (calendar. day_of_month, + n); Return DF. format (rightnow. gettime ();} // The date after the current date plus or minus n days, returns string (yyyy-mm-dd) Public date ndaysafternowdate (int n) {calendar rightnow = calendar. getinstance (); // rightnow. add (calendar. day_of_month,-1); rightnow. add (calendar. day_of_month, + n); Return rightnow. gettime () ;}// if a date string is specified, the Public String ndaysafteronedatestring (string basicdate, int N) of the date string plus or minus n days is returned) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Date tmpdate = NULL; try {tmpdate = DF. parse (basicdate);} catch (exception e) {// Date string format error} Long nday = (tmpdate. gettime ()/(24*60*60*1000) + 1 + n) * (24*60*60*1000); tmpdate. settime (nday); Return DF. format (tmpdate);} // for a given date, return the public date ndaysafteronedate (date basicdate, int N) {long nday = (basicdate. gettime ()/(24*60*60*1000) + 1 + n) * (24*60*60*1000); basicdate. settime (nday); Return basicdate;} // calculate the number of days between two dates. Public int ndaysbetweentwodate (date firstdate, date seconddate) {int nday = (INT) (seconddate. gettime ()-firstdate. gettime ()/(24*60*60*1000); Return nday;} // calculate the number of days between two dates. Public int ndaysbetweentwodate (string firststring, string secondstring) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Date firstdate = NULL; Date seconddate = NULL; try {firstdate = DF. parse (firststring); seconddate = DF. parse (secondstring);} catch (exception e) {// Date string format error} int nday = (INT) (seconddate. gettime ()-firstdate. gettime ()/(24*60*60*1000); Return nday ;}}

 

Source code:

Http://cid-6adab3222e64b22c.skydrive.live.com/self.aspx/.Public/%e4%bb%a3%e7%a0%81/javaToolClass/FormatDate.java

 

 

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.