Calendar Class in Java With Examples

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Calendar Class in Java with examples

Calendar class in Java 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, Serializable, Cloneable interfaces.
As it is an Abstract class, so we cannot use a constructor to create an
instance. Instead, we will have to use the static method
Calendar.getInstance() to instantiate and implement a sub-class.
 Calendar.getInstance(): return a Calendar instance based on the
current time in the default time zone with the default locale.
 Calendar.getInstance(TimeZone zone)
 Calendar.getInstance(Locale aLocale)
 Calendar.getInstance(TimeZone zone, Locale aLocale)
Locale :- Specific language in a specific COUNTRY
English :- en
France- french :-- fr-FR
Germany de-DE
China ch-ZN

import java.util.*;
public class Calendar1 {
    public static void main(String args[])
    {
        Calendar c = Calendar.getInstance();
        System.out.println("The Current Date is:" + c.getTime());
    }
}

METHOD DESCRIPTION

It is used to add or subtract the specified amount


abstract void add(int of time to the given calendar field, based on the
field, int amount) calendar’s rules.

It is used to return the value of the given calendar


field.
int get(int field)

It is used to return the maximum value for the


abstract int given calendar field of this Calendar instance.
getMaximum(int field)
METHOD DESCRIPTION

abstract int It is used to return the minimum value for the given
getMinimum(int field) calendar field of this Calendar instance.

It is used to return a Date object representing this


Date getTime() Calendar’s time value.

import java.util.*;
public class Calendar2 {
    public static void main(String[] args)
    {
        // creating Calendar object
        Calendar calendar = Calendar.getInstance();
          
        // Demonstrate Calendar's get()method
        System.out.println("Current Calendar's Year: " +
calendar.get(Calendar.YEAR));
        System.out.println("Current Calendar's Day: " +
calendar.get(Calendar.DATE));
        System.out.println("Current MINUTE: " +
calendar.get(Calendar.MINUTE));
        System.out.println("Current SECOND: " +
calendar.get(Calendar.SECOND));
    }
}

import java.util.*;
public class Calendar3 {
    public static void main(String[] args)
    {
        // creating calendar object
        Calendar calendar = Calendar.getInstance();
      
        int max = calendar.getMaximum(Calendar.DAY_OF_WEEK);
        System.out.println("Maximum number of days in a week: " + max);
          
        max = calendar.getMaximum(Calendar.WEEK_OF_YEAR);
        System.out.println("Maximum number of weeks in a year: " + max);
    }
}
public class Calendar5 {
    public static void main(String[] args)
    {
        // creating calendar object
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -15);
        System.out.println("15 days ago: " + calendar.getTime());
        calendar.add(Calendar.MONTH, 4);
        System.out.println("4 months later: " + calendar.getTime());
        calendar.add(Calendar.YEAR, 2);
        System.out.println("2 years later: " + calendar.getTime());
    }
}

Java SimpleDateFormat
The java.text.SimpleDateFormat class is used to both parse and
format dates according to a formatting pattern you specify yourself. When
parsing dates, the Java SimpleDateFormat typically parses the date
from a Java String. When formatting dates,
the SimpleDateFormat typically formats a Date object into a String,
although it can also format the date into a StringBuffer.

Creating a SimpleDateFormat

You create a SimpleDateFormat instance like this:

String pattern = "yyyy-MM-dd";


SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

The pattern parameter passed to
the SimpleDateFormat constructor is the pattern to use for parsing
and formatting of dates. The pattern syntax is covered later in this text. The
pattern is just a regular Java String.

Formatting Dates

Once you have created a SimpleDateFormat instance you can


format dates using its format() method. Here is an example:

String pattern = "yyyy-MM-dd";


SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String date = simpleDateFormat.format(new Date());


System.out.println(date); 2021-07-14

The Date instance passed to the format() method is


a java.util.Date instance.

Java Date Format


The java.text.DateFormat class provides various methods to format and parse date
and time in java in language independent manner. The DateFormat class is an
abstract class. java.text.Format is the parent class and java.text.SimpleDateFormat is
the subclass of java.text.DateFormat class.

In java, converting date into string is called formatting and vice-versa parsing. In
other words, formatting means date to string and parsing means string to date.

import java.text.DateFormat;  
import java.util.Date;  
public class DateFormatExample {  
    public static void main(String[] args) {  
        Date currentDate = new Date();  
        System.out.println("Current Date: "+currentDate);   Wed Jul 14 11:52:….
        String dateToStr = DateFormat.getInstance().format(currentDate);  
        System.out.println("Date Format using getInstance(): "+dateToStr);  
    }  
}

Java DateFormat Example: String to Date


Let's see the simple example to convert string into date using java.text.DateFormat
class.

import java.text.DateFormat;  
import java.util.Date;  
public class DateFormatExample3 {  
    public static void main(String[] args)throws Exception {  
        Date d = DateFormat.getDateInstance().parse("31 July, 2021");  
        System.out.println("Date is: "+d);  
    }  
}  

You might also like