I have two calendar dates where i am getting the difference between in days, hours, and minutes.
This works perfectly if the end date is greater than the start date. What doesnt work is if the start date is the same day of week as the end date, but an earlier time than the end date. For example: end date 2:20 pm Saturday, and start date is 7:20 pm on saturday. It calculates it at like 0days, and 5 hours. But, it should be more like 7 days.
Here is the code
long t1 = curCal.getTimeInMillis();
long t2 = setCal.getTimeInMillis();
if(t2 < t1){
days = t1-t2;
}else{
days = t2-t1;
}
long toDays = TimeUnit.MILLISECONDS.toDays(days);
long toHours = TimeUnit.MILLISECONDS.toHours(days) % 24;
long toMinutes = TimeUnit.MILLISECONDS.toMinutes(days) % 60;
String toastMessage = String.format(" %d Days %d Hours %d Minutes", toDays, toHours, toMinutes);
Toast.makeText(context, "ALARM in" + " " + toastMessage , Toast.LENGTH_LONG).show();
How can i handle the case where the end date is the same day as the start date, but the end date is a time before the start date?
Thanks
EDIT
I think i solved my problem. I am adding it for anyone else having the same issue. if end date = startdate(same day) add 7 to the calendar object for enddate. psuedocode
if (enddate == startdate)) {
enddate.add(Calendar.DAY_OF_YEAR, 7);
}
Running just now (Sunday 22:03:17 in Büsingen) I got:
I believe that I am contributing the answer that is not only the modern one but also the more robust one.
Calendar
class is long outdated and by today’s standards poorly designed. Instead I use and recommendjava.time
, the modern Java date and time API.ZonedDateTime
objects minimizes surprises here. It does require you to fill in your desired time zone where I put Europe/Busingen since summer time transitions are time zone specific.Calendar
, a date and time, for a weekly recurring alarm seems a bit funny. What you need is a day-of-week and a time of day, so I use that.java.time
offers the classes needed, theDayOfWeek
enum and theLocalTime
class.I am in fact so modern that I am using the
toXxxPart
methods of theDuration
class that were introduced in Java 9. For formatting theDuration
if you are not yet using Java 9 you will need to subtract first the days from the duration to get the hours: use theminusDays
method. Then do similarly withminusHours
to get the minutes.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.org.threeten.bp
with subpackages.Links
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Reading your question another way, if
t1
is the start date andt2
is the end date, your logic does not include the case wheret1 < t2
andt2 - t1
< 1. In this case, you need to add 7 to the number of days. Something like:All of this can be simplified to