7

I need to calculate number of days between two dates and I am using below code. problem is it is returning me 2 but actually it should return 3 because difference between 30 june 2016 to 27 june is 3. can you please help where it should include current date as well in 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;
}

expiry date is 2016-06-30 and current date is 2016-06-27

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AngryJS
  • 955
  • 5
  • 23
  • 47

4 Answers4

7

Reason is, you are not subtracting two dates with same time format.

Use Calendar class to change the time as 00:00:00 for both date and you will get 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 explaination in Jim Garrison' answer

Community
  • 1
  • 1
PyThon
  • 1,007
  • 9
  • 22
5

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);
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
3

The problem is that

Date createdDate = new Date();

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

Let's say you ran this at exactly 18:00 local time, you end 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 be using the newer java.time.* classes from Java 8. There is a class LocalDate that represents dates without time-of-day. It includes methods for parsing using a format, and LocalDate.now() to get the current date, as well as methods for calculating intervals between LocalDate instances.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
-1

Using the 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);
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
yash sachdeva
  • 637
  • 5
  • 13