-1

I want to use the difference method. It gets a certain date parameter, then calculates the difference between two dates (this and other)

The calculateDate is the way to get the days passed since the Christian counting. I wanted to use it inside the difference method, but I get the following error while trying to compile:

cannot find symbol - variable calculateDate

The difference has to be an absolute value, so I added the Math.abs.

public int difference (Date other) {
            return Math.abs(this.calculateDate-other.calculateDate);
}

//computes the day number since the beginning of the Christian counting of years
private int calculateDate (int day, int month, int year)
{
    if (month < 3)
    {
        year--;
        month = month + 12;
    }
    return 365 * year + year/4 - year/100 + year/400 + ((month+1) * 306)/10 + (day - 62);

}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Idosud
  • 9
  • 1
  • `calculateDate` is not a variable. Its a method that is taking three parameters – Md Johirul Islam Apr 21 '18 at 22:10
  • If you want call a method, then you need to supply arguments. – Oliver Charlesworth Apr 21 '18 at 22:10
  • from the looks of it, `calculateDate(...)` is a method, expecting three `int`s, but you use it as if it were a attribute: `this.calculateDate` – Turing85 Apr 21 '18 at 22:11
  • so what should I do in order to be able to proceed? – Idosud Apr 21 '18 at 22:15
  • @Idosud: Do what you've been told: Supply appropriate arguments to your calls of `calculateDate`. That is the day, the month and the year of `this` and `other`, respectively. Or consider rewriting `calculateDate` to return the number of days for the current instance, so you can just call it with no arguments (but you'll still need to write a pair of parenthesis, but without anything between them). – sticky bit Apr 21 '18 at 22:29
  • Possible duplicate of [how to compare two dates in java?](https://stackoverflow.com/questions/8367936/how-to-compare-two-dates-in-java) – KYHSGeekCode Apr 22 '18 at 00:42

1 Answers1

0

It would be easier to use java.time library instead of writing the day counting code by hand, unless you have a very specific requirement:

private int difference(LocalDate date) {
  LocalDate start = LocalDate.of(0, 1, 1); // 1 Jan 0000 
  return ChronoUnit.DAYS.between(start, date); 
}

You can map from java.util.Date to java.time.LocalDate with:

Date date = ...
LocalDate ld = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111