I am fairly new to (Java) programming and I am trying to build a little Android app that will tell you if it's already weekend or not.
I already got it so far that it can tell you if it's weekend or not, however I also want to show a little countdowntimer in that app until the weekend starts, or ends.
I am unable to think about an Algorithm to calculate the time (in milliseconds) between the current time and the next Saturday (if it's not in a weekend) or Monday if it's a weekend.
I know how to get the current day in the week using the Calendar class, however I get stuck when I try to get the time until the next Saturday or Monday. I can't think about a way to do this.
This might sound like a stupid question but I have really lost my mind in this. Hopefully one of you guys can help me out.
Here's the process, but as a description rather than code, leaving the rest for you as an implementation exercise:
Calendar
objectset
calls to get to the right time of day (so set the hour to 0, the minute to 0, the second to 0, the millisecond to 0) - assuming that you're looking for midnight, of coursegetTimeInMillis()
to get the point in time you're aiming forOne thing to bear in mind is that daylight saving time changes can make things "interesting"... so at the very start of Saturday, there might be 47 or 49 hours before you get to the start of Monday, instead of the normal 48. I'd expect the above algorithm to cope in most cases, but if you "aim" for Sunday you could have an odd situation in Brazil (and a few other places) when you try to get to midnight, as that might be skipped entirely... Time zones are annoying like that.
Additionally, if you can use Joda Time it could make your life simpler.
See more on this question at Stackoverflow