Get the time of the previous day and the next day in Java

Posted by sergio-pro on Mon, 25 Nov 2019 19:00:44 +0100

               

Today, when developing a project, we encounter a problem that is how to get the day before and the day after the current time. The logic of this implementation is not complicated, and it is not difficult to write by yourself, but it seems that there is no need to write such a method by yourself. Think about the Calendar class in Java should have such a method, so there are two ways to check the relevant data online: they are 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(2014,01,01);  System.out.print("current time:");  printCalendar(cl);  //The day before  cl = setCalendar(2014,01,01);  getBeforeDay(cl);  System.out.print("The day before:");  printCalendar(cl);  //The next day  cl = setCalendar(2014,01,01);  getAfterDay(cl);  System.out.print("The next day:");  printCalendar(cl); }  /**  * Set 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 time of the day before the current time* @param cl  * @return  */ private static Calendar getBeforeDay(Calendar cl){  //Roll forward using the roll method  //cl.roll(Calendar.DATE, -1);  //Set directly using the set method  int day = cl.get(Calendar.DATE);  cl.set(Calendar.DATE, day-1);  return cl; }  /**  * Get the next day of the current time* @param cl  * @return  */ private static Calendar getAfterDay(Calendar cl){  //Roll back to the next day using the roll method  //cl.roll(Calendar.DATE, 1);  //Use set method to set time value directly  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 above code shows the use of set method and roll method. A Calendar is passed in and a Calendar is returned after setting. However, there is one difference between roll and set: in order to test the difference between the two methods, we set the time as a boundary value: 2014-01-01: see the running effect:

It is a rendering of the time set by the set method:


Through the rendering, we can see that there is no problem for the set method to get the time of the previous day and the next day. Next, let's take a look at the renderings of the roll method to get the time of the previous day and the next day:


We found that there was a problem in obtaining the time of the previous day, that is, the day before 2014-01-01 was 2014-01-31, so we can see that the roll method is not a cascading change when obtaining the previous day and the next day, it only modifies the current day, but does not consider year and month. Of course, when modifying the day, it is a cyclic change, such as January is 1-31, It's a cycle.

Through the above analysis, we can see the difference between set method and roll method.

           

Topics: Programming Java