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.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Frits Walraven
  • Piet Souris

getting time till yesterday midnight in J2ME

Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Saloon Keeper
Posts: 22998
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Dushyant Chhetri
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic