1
LocalDate today=LocalDate.now();

And the event date is: eventDate=LocalDate.of(year, monthOfYear,dayOfMonth); (from the date picker dialog)

I'm trying to calculate the days difference between them... The shortest thing I have found is this:

int DaysDifference = Period.between(eventToDisplay.getEventDate(),today).getDays();

While the first object is "today", and the second one is "eventToDisplay.getEventDate()." It didn't work for me, it showed the wrong number of days.

I have also tried to do it like this:

eventToDisplay.getEventDate().compareTo(today)

Also didn't work... I have also tried to do it without joda-time, because I had troubles with it, because of what I'm trying to do with date and time...

The other things I have found are long and complicated, and I thought maybe there is a better way, without the joda-time.


EDIT:

I have just tried this:

  Calendar now = Calendar.getInstance();
            Calendar chosenDate=Calendar.getInstance();
            chosenDate.set(eventToDisplay.getEventDate().getYear(),eventToDisplay.getEventDate().getMonth().getValue(),eventToDisplay.getEventDate().getDayOfMonth());
            long def=  chosenDate.getTimeInMillis() - now.getTimeInMillis();
            long DaysDifference  =TimeUnit.MILLISECONDS.toDays(def);

Didn't work for me


EDIT: This has worked for me:

 LocalDate today=LocalDate.now();
            Calendar now = Calendar.getInstance();
            now.set(today.getYear(),today.getMonthValue(),today.getDayOfMonth());
            Calendar chosenDate=Calendar.getInstance();
            chosenDate.set(eventToDisplay.getEventDate().getYear(),eventToDisplay.getEventDate().getMonthValue(),eventToDisplay.getEventDate().getDayOfMonth());
            long def=  chosenDate.getTimeInMillis() - now.getTimeInMillis();
            long daysDifference =TimeUnit.MILLISECONDS.toDays(def);
  • what are your values – Amarjit Jul 23 '16 at 11:55
  • LocalDate today=LocalDate.now(), and the other one is a simple date from date picker dialog like: 3.3.2016 –  Jul 23 '16 at 11:58
  • http://stackoverflow.com/questions/23323792/android-days-between-two-dates – Amarjit Jul 23 '16 at 12:10
  • 1
    Ty to parse your current date in same format as other date – Amarjit Jul 23 '16 at 12:11
  • When you can use `LocalDate` from java.time, the modern Java date and time API, you should *not* mix in `Calendar`. That class is poorly designed and long outdated. Also your code using `Calendar` has a number of flaws, probably exactly because that class is so confusing and hard and counter-intuitive to use correctly. – Ole V.V. Apr 18 '21 at 09:45

2 Answers2

0

you can use something like this:

    Calendar now = Calendar.getInstance();

    Calendar end=Calendar.getInstance();
    end.set(<year>, <month>, <day>);

    long def=  end.getTimeInMillis() - now.getTimeInMillis();

    long days =TimeUnit.MILLISECONDS.toDays(def);
mehd azizi
  • 594
  • 1
  • 5
  • 16
0

java.time

Since you can use LocalDate from java.time, the modern Java date and time API, I warmly recommend that you stick to java.time. Calculating the difference is simple and straightforward when you know how:

    LocalDate today = LocalDate.now(ZoneId.systemDefault());
    LocalDate eventDate = LocalDate.of(2021, 5, 5);

    long differenceDays = ChronoUnit.DAYS.between(today, eventDate);
    
    System.out.println("Difference is " + differenceDays + " days.");

Output when I ran today (APril 18 in my tme zone):

Difference is 17 days.

If your date picker uses 0-based months (some date pickers insanely use 0 for January through 11 for December), remember to add 1 to the month number before passing it to LocalDate.

What went wrong in all your attempts?

    int DaysDifference = Period.between(eventToDisplay.getEventDate(),today).getDays();

The Period class represents a period of years, months and days. Since months have different lengths, a Period does not correspond to any exact number of days, so it’s not the right class to use here. You tried to use its getDays method, which gives you the days component of the period, not the months or the years. So if the two days are less than a month apart, you will get the correct result, otherwise not. If for example the two dates are 1 month 3 days apart, you will only get the 3 days.

The Calendar class used in more than one of your attempts is poorly designed and long outdated. Counting days correctly with it would be truly cumbersome, so no wonder that your attempts gave the wrong results.

Both of your attempts are wrong for at least two reasons:

  1. A Calendar has a date and a time of day. So by finding the difference in milliseconds and dividing by the number of milliseconds that you think are in a day, you will get different results depending on the time of day that happens to be in each of your Calendar objects. Your code calls Calendar.getInstance() twice. In an extreme situation your code may run across midnight so the time in the first Calendar will be close to 23:59:59 and in the second close to 00:00, which will almost certainly give you an error of 1 day.
  2. A day is not always 24 hours. Summer time (DST) is the most frequent but not the only reason why a date is sometimes 23 hours, 25 hours or some other length. If for example you try to count days across the spring forward where a day is only 23 hours or 23 hours 30 minutes, your code will count 1 day too few.

Furthermore this line from the snippet that you say that works is definitely wrong:

    now.set(today.getYear(),today.getMonthValue(),today.getDayOfMonth());

You are using the 1-based month number from LocalDate, for example 4 for April, as a 0-based month number in Calendar, for example 4 would mean May. So your Calendar is off by 1 month. Since I haven’t got your complete code, is may in some cases balance out by another error that causes the other Calendar to be 1 month off too, I cannot know. Since months have different lengths, you will still get an error of up to 3 days sometimes.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161