Yet Another Date Object
The Adept library also includes a Date class that provides easy-to-use facilities where a Calendar object is unwieldy and overkill. Like java.util.date it keeps the date and time in Epoch time. This is a fancy name for the number of milliseconds since the first of January 1970. And, no - there will be no Y2K-like issues in Java since it is a long, being 64 bits long. It was only the old C programs where a long is 32 bits that the time will roll over in about 25 years.
Like the String class, the Date class is immutable - with all the additional benefits an immutable class carries with it. If you're scratching your head here, immutable simply means that it cannot be changed. To quote Brian Goetz of IBM, "Immutable objects have a number of properties that make working with them easier, including relaxed synchronization requirements and the freedom to share and cache object references without concern for data corruption". Get the whole story here.
A Date object can be created with the current time, an Epoch time, a Calendar or an internal Date mathematics function.
Once you have a date, the class allows you to:
- Change it to other forms: such as seconds() for Epoch seconds - being a date, accurate to the second, that fits into an int (32 bits); timeOfDay() which then returns a Time object for more granular work; calendar() for dealing with the rest of the Java world; or timestamp or dateStamp() for the date in a reverse order string (20050425 for April 25, 2005) that can be used for lists or filenames that need to be sorted by date.
- Date/Time Maths: being add() to add a time interval (days, hour, minutes, seconds) to a date or next() to set the date for a specific time of day (as in 5pm in 3 days time).
- Date Terms: being able to add or subtract a term from a date or to be able to extract a term between two dates. Very useful for loans, leases and other similar software requirements.
- Days in Month: I've often found a need to work out the days in a particular month for various scheduling or display purposes. For this reason I've exported a static method that provides this information, given the year and month.








0 Comments:
Post a Comment
<< Home