Java date comparison and getting a day before and after

Source: Internet
Author: User

    /*** Compare two specified time, the result is 0 means the same,< 0 means the first time is earlier than the second time *@paramFirstDay *@paramSecondday *@return     */     Public Static intCompareTo (String firstday,string secondday) {Calendar C=calendar.getinstance (); Calendar C2=calendar.getinstance (); Date Date=NULL; Date Date2=NULL; Try{Date=NewSimpleDateFormat (Date_format_pattern). Parse (FirstDay); Date2=NewSimpleDateFormat (Date_format_pattern). Parse (Secondday); }          Catch(ParseException e) {e.printstacktrace ();          } c.settime (date);         C2.settime (DATE2); returnC.compareto (C2); } 
    /*** Get the first n days of the specified date * *@paramSpecifiedday YY-MM-DD *@return      * @throwsException*/       Public StaticString Getbeforeday (String specifiedday,intN) {Calendar C=calendar.getinstance (); Date Date=NULL; Try{Date=NewSimpleDateFormat (Date_format_pattern). Parse (Specifiedday); }          Catch(ParseException e) {e.printstacktrace ();          } c.settime (date); intDay =C.get (calendar.date); C.set (Calendar.date, Day-N); String Daybefore=NewSimpleDateFormat (Date_format_pattern). FORMAT (C.gettime ()); returnDaybefore; }     
 /*** Get the day after the specified date * *@paramSpecifiedday YY-MM-DD *@return      */       Public StaticString Getafterday (String specifiedday,intN) {Calendar C=calendar.getinstance (); Date Date=NULL; Try{Date=NewSimpleDateFormat (Date_format_pattern). Parse (Specifiedday); }          Catch(ParseException e) {e.printstacktrace ();          } c.settime (date); intDay =C.get (calendar.date); C.set (Calendar.date, Day+N); String Dayafter=NewSimpleDateFormat (Date_format_pattern). FORMAT (C.gettime ()); returnDayafter; }

Java date comparison and getting a day before and after

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.