JAVA computes two instances whose dates differ

  • 2020-04-01 02:27:59
  • OfStack

Data that is more than a few days apart is used when Java is developing logistics or other features, so I've sorted it out.
Method of invocation:


long date1 = getDateTime("20121201");//Can be changed to your own date type, but in the format of "20121212"
long date2 = getDateTime("20121212");   
int day = dateInterval(date1, date2);   
System.out.println(day);
 

Method invocation:

  
public static int dateInterval(long date1, long date2) { 
    if(date2 > date1){ 
        date2 = date2 + date1; 
        date1 = date2 - date1; 
        date2 = date2 - date1; 
    } 

     //The Canlendar class is an abstract class & NBSP;  
     //Provides a rich calendar field & PI;
     //& NBSP; is used in this program;
     //Calendar. YEAR      Year & NBSP; of the date;
     //Calendar. DAY_OF_YEAR         Days of the current year & NBSP;  
     //GetActualMaximum (calendar.day_of_year) returns 365 days or 366 days & mailed this year;
    Calendar calendar1 = Calendar.getInstance(); //Get a calendar & NBSP;  
    calendar1.setTimeInMillis(date1); //Set the current and previous time value of this Calendar with the given long value.    

    Calendar calendar2 = Calendar.getInstance(); 
    calendar2.setTimeInMillis(date2); 
    //Judge the year first & cake;  
    int y1 = calendar1.get(Calendar.YEAR); 
    int y2 = calendar2.get(Calendar.YEAR); 

    int d1 = calendar1.get(Calendar.DAY_OF_YEAR); 
    int d2 = calendar2.get(Calendar.DAY_OF_YEAR); 
    int maxDays = 0; 
    int day = 0; 
    if(y1 - y2 > 0){ 
        day = numerical(maxDays, d1, d2, y1, y2, calendar2); 
    }else{ 
        day = d1 - d2; 
    } 
    return day; 
}   

  
public static int numerical(int maxDays, int d1, int d2, int y1, int y2, Calendar calendar){ 
    int day = d1 - d2; 
    int betweenYears = y1 - y2; 
    List<Integer> d366 = new ArrayList<Integer>(); 

    if(calendar.getActualMaximum(Calendar.DAY_OF_YEAR) == 366){ 
        System.out.println(calendar.getActualMaximum (Calendar.DAY_OF_YEAR)); 
        day += 1; 
    } 

    for (int i = 0; i < betweenYears; i++) { 
        //Year + 1 sets the number of days in the next year & NBSP;  
        calendar.set(Calendar.YEAR, (calendar.get (Calendar.YEAR)) + 1); 
        maxDays = calendar.getActualMaximum (Calendar.DAY_OF_YEAR); 
        //The first 366 days do not include all 366 records without + 1, no addition and then minus an & PI;  
        if(maxDays != 366){ 
            day += maxDays; 
        }else{ 
            d366.add(maxDays); 
        } 
        //If the last maxDays equals 366 day - 1& NBSP;  
        if(i == betweenYears-1 && betweenYears > 1 && maxDays == 366){ 
            day -= 1; 
        } 
    } 

    for(int i = 0; i < d366.size(); i++){ 
        //One or more 366 days & NBSP;  
        if(d366.size() >= 1){ 
            day += d366.get(i); 
        }  
    }   
    return day; 
} 

  
public static long getDateTime(String strDate) { 
    return getDateByFormat(strDate, "yyyyMMdd").getTime(); 
} 

  
public static Date getDateByFormat(String strDate, String format) { 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    try{ 
        return (sdf.parse(strDate)); 
    }catch (Exception e){ 
        return null; 
    } 
}
 

Case 2

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test16 {
 
 public static void main(String[] args) throws ParseException {
  // TODO Auto-generated method stub
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date d1=sdf.parse("2012-09-08 10:10:10");
  Date d2=sdf.parse("2012-09-15 00:00:00");
  System.out.println(daysBetween(d1,d2));
  System.out.println(daysBetween("2012-09-08 10:10:10","2012-09 -15 00:00:00"));
 }

   
    public static int daysBetween(Date smdate,Date bdate) throws ParseException  
    {  
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM- dd");
     smdate=sdf.parse(sdf.format(smdate));
     bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();  
        cal.setTime(smdate);  
        long time1 = cal.getTimeInMillis ();               
        cal.setTime(bdate);  
        long time2 = cal.getTimeInMillis ();       
        long between_days=(time2-time1)/ (1000*3600*24);

       return Integer.parseInt(String.valueOf (between_days));         
    }  


    public static int daysBetween(String smdate,String bdate) throws ParseException{
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM- dd");
        Calendar cal = Calendar.getInstance();  
        cal.setTime(sdf.parse(smdate));  
        long time1 = cal.getTimeInMillis ();               
        cal.setTime(sdf.parse(bdate));  
        long time2 = cal.getTimeInMillis ();       
        long between_days=(time2-time1)/ (1000*3600*24);

       return Integer.parseInt(String.valueOf (between_days));   
    }
}
 

Example 3
 
//Get the remaining days
   SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");
   Date d0=new java.util.Date();
   Date d1=df.parse(end_date);
   long time0=d0.getTime();
   long time1=d1.getTime();
   System.out.println((time1-time0)/(1000*60*60*24));

This is a good way to calculate the number of days between the two times


    public static int diffdates(Date date1, Date date2) {
        int result = 0;
        ElapsedTime et = new ElapsedTime();
        GregorianCalendar gc1 = new GregorianCalendar ();
        GregorianCalendar gc2 = new GregorianCalendar ();
        gc1.setTime(date1);
        gc2.setTime(date2);
        result = et.getDays(gc1, gc2);
        return result;
    }  

Then the method in ElapseTime is:

public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
  int elapsed = 0;
  GregorianCalendar gc1, gc2;
  if (g2.after(g1)) {
   gc2 = (GregorianCalendar) g2.clone();
   gc1 = (GregorianCalendar) g1.clone();
  } else {
   gc2 = (GregorianCalendar) g1.clone();
   gc1 = (GregorianCalendar) g2.clone();
  }
  gc1.clear(Calendar.MILLISECOND);
  gc1.clear(Calendar.SECOND);
  gc1.clear(Calendar.MINUTE);
  gc1.clear(Calendar.HOUR_OF_DAY);
  gc2.clear(Calendar.MILLISECOND);
  gc2.clear(Calendar.SECOND);
  gc2.clear(Calendar.MINUTE);
  gc2.clear(Calendar.HOUR_OF_DAY);
  while (gc1.before(gc2)) {
   gc1.add(Calendar.DATE, 1);
   elapsed++;
  }
  return elapsed;
}
 

It's actually easiest to use joda

public boolean isRentalOverdue(DateTime datetimeRented) {
  Period rentalPeriod = Period.days(2);
  return datetimeRented.plus(rentalPeriod).isBeforeNow()
}
 


Related articles: