June 28, 2004

Manipulating time and dates in Java

Need to get a certain date or time -- other than right now? Here's a hint: don't use java.util.Date. Use java.util.GregorianCalendar instead. For example, if you needed a date object that represented the moment twenty four hours ago:

java.util.GregorianCalendar lastAcceptable = 
         new java.util.GregorianCalendar();
lastAcceptable.add(java.util.Calendar.HOUR, -24);

You can use any of the constants defined in java.util.Calendar to add or subtract time from a date. Check out the GregorianCalendar API for more information on manipulating and retreiving dates and times.

Technorati tags:

src="http://feeds.feedburner.com/~s/ChaosMagnet?i=http://www.chaosmagnet.com/blog/archives/000453.html" type="text/javascript" charset="utf-8">
Posted by roadnick at June 28, 2004 03:08 PM | TrackBack

Comments

Post a comment









Remember personal info?