Today, I encountered a problem in the development project: how to obtain the previous day and the next day of the current time. The implementation logic is not complicated, and it is not difficult to write your own words, however, it seems that there is no need to write such a method on your own. Think about the Calendar class in Java, there should be such a method, so I checked the relevant information online, there are two methods: the set and roll methods are used as follows:
Package com. java. demo; import java. util. calendar; public class DateUtil {public static void main (String [] args) {// current time Calendar cl = setCalendar (, 01); System. out. print ("current time:"); printCalendar (cl); // cl = setCalendar (, 01); getBeforeDay (cl); System. out. print ("previous day:"); printCalendar (cl); // cl = setCalendar (, 01); getAfterDay (cl); System. out. print ("next day:"); printCalendar (cl );} /*** set the time * @ param year * @ param month * @ param date * @ return */public static Calendar setCalendar (int year, int month, int date) {Calendar cl = Calendar. getInstance (); cl. set (year, month-1, date); return cl ;} /*** get the previous day Time of the current time * @ param cl * @ return */private static Calendar getBeforeDay (Calendar cl) {// roll forward using the roll method // cl. roll (Calendar. DATE,-1); // use the set method to directly set int day = cl. get (Calendar. DATE); cl. set (Calendar. DATE, day-1); return cl;}/*** get the last day of the current time * @ param cl * @ return */private static Calendar getAfterDay (Calendar cl) {// use the roll method to roll back to the last day. // cl. roll (Calendar. DATE, 1); // use the set method to directly set the time value int day = cl. get (Calendar. DATE); cl. set (Calendar. DATE, day + 1); return cl;}/*** print time * @ param cl */public static void printCalendar (Calendar cl) {int year = cl. get (Calendar. YEAR); int month = cl. get (Calendar. MONTH) + 1; int day = cl. get (Calendar. DATE); System. out. println (year + "-" + month + "-" + day );}}
The code above illustrates the use of the set Method and the roll method. Input a Calendar and return a Calendar after setting. However, there is a difference between roll and set: to test the difference between the two methods, we set the time to a boundary value: to see the running effect:
Is set by the set method:
We can see that the set method has no problem in obtaining the time of the previous day and the next day. Next let's take a look at the roll method to get the time of the previous day and the next day: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140208/20140208132932222.png" alt = "">
We found that the problem occurred when obtaining the time of the previous day, that is, the previous day of February 1, was. We can see that, the roll method does not cascade changes when obtaining the previous day and the next day. It only modifies the current day without considering year and month, of course, when he modifies the day, it is a cyclical change. For example, in February: 1-31, it is a loop.
Through the above analysis, we can see that the set and roll methods are different.