Calendar - sample program in Java

By: Lakshmi  

The abstract Calendar class provides a set of methods that allows you to convert a time in milliseconds to a number of useful components. Some examples of the type of information that can be provided are: year, month, day, hour, minute, and second. It is intended that subclasses of Calendar will provide the specific functionality to interpret time information according to their own rules. This is one aspect of the Java class library that enables you to write programs that can operate in several international environments. An example of such a subclass is GregorianCalendar.

Calendar provides no public constructors.

Calendar defines several protected instance variables. areFieldsSet is a boolean that indicates if the time components have been set. fields is an array of ints that holds the components of the time. isSet is a boolean array that indicates if a specific time component has been set. time is a long that holds the current time for this object. isTimeSet is a boolean that indicates if the current time has been set.

The following program demonstrates several Calendar methods:

// Demonstrate Calendar
import java.util.Calendar;
class CalendarDemo {
public static void main(String args[]) {
String months[] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
// Create a calendar initialized with the
// current date and time in the default
// locale and timezone.
Calendar calendar = Calendar.getInstance();
// Display current time and date information.
System.out.print("Date: ");
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
// Set the time and date information and display it.
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 29);
calendar.set(Calendar.SECOND, 22);
System.out.print("Updated time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");

Sample output is shown here:
Date: Jan 25 1999
Time: 11:24:25
Updated time: 10:29:22

Archived Comments

1. I need program for this question
To accept date from user. If it is sat/sun - tell it is a we

View Tutorial          By: Prasanna Kumar at 2016-02-10 03:16:42

2. This program is faulty .

Check for 2014 with Any month..crosscheck with day and date.

View Tutorial          By: Shrikant Kale at 2014-09-09 07:31:04

3. For more Java Date and Calendar Examples and Conversions please go through this blog <a href=&quo
View Tutorial          By: Anil Nivargi at 2014-09-07 10:10:15

4. nice article...for more Date and Calendar examples please go through this blog <a href="http
View Tutorial          By: Anil Nivargi at 2014-08-25 17:49:19

5. nice code..for more Date and Calendar examples Please go through this blog <a href="http://a
View Tutorial          By: Anil Nivargi at 2014-08-24 17:45:56

6. Thanks nice code..If anybody wants to know the interview questions with answers then please go throu
View Tutorial          By: Anil Nivargi at 2014-07-26 10:12:22

7. its good,,,can it be done more simpler without using arrays because i find it difficult.......using
View Tutorial          By: gahanaatherya at 2013-10-14 11:50:02

8. can i also get the variable description for this program??
View Tutorial          By: Maya Rao at 2013-10-04 06:06:00

9. Epic fail code. !!!! ( same comment by Mishika Siag )
View Tutorial          By: Vijay Vyas at 2013-03-04 07:24:50

10. Very nice demo. thanks! :)
View Tutorial          By: Nikhil Gala at 2012-01-13 10:03:57

11. I discover unlimited calendar.this calendar use of Javascript.This calendar is worlds first calendar
View Tutorial          By: Lalit Kumbhani at 2011-12-31 16:02:06

12. i was after a user interactive calendar where they can see the calendar for the entire month so that
View Tutorial          By: Harry at 2011-11-17 09:01:07

13. thank you for posting that!!! it helped me a [email protected][email protected]
View Tutorial          By: laila at 2011-10-06 00:07:43

14. i want to the out put is

View Tutorial          By: sampath at 2011-07-18 08:30:01

15. thank you for this info. about the Java program;through w/ this I could able to learn bec. there's a
View Tutorial          By: Eljun at 2011-01-11 03:28:09

16. i want java code for day planner please send me on my email
my email id is [email protected]

View Tutorial          By: sumaiya pathan at 2010-10-15 21:34:23

17. plz
View Tutorial          By: shivdeep at 2010-07-27 00:56:03

18. ,./;'wat i searching for is that, the complete java program of 2010 calendar,.;'which shows the ff.<
View Tutorial          By: chor at 2010-03-28 23:09:37

19. i want a calendar that shows the year, the month and the whole day. But its alrigth.
View Tutorial          By: steven quijano at 2010-03-18 20:50:37

20. i Want the Flow Chart and code for the mobile Calendar in j2me
View Tutorial          By: Sudip Khatiwara at 2010-03-15 04:10:41

21. i Want the Flow Chart for the Calendar Demo Program
View Tutorial          By: Narayanan V at 2010-01-06 18:57:25

22. hi..
View Tutorial          By: jazzmine alfad at 2009-03-05 17:30:26

23. this is the one of the best site to search java program.
View Tutorial          By: gautam gorai at 2009-01-13 06:44:15

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial