File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes What class to use for date-related calculations. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "What class to use for date-related calculations." Watch "What class to use for date-related calculations." New topic
Author

What class to use for date-related calculations.

Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
I need to disregard records more than 48 hours removed. So what would you advise I use: Date or Calendar?


Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
mike acre
Ranch Hand

Joined: Sep 23, 2003
Posts: 197
Date is lighter & faster (by a huge amount) and simpler for Mr JP and there is no need for Calendar.

You can get a Date from the database format using SimpleDateFormat, then you can subtract 48hrs from it and compare it with the current date.


SCJP 1.4, SCJD
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by mike acre:
Date is lighter & faster (by a huge amount) and simpler for Mr JP and there is no need for Calendar.

You can get a Date from the database format using SimpleDateFormat, then you can subtract 48hrs from it and compare it with the current date.


Beware of the use of Date and attempts to determine today's date from it. The value is stored in UTC (GMT) and as such if you aren't in England or its summer, your value for midnight isn't the same as Date's. The easiest way around this is to use the SimpleDateFormat to convert midnight this morning to a Date and then use that in calculations.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Here's the method... I bet there is an easier way...


[ September 07, 2004: Message edited by: Anton Golovin ]
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Anton Golovin:
Here's the method... I bet there is an easier way...

Here's what I do, I only allow today, tomorrow and the day after.

It calsulates minDate as midnight this morning, then calculates the max date as 2 days later. Since all values are at midnight, time == 0, this will work. Here's the Functions methods:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What class to use for date-related calculations.
 
Similar Threads
How to instabntiate the class through EJB QL
conversion from Date ojbect to Date object
Date method
Short/Long day?
Calendar class and getting the correct date