SoFunction
Updated on 2025-04-04

Android programming implements a method to calculate the number of days between two dates and print all dates

This article describes the Android programming method to calculate the number of days between two dates and print all dates. Share it for your reference, as follows:

The following code calculates the number of days between two dates and prints all dates

Note: At the beginning, when increasing the number of days, the milliseconds of a day are directly used 24*60*60*1000 to gradually increase the number of days. When testing again, it was found that when the number of days between two dates exceeds 24 days, the printed date is actually before the start date (for example, when printing 2016/12/18-2017/1/23, the printed date is actually 2016/12/1). Later, it was found that the reason was that 24*60*60*1000 is an int value, and the value range of the int value is 2 to the power of 31: +/- 2147483648. When the maximum number exceeds, it will become the minimum number, which will cause the date to become smaller. Just change 24*60*60*1000 to a long type value: private long static final long ONE_DAY_MS=24*60*60*1000

/**
 * Calculate the date between two dates
 * @param startTime
 * @param endTime
 */
private void betweenDays(long startTime,long endTime,long mills_select,int code){
   Date date_start=new Date(startTime);
   Date date_end=new Date(endTime);
   // Calculate the date from the start time when the end time is 0   Calendar fromCalendar = ();
   (date_start);
   (Calendar.HOUR_OF_DAY, 0);
   (, 0);
   (, 0);
   (, 0);
   Calendar toCalendar = ();
   (date_end);
   (Calendar.HOUR_OF_DAY, 0);
   (, 0);
   (, 0);
   (, 0);
   int s = (int) ((() - ())/ (ONE_DAY_MS));
   if(s>0){
     for(int i = 0;i<=s;i++){
       long todayDate = () + i * ONE_DAY_MS;
       /**
        * yyyy-MM-dd E :2012-09-01
        */
       ("Print date",(todayDate,"yyyy-MM-dd"));
     }
   }else {//This is within the same day       ("Print date",(startTime,"yyyy-MM-dd"));
   }
}

()The method code is as follows:

/**
 * Format the time passed in
 *
 * @param time The time required for formatting
 * @param formatStr format
 * @return
 */
public static String getCustonFormatTime(long time, String formatStr) {
    SimpleDateFormat format = new SimpleDateFormat(formatStr);
    Date d1 = new Date(time);
    return (d1);
}

PS: Here are a few online tools for your reference:

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online Perpetual Calendar:
http://tools./bianmin/wannianli

Online Lunar/Gregorian calendar conversion tool:
http://tools./bianmin/yinli2yangli

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about Android related content, please check out the topic of this site:Android date and time operation skills summary》、《Android development introduction and advanced tutorial》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.