Skip to content Skip to sidebar Skip to footer

Remaining Days To A Date Is Not Showing Correctly

Ok, so I've created a function to show the number of days until a date in the future... It is correct until the amount of days is over 9 days.. if over it seems to show a ran

Solution 1:

java.time

java.time, the modern Java date and time API, has methods for calculating the number of days between two dates. So don’t bother doing this calculation yourself. It’s error-prone.

DateTimeFormatterdateFormatter= DateTimeFormatter.ofPattern("dd-MM-uuuu H:mm");
    StringinputDateString="01-10-2018 23:59";
    LocalDatetoday= LocalDate.now(ZoneId.of("Pacific/Auckland"));
    LocalDateexpiration= LocalDate.parse(inputDateString, dateFormatter);
    if (expiration.isAfter(today)) {
        StringnumberOfDays="Days left: " + ChronoUnit.DAYS.between(today, expiration);
        System.out.println(numberOfDays);
    }

Running this snippet just now (already September 13 in New Zealand) I got this output:

Days left: 18

Please substitute your correct time zone if it didn’t happen to be Pacific/Auckland.

The answer by LaVepe has already explained nicely and correctly what went wrong in your code, so I am not repeating that.

The date and time classes you were using — Calendar and SimpleDateFormat — are long outdated and were always poorly designed. There is a way to get Calendar to count days, 1 day at a time, but it’s not well suited for that. SimpleDateFormat is notorious for the trouble it has caused for many programmers. I recommend you avoid those classes altogether and use java.time instead.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Solution 2:

Note that Calendar.DAY_OF_MONTH returns the day of the month between 1 and 31

so it will calculate difference between two days (number between 1 and 31) as if they were in the same month

I would suggest to rather use timestamps and then convert the result from millis to number of days like this:

long oneDay = 24 * 60 * 60 * 1000;  // in millisecondslong diff = day.getTime().getTime() - calCurr.getTime().getTime();
long numberOfDays = diff / oneDay;

then you can change it to String with Long.toString(numberOfDays)

Solution 3:

To get interval days between two days, you could do like this:

publiclongdaysTillExpire() {

    SimpleDateFormatsdf=newSimpleDateFormat("dd-MM-yyyy");
    StringdateInString="01-10-2018 23:59";
    Datedate=null;
    try {
        date = sdf.parse(dateInString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    CalendarexpiredCalendar= Calendar.getInstance();
    expiredCalendar.setTime(date);

    longmsDiff= expiredCalendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
    longdaysDiff= TimeUnit.MILLISECONDS.toDays(msDiff); 

    return daysDiff;
}

above function is tested successfully, modify it according to your requirment.

Post a Comment for "Remaining Days To A Date Is Not Showing Correctly"