Calculate the number of days between two dates in java

I need to calculate the number of days between two dates, and I use the code below. the problem is that it returns me 2, but in fact it should return 3, because the difference between June 30, 2016 and June 27 is 3. Could you help if it should include the current date, as well as the difference ?

public static long getNoOfDaysBtwnDates(String expiryDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date expDate = null; long diff = 0; long noOfDays = 0; try { expDate = formatter.parse(expiryDate); //logger.info("Expiry Date is " + expDate); // logger.info(formatter.format(expDate)); Date createdDate = new Date(); diff = expDate.getTime() - createdDate.getTime(); noOfDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); long a = TimeUnit.DAYS.toDays(noOfDays); // logger.info("No of Day after difference are - " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)); System.out.println(a); System.out.println(noOfDays); } catch (ParseException e) { e.printStackTrace(); } return noOfDays; } 

Validity is 2016-06-30, and the current date is 2016-06-27

+7
java date datetime
source share
4 answers

The reason is because you are not subtracting two dates with the same time format.

Use the Calendar class to change the time as 00:00:00 for both dates and you will get the exact difference in days.

 Date createdDate = new Date(); Calendar time = Calendar.getInstance(); time.set(Calendar.HOUR_OF_DAY, 0); time.set(Calendar.MINUTE, 0); time.set(Calendar.SECOND, 0); time.set(Calendar.MILLISECOND, 0); createdDate = time.getTime(); 

More explanation in Jim Harrison's comment.

+7
source share

Why not use LocalDate ?

 import java.time.LocalDate; import static java.time.temporal.ChronoUnit.DAYS; long diffInDays(LocalDate a, LocalDate b) { return DAYS.between(a, b); } 
+5
source share

The problem is

 Date createdDate = new Date(); 

sets createdDate at the current moment, that is, it includes the current time, as well as the date. When you parse a string using the specified format, the time is initialized to 00:00:00 .

Say you started it at exactly 18:00 local time, you ended up with

 createdDate = 2016-06-27 18:00:00.000 expDate = 2016-06-30 00:00:00.000 

The difference is 2 days 6 hours, not 3 days.

You should use the new java.time.* Classes from Java 8. There is a LocalDate class that represents dates without a time of day. It includes methods for parsing using the format and LocalDate.now() to get the current date, as well as methods for calculating the intervals between instances of LocalDate .

+3
source share

Using Calendar.get(Calendar.DAY_OF_MONTH) as pointed out by python:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date expDate = null; String expiryDate ="2016-06-30"; int diff = 0; try { expDate = formatter.parse(expiryDate); //logger.info("Expiry Date is " + expDate); // logger.info(formatter.format(expDate)); Calendar cal = Calendar.getInstance(); int today = cal.get(Calendar.DAY_OF_MONTH); cal.setTime(expDate); diff = cal.get(Calendar.DAY_OF_MONTH)- today; } catch (ParseException e) { e.printStackTrace(); } System.out.println(diff); 
0
source share

All Articles