by Dinesh Thakur Category: Swing

In this Example, Calendar class is an abstract class present in java.util package. getInstance method is a static method of Calendar class. This method is used to create the object of Calendar class. Return type of this method is Calendar class object. Syntax-public static Calendar getInstance (). get method is a predefined method of Calendar class. It is used to return the value of a given calendar field. Return type of this method is int type. Syntax-public int get (int)

MONTH, YEAR, DATE are predefined constants of Calendar class. Syntax-public static final int MONTH, public static final int YEAR, public static final int DATE add method is a predefined abstract method of Calendar class. It adds or substracts the specified amount of time to the given calendar field. Here we subtract 10 days from the current date. Syntax-public abstract void add (int field, int amount)

 

Here is the Java Example for Calendar Class

import java.util.Calendar;

public class ShowDateCalendar
{
            public static void main(String[] args)
      {
           Calendar now=Calendar.getInstance();
           System.out.println("Current Date: " +(now.get (Calendar.MONTH)) + "-" + now.get(Calendar.DATE) + "-"+ now.get (Calendar.YEAR));
           now.add(Calendar.DATE,1);
           System.out.println ("Date After One Day: "+ (now.get (Calendar.MONTH)) +"-"+ (now.get (Calendar.DATE) +1) +"-"+ now.get (Calendar.YEAR));
           now=Calendar.getInstance();
           now.add(Calendar.DATE,-10);
           System.out.println ("Date Before 10 Days : " +"-" +(now.get(Calendar.MONTH)) +"-" + (now.get(Calendar.DATE)) + "-" + now.get(Calendar.YEAR));
      }
}

Calendar Class in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article