Java how to get date's "Yesterday" and "Tomorrow" Sample code _java

Source: Internet
Author: User

Objective

I believe that everyone in Java to use the most of the time class is java.util.Date , because the date class getYear() , getMonth() such as the acquisition of the year, month, day of the method is discarded, so the problem of this article to use the calendar to achieve, the following directly look at the sample code.

Use Calendar class: Calendar

@Test public
 void Datetest () {
 Date today = new Date ();
 for (int i=0;i<10;i++) {Today
 = yesterday (today);
 System.out.println (today);
 System.out.println ("------------");
 for (int i=0;i<10;i++) {Today
 = tomorrow (today);
 System.out.println (today);
 }

 /**
 * Return yesterday *
 @param
 today * @return
 /public
 date yesterday {
 Calendar Calendar = Calendar.getinstance ();
 Calendar.settime (today);
 Calendar.set (Calendar.date, Calendar.get (calendar.date)-1);
 return Calendar.gettime ();
 }

 /**
 * return tomorrow
 * @param today
 * @return
 /Public date tomorrow (date today) {
 Calendar Calendar = Calendar.getinstance ();
 Calendar.settime (today);
 Calendar.set (Calendar.date, Calendar.get (calendar.date) + 1);
 return Calendar.gettime ();
 }

Summarize

The above is about using Java to get Date "Yesterday" and "Tomorrow" all the content, I hope this article on the content of everyone's study or work can bring some help, if you have questions you can message exchange.

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.