This week's book giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line! See this thread for details.
I was trying my hand at filtering time and setting alarms for certain notifications in my application.
Now I am stuck at trying to get the time in milliseconds(the usual representation) into a long variable uptill the previous midnight.
This is because I need to compare the alarm time with the present time and if the alarm time has crossed the present time then it should be simply overlooked and application should proceed forward through the list of several other alarms for the same day.
i need the time till the previous midnight because My alarm times are repeated and stored in long format for some hours of the day.
like if my alarm is for 4AM , then it will be represented by long in millisecond for 0 to 4 hrs ie. 4X 60 X60X1000 ms
So the long uptill midnight on adding to this will give me the means to compare withthe system time.
I can give you some answers based on the standard JRE. Hopefully they'll still work on the JME VM.
java.util.Data has a pair of methods named "before" and "after" that can be used for date comparisons. As far as actual "midnight" computations go, that sort of stuff is generally provided by the Calendar classes in order to allow for locale issues. Although you can probably do it "brute force" by doing a div/mod trunctation down to the last multiple of 1000*3600*24, which is the number of milliseconds in a day, allowing for the clock having started at midnight, Jan 1, 1970 (Ithink that's Java's epoch, but I may be off).
If you want to run a timer, it's best to calculate the amount of time until the desired event, and sleep for that interval. If you continuously poll, you'll load down the CPU, and on a mobile device, that means also draining the battery charge. In the event of multiple timers, the easiest way to handle that is to compute the differentials between events and sleep for the differential periods. Be sure and query the clock when starting a new interval, however, since otherwise you could leak time. Also, there could have been a delay while higher-priority functions were handled before the sleeping thread got awakened, so you want to correct for that.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
posted 11 years ago
Thank You Tim,
I have done exactly the same work
Applied brute computation by getting the remainder using the mod operator and deducted as to get the time till the last midnight.
I also have taken care of not polling the CPU continuously by implementing it with a push registry in a fresh thread.
Now I am looking into the finer aspects as you had mentioned about the delay in time or time lekage.
Once again thank you,
Will bounce back with more queries
I knew I would regret that burrito. But this tiny ad has never caused regrets: