HudaTutorials.com

java util Calendar Class - java.util.Calendar Class in Java

Last updated on

java.util.Calendar Class

Calendar Class in Java

Java Calendar Class

The Java Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected. Like other locale-sensitive classes, Java Calendar provides a class method, getInstance, for getting a generally useful object of this type. Java Calendar's getInstance method returns a Java Calendar object whose calendar fields have been initialized with the current date and time :

Calendar rightNow = Calendar.getInstance();

A Java Calendar object can produce all the calendar field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional). Java Calendar defines the range of values returned by certain calendar fields, as well as their meaning. For example, the first month of the calendar system has value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such as ERA.

How to Getting and Setting Calendar Field Values in Java ?

The calendar field values can be set by calling the set methods. Any field values set in a Java Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.

What is Leniency in Java Calendar ?

Java Calendar has two modes for interpreting the calendar fields, lenient and non-lenient. When a Java Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Java Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized.

For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.

When a Java Calendar is in non-lenient mode, it throws an exception if there is any inconsistency in its calendar fields. For example, a GregorianCalendar always produces DAY_OF_MONTH values between 1 and the length of the month. A non-lenient GregorianCalendar throws an exception upon calculating its time or calendar field values if any out-of-range field value has been set.

How to convert milliseconds to seconds with precision ?

double seconds = milliseconds / 1000.0;

There's no need to manually do the two parts separately - you just need floating point arithmetic, which the use of 1000.0 (as a double literal) forces. (I'm assuming your milliseconds value is an integer of some form.) Note that as usual with double, you may not be able to represent the result exactly. Consider using BigDecimal if you want to represent 100ms as 0.1 seconds exactly. (Given that it's a physical quantity, and the 100ms wouldn't be exact in the first place, a double is probably appropriate.)

Why do we say 52 weeks in a year ?

There are 52 complete weeks in a year. The year has 365 days, leaving one extra day. A leap year has 366 days, adding a second extra day. This makes 52 1/7 weeks in a normal year and 52 2/7 weeks in a leap year. If the 1st of Jan is on day 0 and there is no leap year, then there are 365 days in the year with the last week having only 2 days, this gives 52 complete weeks of 7 days and a 2 day week. If the year is a leap year then the last week has 3 days. There are 52 complete weeks and a remaining 2 or 3 days in the last incomplete week. Actually there are week number 1 to 53, where week 1 is always the first week that starts on a Sunday and week 53 contains the remaining days which may include the overlap into January of the next year.

Following Java Calendar class example you can learn how to create Java Calendar. And also learn how to use Java Calendar class.

How to create Java Calendar

/*	How to create Java Calendar Example
	Save with file name CalendarExample.java	*/
public class CalendarExample
{
	public static void main(String args[])
	{
		// java.util.Calendar DECLARATION
		java.util.Calendar c;
		// java.util.Calendar OBJECT CREATION
		c = java.util.Calendar.getInstance();
		// java.util.Calendar OUTPUT
		System.out.println(c.getTime());
		// java.util.Date
		java.util.Date d = new java.util.Date(c.getTimeInMillis());
		// java.util.Date OUTPUT
		System.out.println(d);
		// GETTING TIMEZONE
		System.out.println("TimeZone is : "+ c.getTimeZone().getID());
	}
}

Following Java Calendar class example you can learn how to get date and time from java.util.Calendar class with Calendar Fields.

How to get Date and Time from Java Calendar

/*	How to get Date and Time from Java Calendar Example
	Save with file name CalendarExample2.java	*/
import java.util.*;
public class CalendarExample2
{
	public static void main(String args[])
	{
		// java.util.Calendar DECLARATION
		Calendar c;
		// java.util.Calendar OBJECT CREATION
		c = Calendar.getInstance();
		// java.util.Calendar OUTPUT
		System.out.println(c.getTime());
		// CALENDAR DATE
		System.out.println("Date : " + c.get(Calendar.DATE));
		// CALENDAR MONTH
		// YOU SHOULD ADD +1 FOR OUTPUT
		// i.e. JAN = 0, FEB = 1,...,NOV = 10,DEC =11
		System.out.println("Month : " + (c.get(Calendar.MONTH)+1));
		// CALENDAR YEAR
		System.out.println("Year : " + c.get(Calendar.YEAR));
		// CALENDAR HOUR
		System.out.println("Hour : " + c.get(Calendar.HOUR));
		// CALENDAR MINUTE
		System.out.println("Minute : " + c.get(Calendar.MINUTE));
		// CALENDAR SECOND
		System.out.println("Second : " + c.get(Calendar.SECOND));
		// CALENDAR AM OR PM (AM = 0, PM = 1)
		System.out.println("AM OR PM : " + c.get(Calendar.AM_PM));
	}
}

Following Java Calendar class example you can learn how to compare two java.util.Calendar objects and how to get the Maximum value and Minimum value of particular Calendar Field.

How to compare two Java Calendar objects

/*	How to compare two Java Calendar objects Example
	Save with file name CalendarExample3.java	*/
import java.util.*;
public class CalendarExample3
{
	public static void main(String args[])
	{
		// java.util.Calendar DECLARATION
		Calendar c, d;
		// java.util.Calendar OBJECT CREATION
		c = Calendar.getInstance();
		d = Calendar.getInstance();
		// java.util.Calendar OUTPUT
		System.out.println("c value : " + c.getTime());
		System.out.println("d value : " + d.getTime());
		System.out.println("c after d : " + c.after(d));
		System.out.println("c before d : " + c.before(d));
		System.out.println("c compareTo d : " + c.compareTo(d));
		System.out.println("c equals d : " + c.equals(d));
		System.out.println("getActualMaximum:"+c.getActualMaximum(Calendar.DATE));
		System.out.println("getActualMinimum:"+c.getActualMinimum(Calendar.DATE));
	}
}

Following Java Calendar class example you can learn how to set the values to java.util.Calendar Object.

How to set the values to java.util.Calendar Object

/*	How to set the values to java.util.Calendar Object Example
	Save with file name CalendarExample4.java	*/
import java.util.*;
public class CalendarExample4
{
	public static void main(String args[])
	{
		// java.util.Calendar DECLARATION
		Calendar c;
		// java.util.Calendar OBJECT CREATION
		c = Calendar.getInstance();
		// java.util.Calendar OUTPUT
		System.out.println("c value : " + c.getTime());
		// SET THE VALUES FOR CALENDAR
		c.set(2011,9,1,2,3,4);
		// java.util.Calendar OUTPUT AFTER SETTING THE VALUES
		System.out.println("c value after change : " + c.getTime());
	}
}