Remaining Days To A Date Is Not Showing Correctly
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.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu H:mm");
String inputDateString = "01-10-2018 23:59";
LocalDate today = LocalDate.now(ZoneId.of("Pacific/Auckland"));
LocalDate expiration = LocalDate.parse(inputDateString, dateFormatter);
if (expiration.isAfter(today)) {
String numberOfDays = "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.bpwith subpackages.
Links
- Answer by LaVepe
- Oracle tutorial: Date Time explaining how to use
java.time. - Java Specification Request (JSR) 310, where
java.timewas first described. - ThreeTen Backport project, the backport of
java.timeto Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
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 milliseconds
long 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:
public long daysTillExpire() {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "01-10-2018 23:59";
Date date = null;
try {
date = sdf.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar expiredCalendar = Calendar.getInstance();
expiredCalendar.setTime(date);
long msDiff = expiredCalendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
long daysDiff = 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"