Calendar Example

In this section, we are discussing the entire functionality of java.util.Calender class

Calendar Example

In this section, we are discussing the entire functionality of java.util.Calender class

Calendar Example

Calendar Example

     

In this section, we are discussing the entire functionality of java.util.Calender class. In the following code segment we are performing various operations on Date object using its methods. The method getTime() of the calendar class returns a Date object that is then passed to the println() method just to print today's date. The arithmetic function of the date class adds specific amount of time to the given time field following the calendar's rule. This example subtracts two years from the current date of the calendar and also add 3 days to the current date of the calendar.

Description of program:

In the following code segment, first we are getting the current date and time into the date object by using the
getInstance() and getTime() methods of the calendar class in a method CalendarTimemethod() and printing this current date and time on the console. 

Then we are taking another method SimpleDateFormatmethod(), this method is getting an instance of the calendar class into the date object. Now we are taking an object of SimpleDateFormat class. Now we are getting the current date and time from the date object by calling getTime() method on this object. Now we are passing this current date and time into the format() method of the dateformatter object (SimpleDateFormat class) just to get the current date and time into the simple date format, after that we are printing current date and time on the console.

Now we are taking another method Adddates() that adds two different dates. In this method we are taking an instance date of the calendar class into a reference variable cldr and an object dateformatter of the SimpleDateFormat class. Now we are taking the clone of the calendar class and passing the reference of this clone into cldr. Now we are getting the dates of two years ago and five years after, by using the add() method and printing these values on the console.

Now we are taking another method that is DateDifference(). In this method we are taking date and time from an object of the GregorianCalendar class and passing it into an object startDate1 of the Date class and also taking an another object endDate1 of the date class. Now we are taking the difference of times of these two objects and printing it on the console.

Now in the last Getcalendermethods() we are displaying values to demonstrate the various method of the calendar class.

Here is the code of program:

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;

public class CalendarExample {
  
  private static void CalendarTimemethod() {
  Date date = Calendar.getInstance().getTime();
  System.out.println("Current date and time is: " + date);
  System.out.println();
  }

  private static void SimpleDateFormatmethod() {
  Calendar date = Calendar.getInstance();
  SimpleDateFormat dateformatter = new SimpleDateFormat
  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
  System.out.println("Current date and 

time in simple date format: " 
  + dateformatter.format(date.getTime()));
  System.out.println();
  }

  private static void Adddates() {

  System.out.println("Performing 

operations on calendar dates.");

  // Get today's date
  Calendar date = Calendar.getInstance();
  Calendar cldr;
  SimpleDateFormat dateformatter = new SimpleDateFormat
  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

  cldr = (Calendar) date.clone();
  cldr.add(Calendar.DAY_OF_YEAR, - (365 * 2));
  System.out.println("Before two years it was: "
  + dateformatter.format(cldr.getTime()));

  cldr = (Calendar) date.clone();
  cldr.add(Calendar.DAY_OF_YEAR, + 5);
  System.out.println("After five years it will be: " 
 + dateformatter.format(cldr.getTime()));

  System.out.println();
  }

  private static void DateDifference() {
  
  System.out.println("Difference between two dates");
  Date startDate1 = new GregorianCalendar(2005, 02, 

25, 14, 00).getTime();
  Date endDate1 = new Date();;

  long diff = endDate1.getTime() - startDate1.getTime();

  System.out.println("  Difference between " + endDate1);
  System.out.println("  and " + startDate1 + " is " 
   + (diff / 

(1000L*60L*60L*24L)) + " days.");
  System.out.println();
  }

  private static void Getcalendermethods() {

  System.out.println("Various get methods 

of the calendar class:");
  Calendar calender = Calendar.getInstance();

  System.out.println("Year : " 

+ calender.get(Calendar.YEAR));
  System.out.println("Month  : " 

+ calender.get(Calendar.MONTH));
  System.out.println("Day of Month  : " 

+ calender.get(Calendar.DAY_OF_MONTH));
  System.out.println("Day of Week  : " 

+ calender.get(Calendar.DAY_OF_WEEK));
  System.out.println("Day of Year  : " 

+ calender.get(Calendar.DAY_OF_YEAR));
  System.out.println("Week of Year  : " 

+ calender.get(Calendar.WEEK_OF_YEAR));
  System.out.println("Week of Month  : " 

+ calender.get(Calendar.WEEK_OF_MONTH));
  System.out.println
   ("Day of the Week in Month : " 

+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
  System.out.println("Hour  

 : " + calender.get(Calendar.HOUR));
  System.out.println("AM PM 

 : " + calender.get(Calendar.AM_PM));
  System.out.println("Hour of the Day 

 : " + calender.get(Calendar.HOUR_OF_DAY));
  System.out.println("Minute 

  : " + calender.get(Calendar.MINUTE));
  System.out.println("Second  

 : " + calender.get(Calendar.SECOND));
  System.out.println();
  }

  public static void main(String[] args) {
  System.out.println();
  CalendarTimemethod();
  SimpleDateFormatmethod();
  Adddates();
  DateDifference();
  Getcalendermethods();
  }
}

Here is the output:

C:\Examples>java CalendarExample

Current date and time is: Mon Dec 10 18:37:06 GMT+05:30 2007

Current date and time in simple date format: Mon 2007.12.10 at 06:37:07 PM GMT+05:30

Performing operations on calendar dates.
Before two years it was: Sat 2005.12.10 at 06:37:07 PM GMT+05:30
After five years it will be: Sat 2007.12.15 at 06:37:07 PM GMT+05:30

Difference between two dates
Difference between Mon Dec 10 18:37:07 GMT+05:30 2007
and Fri Mar 25 14:00:00 GMT+05:30 2005 is 990 days.

Various get methods of the calendar class:
Year : 2007
Month : 11
Day of Month : 10
Day of Week : 2
Day of Year : 344
Week of Year : 50
Week of Month : 3
Day of the Week in Month : 2
Hour : 6
AM PM : 1
Hour of the Day : 18
Minute : 37

Download of this program.

  0