1

I try to get the correct difference time between the current day and second selected day from the calendar. I'm using in this case LocalDate and the 3 methods getDays() getMonths() getYears() to get the day and the month also the year:

public int dateDiff(int year,int month,int day) {
    final int Day = c.get(Calendar.DAY_OF_MONTH);
    final int Month = c.get(Calendar.MONTH);
    final int Year = c.get(Calendar.YEAR);
    LocalDate localDate1 = LocalDate.of(year,month,day);
    LocalDate localDate2 = LocalDate.of(Year,Month,Day);

    Period period = Period.between(localDate2,localDate1);
    int dayDiff = period.getDays();

    return dayDiff;
}

public void onSelectedDayChange(@NonNull CalendarView view, final int year, final int month, final int dayOfMonth) {
    textView.setText(""+dateDiff(year, month, day));
}

But each time when I test the code I got in the textView "0" I try to see the value of the variable "period" and I got (P2M8D 'this result got in my example') that's mean the variable period count the difference between the days and the problem in the methods. How can I solve this problem?

kAvEh
  • 534
  • 4
  • 15
  • Turn on the debugger and inspect the values mate. – Vucko Apr 25 '20 at 17:53
  • 1
    I suggest you don't use the `Calendar` class, as it's obsolete. You already have access to the Java 8 Date and Time API (in the `java.time` package), so you should use those instead. – MC Emperor Apr 25 '20 at 17:55
  • I use `Calendar` because I need calendar in my project. The user tap the date he want and write an event – Mohamed Amine Sekmani Apr 25 '20 at 19:47
  • Can you replace current date logic with `LocalDate.now()` and try. – Aviral Verma Apr 26 '20 at 09:07
  • 1
    `period.getDays()` is probably not what you want, use `ChronoUnit.DAYS.between` see https://stackoverflow.com/questions/30833582/how-to-calculate-the-number-of-days-in-a-period. You still shouldn't get `0` though. There is some other problem in code you didn't show us. – Oleg Apr 26 '20 at 10:16
  • Thanks all for help, I'm solved the problem when I change the variables name, well in my project I named the current time by Day,Month,Years and the selected time by day,month,year so the capital letter in variables name made the project some problem. – Mohamed Amine Sekmani Apr 26 '20 at 15:43
  • You shouldn't start your variable names with a capital. Instead, follow the Java Naming Conventions: variabele names and method names should always be written in camelCase. – MC Emperor Apr 26 '20 at 17:45

2 Answers2

1

The problem in this project are in the name of variables, I'm using the same name of current time and the selected time I just change the first letter with capital one but this make problem. That's why every time when i run the project i got 0 I change the program like that:

public int dateDiff(int year,int month,int day) {
final int dayOfToday = c.get(Calendar.DAY_OF_MONTH);
final int monthOfToday = c.get(Calendar.MONTH);
final int yearOfToday = c.get(Calendar.YEAR);
LocalDate localDate1 = LocalDate.of(year,month,day);
LocalDate localDate2 = LocalDate.of(Year,Month,Day);

Period period = Period.between(localDate2,localDate1);
int dayDiff = period.getDays();

return dayDiff;
}
0

Here, this should help.

public int dateDiff(int year,int month,int day) {
  Calendar thatDay = Calendar.getInstance();
  thatDay.set(Calendar.DAY_OF_MONTH,day);
  thatDay.set(Calendar.MONTH,month); // 0-11 so 1 less
  thatDay.set(Calendar.YEAR, year);

  Calendar today = Calendar.getInstance();

  long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
  long days = diff / (24 * 60 * 60 * 1000);
  return days;
}
Amit Gupta
  • 633
  • 1
  • 8
  • 19