Java.util - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util Calender

Java.util Calender

shape Description

In programming, the calendar is a unique base class for changing over between a Date protest and a game plan of entire number fields, for instance, YEAR, MONTH, DAY, HOUR, and so forth. Where a Date protest addresses a specific minute in time with millisecond exactness. Taking after is the language structure presentation for java.util.Calendar class. [java] public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar> [/java] Following are the different fields for java.util.Calendar class.
Fields Description
AM To show the time of the day from midnight to simply before twelve.
AM_PM This is the field number for get and set showing whether the HOUR is before or evening.
APRIL Helpful constant for month.
DATE Helpful constant for time and date.
DAY_OF_MONTH To indicate the day of the month.
DAY_OF_WEEK To indicate the day of the week.
ERA To indicate the ers such as AD,BC .

shape Conceptual Figure

Following is the conceptual figure for java.util Calender Class. The java.util.Calender class will hire the methods from java.lang.Object class. The concrete subclass of calendar class is java.util.GregorianCalendar class.

Methods for java.util Calender Class

shape Methods

Following are the basic and regular using methods for java.util Calender Class.
Methods Description
add(int, int) It is the arithmetic function of date.
after(Object) To return whether this Calendar speaks to a period after the time spoke to by the predetermined Object.
before(Object) To return whether this Calendar speaks to a period before the time spoke to by the predetermined Object.
clear() To clear the estimations of all the time fields.
clone() To make and give back a duplicate of this object.
int compareTo(Calendar anotherCalendar) To analyze the time values spoke to by two Calendar objects.
getInstance() To get a Calendar utilizing the default timezone and region.
roll(int, boolean) To include or subtract a solitary unit of time on the given time field without changing bigger fields.

By Using java.util.Calendar.add() Method

shape Description

