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.