Java Calendar Class Tutorial in Programming Languages Learning

Categories: Language

Introduction

This tutorial is designed to provide you with an understanding of the Java Calendar class, it’s backgrounds, purpose and uses. It is created in such a manner as to allow even beginning Java programmers an understanding and feel for the uses of the Java Calendar class. The Java Calendar class was added to the Java Development Kit in JDK 1.1. It is designed to permit the conversion between a specific instance in time and a set of Calendar fields. (Oracle, 2004, 2010) What exactly does this mean? The Calendar class uses a huge array of tables to keep track of time based on daylight savings time, timezones, and calendar history, the calendar class uses a system time to determine where whatever event is being tracked falls on in the internal calendar tables.

Some confusion is caused by this as Java does keep track of specific location time, and the programmer needs to keep this in mind when using the Calendar class even when it isn’t relevant to the problem being addressed (Roedy Green, Canadian Mind Products, 1196-2011).

Get quality help now
KarrieWrites
KarrieWrites
checked Verified writer

Proficient in: Language

star star star star 5 (339)

“ KarrieWrites did such a phenomenal job on this assignment! He completed it prior to its deadline and was thorough and informative. ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

How to use the Calendar Class

The Calendar Class and it’s associated methods are used by three methods.

  • The programmer can import the calendar class and whatever specific method or constructor they will use in their program by importing it as importjava.util.specificfunction;
  • For instance, if the programmer wished to set the date within a program, but that was all that was needed, for instance no date specific math was needed as in a log file, then the programmer could use, importjava.
    Get to Know The Price Estimate For Your Paper
    Topic
    Number of pages
    Email Invalid email

    By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

    "You must agree to out terms of services and privacy policy"
    Write my paper

    You won’t be charged yet!

    util.Calendar.set;

  • If the programmer needs to have greater functionality associated with the dates being used within a program the programmer should use the entire Calendar class. This takes up more space within a program, however provides a much simpler method of calling items from the Java Calendar class as all of it’s functionality is available to the entire program. This format would simply be, importjava.util.Calendar;
  • Finally the programmer can call within the program any specific function required within the Calendar class by using a fully qualified name for the method being used, if the programmer wished to use the set function once within a program and that was all that was needed the programmer could simply use public void java.util.Calendar.set

For ease of use of associated Java Calendar methods and constructs however it is generally recommended that the programmer use the import function and import the entire Calendar class. This provides for significantly less time coding, and less possibility of mistakes in typing out commands and code as less code is necessary.

Section Example 1

Every so often a program wishes to know something about a date, such as what day of the week something took place or maybe you would like to know which of the 12 months have 30 days. This can be completed with the Calendar class within the java.util package. The very first thing that should be done is to import the package: import java.util.Calendar;

The next thing to do is to get an instance of the Calendar class: Calendar cal = Calendar.getInstance( ); Be aware that the constructor cannot directly be called upon with new Calendar( );, since it’s an abstract class. The next thing to do is set the date and time of what is wanted to know about certain things: cal.set(year,month,day);

For the month parameter, January is 0, February is 1, March is 2, etc. The constants Calendar.MONTH can also be used. Each and every calendar has a set of boundaries that are automatically updated when the calendar is altered. The get ( ) method can access these and a set of constants that characterize a number of available fields. So it can be noted that some very precise things can be done with the calendar now. For example, the week of the year that a day falls on can be found by using: int week = cal.get(Calendar.WEEK_OF_YEAR)

Or For example. Use the getActualMaximum() to find the number of days in a certain month: intdaysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); Here are some fields that may be useful within the Calendar Class:

  • DAY_OF_WEEK - Returns the day of the week that a day falls on, 1 through 7 days.
  • DAY_OF_YEAR - Returns the number of days into the year that the date occurs
  • WEEK_OF_MONTH - Returns the week number in the current month where the date occurs
  • DAY_OF_MONTH - Returns the current day of the month

Section Example 2

Examples of Calendar Class Use:
Below are some typical examples of the usage of the java calendar class. /* Java Calendar
Follow along with this example in a text editor to have java show todays date and time. */
import java.util.Calendar;
public class JavaCalendar {
public static void main (string[] args) {
Calendar cal = Calendar.getInstance() ;
System.out.println("Today is : " + cal.getTime() ) ;

By following the formats for cal.get instances mentioned above, one can find the various dates, times, weeks, and months in current formats, or in predetermined periods. This method allows programmers to set specific time constraints when coding for specific results.

End of Section

Methods Within the Calendar Class
There are many methods within the calendar class. Methods are used for querying, setting, and doing arithmetic on the various fields of the date and time. The most commonly used methods are: * add( )

* set( )
* roll( )

References

  1. Oracle. (2004, 2010). java.util Class Calendar. Retrieved from http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set(int, int)
  2. Roedy Green, Canadian Mind Products. (1196-2011). Calendar : Java Glossary. Retrieved from http://mindprod.com/jgloss/calendar.html
Updated: Nov 01, 2022
Cite this page

Java Calendar Class Tutorial in Programming Languages Learning. (2016, Jun 07). Retrieved from https://studymoose.com/java-calendar-class-tutorial-in-programming-languages-learning-essay

Java Calendar Class Tutorial in Programming Languages Learning essay
Live chat  with support 24/7

👋 Hi! I’m your smart assistant Amy!

Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.

get help with your assignment