how to create and Modifry Calendar using collections in java
Previous | Home | Next |
In this example we are going to create a Calendar and modify it.
For using the class Calendar, firstly we have to import the java.util package which contains the java.util.Calendar class in which contains various definitions and methods which can be applied on the Calendar object. In the following example we have created a class named CollectionExample in which we have created an instance of Calendar and defined various methods.
In this example we have created two user defined methods which are as follows,
IncrementDate(Calendar obj)
This method increments the current date to certain amount of days. This method takes the Calendar object as a parameter. Inside this method we have used the add() method on the Calendar object which have the following syntax,
abstract void add(int date, int amountoftime)
The above method adds or subtracts the specified amount of time to the given calendar date, based on the calendar's rules.
DecrementDate(Calendar obj)
This method decrements the current date to certain amout of days. This method takes the Calendar object as parameter. In this method we have used the add() method on the Calendar object.
In this example we have created a class named Collection Example, in which we have created an object of the Calendar class and initialized it by invoking the static method getInstance() on the Calendar. Then we have displayed the current date and time by using the getTime() on the Calendar instance.
We have also created an object of the CollectionExample and invoked the IncrementDate(Calendar obj) and DecrementDate(Calendar obj) on its objects.
package r4r.co.in;
import java.util.*;
public class CollectionExample
{
/**
* @param args
*/
public static void main(String args[])
{
Calendar clndr=Calendar.getInstance(); // getting a calendar instance
System.out.println("The current time is given as: "+clndr.getTime()); // displaying the time
CollectionExample ce=new CollectionExample(); // creating the object of the CollectionExample class
ce.IncrementDate(clndr); // Invoking the IncrementDate method on the object of CollectionExample class
System.out.println("After incrementing the date, now the time is: "+clndr.getTime()); // displaying the time
ce.DecrementDate(clndr); // Invoking the DecrementDate method on the object of the CollectionExample class
System.out.println("After decrementing the date, now the time is: "+clndr.getTime()); // displaying the time
}
/* Following method will prepond the value of Date */
private void IncrementDate(Calendar clndr)
{
clndr.add(Calendar.DAY_OF_MONTH, 5); // using the add() method on the Calendar instance
}
/* Following method will postpond the value of Date */
private void DecrementDate(Calendar clndr)
{
clndr.add(Calendar.DAY_OF_MONTH, -7);
}
}
The output of the above example is given as:
The current time is given as: Mon Nov 15 18:12:18 PST 2010
After incrementing the date, now the time is: Sat Nov 20 18:12:18 PST 2010
After decrementing the date, now the time is: Sat Nov 13 18:12:18 PST 2010
Previous | Home | Next |