wood burning stoves
The moose likes Java Micro Edition and the fly likes getting time till yesterday midnight in J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "getting time till yesterday midnight in J2ME" Watch "getting time till yesterday midnight in J2ME" New topic

getting time till yesterday midnight in J2ME

Dushyant Chhetri
Ranch Hand

Joined: Oct 28, 2009
Posts: 75
Hi Fellow Ranchers,

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.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

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.

An IDE is no substitute for an Intelligent Developer.
Dushyant Chhetri
Ranch Hand

Joined: Oct 28, 2009
Posts: 75
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 agree. Here's the link: http://aspose.com/file-tools
subject: getting time till yesterday midnight in J2ME
It's not a secret anymore!