Posts

Showing posts from August 19, 2007

Calendar class - Better Way to Manipulate Dates.

Java’s Calendar class offers a set of methods for converting and manipulating temporal information. In addition to retrieving the current date and time, the Calendar class also provides an API for date arithmetic. Calendar’s built-in date/time arithmetic API is extremely useful. This tutorial examines the Calendar class API and presents examples of how you can use Calendar objects to add and subtract time spans to and from dates and times, as well as how to evaluate whether one date precedes or follows another. Adding time spans Let’s say you want to add a time span to a starting date and print the result. Consider the following example, which initializes a Calendar to 16 July 2007 and then adds two months and three days to it to obtain a new value: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; public class CalenderDemo{ public static void main(String[] args) { CalenderDemo tdt = new CalenderDemo(); tdt.calculateDa