I wanted to calculate date between two dates but some solutions on stack overflow doesn't work with me in some case f.e : when end date start after some days than the start day
            Asked
            
        
        
            Active
            
        
            Viewed 84 times
        
    -5
            
            
        - 
                    Please re-read carefully https://stackoverflow.com/help/how-to-ask your question is not specific enough without your own code context and citations when you mention your lack of success with other stackoverflow posts. – Léa Gris Aug 13 '19 at 11:00
1 Answers
-2
            
            
        so with this code i resolved all the issues and it is working fine now and forever :)
private String getDifferenceBtwDates (String dateDebut, String dateFin){
    if (StringUtility.isEmptyOrNull(dateDebut) || StringUtility.isEmptyOrNull(dateFin)){
        return "0" ;
    }else {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date startDate = null;
        Date finDate = null;
        try {
            startDate = sdf.parse(dateDebut);
            finDate = sdf.parse(dateFin);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(startDate);
        calendar2.setTime(finDate);
        /*
         * Use getTimeInMillis() method to get the Calendar's time value in
         * milliseconds. This method returns the current time as UTC
         * milliseconds from the epoch
         */
        long miliSecondForDate1 = calendar1.getTimeInMillis();
        long miliSecondForDate2 = calendar2.getTimeInMillis();
        // Calculate the difference in millisecond between two dates
        long diffInMilis = miliSecondForDate2 - miliSecondForDate1;
        /*
         * Now we have difference between two date in form of millsecond we can
         * easily convert it Minute / Hour / Days by dividing the difference
         * with appropriate value. 1 Second : 1000 milisecond 1 Hour : 60 * 1000
         * millisecond 1 Day : 24 * 60 * 1000 milisecond
         */
        long diffInDays = diffInMilis / (24 * 60 * 60 * 1000);
        return "" + diffInDays ;
    }
}
 
    
    
        Elyakoubi Ahmed
        
- 1
- 1
- 
                    And honestly: this is a pretty terrible and overly specific way of solving the underlying problem. – GhostCat Aug 13 '19 at 11:00
- 
                    @Zun There is nothing wrong about A) figuring ... "no such question exists" to then B) ask ... and provide a self answer. The **real** problem here is the insufficient quality of the question itself. And yes, the low quality of the answer doesn't help changing the mind of this reader. – GhostCat Aug 13 '19 at 11:01
- 
                    This is not a robust solution. The number of days between 2 dates does not equal the number of milliseconds between those dates divided by 24 * 60 * 60 * 1000. This ignores leap seconds and a whole host of other abnormalities. – cameron1024 Aug 13 '19 at 11:04
- 
                    https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result This question, answered by the one and only Jon Skeet, might illustrate the issues here. – cameron1024 Aug 13 '19 at 11:06
