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 . |
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. |
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]
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]
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]
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]
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]
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]
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]
getTimeZone()
is the method of java.util Calender Class to get the time zone.