Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to create and Modifry Calendar using collections in java

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