Java Calendar Class

Java Calendar Class

Java Calendar class is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable interface.


Java Calendar class declaration

Let’s see the declaration of java.util.Calendar class.

  1. public abstract class Calendar extends Object
  2. implements Serializable, Cloneable, Comparable<Calendar>

Methods of Java Calendar

Method Description
abstract void add(int field, int amount) It is used to add or subtract the specified amount of time to the given calendar field, based on the calendar’s rules.
int get(int field) It is used to return the value of the given calendar field.
static Calendar getInstance() It is used to get a calendar using the default time zone and locale.
abstract int getMaximum(int field) It is used to return the maximum value for the given calendar field of this Calendar instance.
abstract int getMinimum(int field) It is used to return the minimum value for the given calendar field of this Calendar instance.
void set(int field, int value) It is used to set the given calendar field to the given value.
void setTime(Date date) It is used to set this Calendar’s time with the given Date.
Date getTime() It is used to return a Date object representing this Calendar’s time value.

 

Java Calendar Class Example

  1. import java.util.Calendar;
  2. public class CalendarExample {
  3.    public static void main(String[] args) {
  4.    Calendar calendar = Calendar.getInstance();
  5.    System.out.println(“The current date is : “ + calendar.getTime());
  6.    calendar.add(Calendar.DATE, –15);
  7.    System.out.println(“15 days ago: “ + calendar.getTime());
  8.    calendar.add(Calendar.MONTH, 4);
  9.    System.out.println(“4 months later: “ + calendar.getTime());
  10.    calendar.add(Calendar.YEAR, 2);
  11.    System.out.println(“2 years later: “ + calendar.getTime());
  12.    }
  13. }

Output:

The current date is : Thu Jan 19 18:47:02 IST 2017
15 days ago: Wed Jan 04 18:47:02 IST 2017
4 months later: Thu May 04 18:47:02 IST 2017
2 years later: Sat May 04 18:47:02 IST 2019

Java Calendar Class Example: get()

  1. import java.util.*;
  2. public class CalendarExample{
  3.   public static void main(String[] args) {
  4.    Calendar calendar = Calendar.getInstance();
  5.    System.out.println(“At present Calendar’s Year: “ + calendar.get(Calendar.YEAR));
  6.    System.out.println(“At present Calendar’s Day: “ + calendar.get(Calendar.DATE));
  7.    }
  8. }

Output:

At present Calendar's Year: 2017
At present Calendar's Day: 20

Java Calendar Class Example: getInstance()

  1. import java.util.*;
  2. public class CalendarExample{
  3.    public static void main(String[] args) {
  4.    Calendar calendar = Calendar.getInstance();
  5.    System.out.print(“At present Date And Time Is: “ + calendar.getTime());
  6.    }
  7. }

Output:

At present Date And Time Is: Fri Jan 20 14:26:19 IST 2017

Java Calendar Class Example: getMaximum()

  1. import java.util.*;
  2. public class CalendarExample {
  3.    public static void main(String[] args) {
  4.    Calendar calendar = Calendar.getInstance();
  5.    int maximum = calendar.getMaximum(Calendar.DAY_OF_WEEK);
  6.    System.out.println(“Maximum number of days in week: “ + maximum);
  7.    maximum = calendar.getMaximum(Calendar.WEEK_OF_YEAR);
  8.    System.out.println(“Maximum number of weeks in year: “ + maximum);
  9.    }
  10. }

Output:

Maximum number of days in week: 7
Maximum number of weeks in year: 53

Java Calendar Class Example: getMinimum()

  1. import java.util.*;
  2. public class CalendarExample {
  3.    public static void main(String[] args) {
  4.    Calendar cal = Calendar.getInstance();
  5.    int maximum = cal.getMinimum(Calendar.DAY_OF_WEEK);
  6.    System.out.println(“Minimum number of days in week: “ + maximum);
  7.    maximum = cal.getMinimum(Calendar.WEEK_OF_YEAR);
  8.    System.out.println(“Minimum number of weeks in year: “ + maximum);
  9.    }
  10. }

Output:

Minimum number of days in week: 1
Minimum number of weeks in year: 1
Next TopicJava TimeZone

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java Calendar Class Java Calendar class is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable interface. Java Calendar class declaration Let’s see the declaration of java.util.Calendar class. public abstract class Calendar extends Object […]

Tags:

....
...