Preface
I believe that the most time class you use in java is java.util.Date . Since the methods of getting year, month, and day such as getYear() getMonth() are abandoned in the Date class, the problems in this article must be implemented with the help of Calendar. Let’s take a look at the sample code directly.
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(Date today) { Calendar calendar = Calendar.getInstance(); calendar.setTime(today); calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1); return calendar.getTime(); } /** * Return to 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 all about using Java to obtain Date "Yesterday" and "Tomorrow". I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.