The following is the syntax declaration of the java.util.Calendar.add(). [java]public abstract void add(int field,int amount)[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; public class DemoCalendar { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // print current date System.out.println("The current date is : " + cal.getTime()); // add 20 days to the calendar cal.add(Calendar.DATE, 15); System.out.println("20 days later: " + cal.getTime()); // subtract 2 months from the calendar cal.add(Calendar.MONTH, -3); System.out.println("2 months ago: " + cal.getTime()); // subtract 5 year from the calendar cal.add(Calendar.YEAR, -6); System.out.println("5 years ago: " + cal.getTime()); } } [/java] The getInstance() is the method to get a Calendar utilizing the default timezone and region. Output: Now compile the code result will be as follows. [java] The current date is : Fri Oct 14 10:05:56 IST 2016 20 days later: Sat Oct 29 10:05:56 IST 2016 2 months ago: Fri Jul 29 10:05:56 IST 2016 5 years ago: Thu Jul 29 10:05:56 IST 2010 [/java]

By Using java.util.Calendar.after() Method

shape Description

The usefulness of this technique is to return whether this Calendar addresses a period after the time addressed by an Object. The following is the syntax declaration of the java.util.Calendar.after(). [java]public boolean after(Object when)[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; import java.util.Date; public class DemoCalendar { public static void main(String[] args) { // create calendar objects. Calendar cal = Calendar.getInstance(); Calendar future = Calendar.getInstance(); // print the current date System.out.println("Current date: " + cal.getTime()); // change year in future calendar future.set(Calendar.YEAR, 2016); System.out.println("Year is " + future.get(Calendar.YEAR)); // check if calendar date is after current date Date time = future.getTime(); if (future.after(cal)) { System.out.println("Date " + time + " is after current date."); } } } [/java] The getInstance() is the method to get a Calendar utilizing the default timezone and region. Output: Now compile the code result will be as follows. [java] Current date: Fri Oct 14 10:14:31 IST 2016 Year is 2016 Date Fri Oct 14 10:14:31 IST 2016 is after current date. [/java]

By Using java.util.Calendar.before() Method

shape Description

The following is the syntax declaration of the java.util.Calendar.before(). [java]public boolean before(Object when)[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; import java.util.Date; public class DemoCalendar { public static void main(String[] args) { // create calendar objects. Calendar cal = Calendar.getInstance(); Calendar past = Calendar.getInstance(); // print the current date System.out.println("Current date: " + cal.getTime()); // change year in past calendar past.set(Calendar.YEAR, 2008); System.out.println("Year is " + past.get(Calendar.YEAR)); // check if calendar date is before current date System.out.println("Before current date:" + cal.before(past)); } } [/java] The getTime() is a method to give current time for an application. The getInstance() is the method to get a Calendar utilizing the default timezone and region. Output: Now compile the code result will be as follows. [java] Current date: Fri Oct 14 10:22:32 IST 2016 Year is 2008 Before current date:false [/java]

By Using java.util.Calendar.clear() Method

shape Description

The functionality of this method is to clear the estimations of all the time fields. Following is the syntax declaration for java.util.Calendar.clear() method. [java]public final void clear()[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; import java.util.Date; public class DemoCalendar { public static void main(String[] args) { // create calendar object Calendar cal = Calendar.getInstance(); // displays the current date and time System.out.println("The current Calendar Date: " + cal.getTime()); // use clear method to set all field values and time value as undefined. cal.clear(); // print the result System.out.println("The calendar shows : " + cal.getTime()); } } [/java] Output: Now compile the code result will be as follows. [java] Current Calendar Date: Fri Oct 14 10:32:26 IST 2016 The calendar shows : Thu Jan 01 00:00:00 IST 1970 [/java]

By Using java.util.Calendar.clone() Method

shape Description

The functionality of this method is to to make and give back a duplicate of this object. Following is the syntax declaration for java.util.Calendar.clone() method. [java]public Object clone()[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DemoCalendar { public static void main(String[] args) { // create new calendar at specific date. Calendar cal = new GregorianCalendar(2016, 06, 10); // print date for default value System.out.println("The past calendar : " + cal.getTime()); // create a clone of first cal Calendar cal2 = (Calendar) cal.clone(); // display the copy System.out.println("The cloned calendar : " + cal2.getTime()); } } [/java] The java.util.GregorianCalendar class is a solid subclass of Calendar and gives the standard date-book framework utilized by the greater part of the world. Output: Now compile the code result will be as follows. [java] The past calendar : Sun Jul 10 00:00:00 IST 2016 The cloned calendar : Sun Jul 10 00:00:00 IST 2016 [/java]

By Using java.util.Calendar.compareTo() Method

shape Description

The functionality of this method is to analyze the time values spoke to by two Calendar objects. Following is the syntax declaration for java.util.Calendar.compareTo() method. [java]public int compareTo(Calendar anotherCalendar)[/java] DemoCalendar.java [java] package com.SPlessons; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DemoCalendar { public static void main(String[] args) { // create two calendar at the different dates Calendar cal1 = new GregorianCalendar(2016, 8, 14); Calendar cal2 = new GregorianCalendar(2006, 11, 12); // compare the time values represented by two calendar objects. int i = cal1.compareTo(cal2); // return positive value if equals else return negative value System.out.println("The result is :"+i); // compare again but with the two calendars swapped int j = cal2.compareTo(cal1); // return positive value if equals else return negative value System.out.println("The result is :" + j); } } [/java] Output: Now compile the code result will be as follows. [java] The result is :1 The result is :-1 [/java]

By Using java.util.Calendar.roll() Method

shape Description

The usefulness of this technique is to return whether this Calendar addresses a period after the time addressed by an Object. Following is the syntax declaration for java.util.Calendar.roll() method. [java]public abstract void roll(int field,boolean up)[/java] DemoCalendar.java [java]package com.SPlessons; import java.util.*; public class DemoCalendar { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // displays the current calendar System.out.println("Month is " + cal.get(Calendar.MONTH)); // roll month cal.roll(Calendar.MONTH, true); // print result after rolling System.out.println("Month is " + cal.get(Calendar.MONTH)); // roll downwards cal.roll(Calendar.MONTH, false); // print result after rolling down System.out.println("Month is " + cal.get(Calendar.MONTH)); } } [/java] Output: Now compile the code result will be as follows. [java] Month is 9 Month is 10 Month is 9 [/java]

Summary

shape Key Points

  • Java.util Calender - The getTimeZone() is the method of java.util Calender Class to get the time zone.
  • The getTime() is the method to get current time.
  • The isLenient() is the method to tell interpretation of date/time is lenient.