Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

dates are fun!

Thread view: 
ba.hons - 06 Nov 2006 15:48 GMT
hello,

wondering if someone could help i need to do a kind of date diff
operation so i can calculate how long an object should last for.

My problem is that as am using this kind of code:

Date Edate = sdf.parse(ExpiryDate);
                diff = Edate.getTime()-currentDate.getTime();
                diff=diff/1000;

and i want my object to expire exactly at midnight, not at when the
currentime is taken.

Any ideas?
Oliver Wong - 06 Nov 2006 17:22 GMT
> hello,
>
[quoted text clipped - 11 lines]
>
> Any ideas?

   You could round diff to the nearest multiple of 86400 (the number of
seconds in a day). This will cause "expiration points" to occur every 24
hours, but whether or not that happens to be midnight locally depends on
what time zone you're in.

   - Oliver
Mark Rafn - 06 Nov 2006 19:50 GMT
>wondering if someone could help i need to do a kind of date diff
>operation so i can calculate how long an object should last for.

Ok.  Though it's kind of a strange way to do things.  This is for some sort of
cache, I presume.  Normally, you want to keep caches for a fixed duration (10
minutes from last update), or in some sort of least-recently-used system based
on total cache size.

If you want to clear it all at midnight, it's probably easier to just drop the
whole cache at midnight rather than having each object calculate when midnight
is going to come.

>My problem is that as am using this kind of code:
>Date Edate = sdf.parse(ExpiryDate);
>                diff = Edate.getTime()-currentDate.getTime();
>                diff=diff/1000;
>and i want my object to expire exactly at midnight, not at when the
>currentime is taken.

Huh?  Do you want to expire at a given expiryDate, or at midnight?  You
haven't described enough of the problem for me to guess what data you have,
and what data you want.

If you're looking for "milliseconds until midnight in the VM default
timezone", try something on the order of:

 // CAVEAT: untested code, expect typos and dumb mistakes.
 Calendar midnight = Calendar.getInstance();

 // roll back a day if it's midnight exactly already, so next midnight is now.
 midnight.add(Calendar.MILLISECOND, -1);

 // set small fields to 0
 midnight.set(Calendar.MILLISECOND, 0);
 midnight.set(Calendar.SECOND, 0);
 midnight.set(Calendar.MINUTE, 0);
 midnight.set(Calendar.HOUR_OF_DAY, 0);

 // add a day.
 midnight.add(Calendar.DAY_OF_MONTH, 1);

 // millis from now until midnight:
 long millisUntilMidnight =
     midnight.getTimeInMillis() - System.currentTimeMillis();

Adjust as needed to deal with your own definitions, timezone requirements,
or with parsing dates from other sources.   Say what you will about the
overcomplexification of the Calendar object, it handles things like this
reasonably well, and if you try to do it yourself, you're going to get it
wrong most of the time, when you have to track daylight/summer time, leap
years, etc.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.