Calendar - sample program in Java

By: Lakshmi Viewed: 156002 times  Printer Friendly Format    


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(months[calendar.get(Calendar.MONTH)]);
System.out.print(" " + calendar.get(Calendar.DATE) + " ");
System.out.println(calendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
// 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) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}

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



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

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

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

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

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

5. i want a calendar that shows the year, the month a
View Tutorial          By: steven quijano at 2010-03-18 20:50:37

6. ,./;'wat i searching for is that, the complete jav
View Tutorial          By: chor at 2010-03-28 23:09:37

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

8. i want java code for day planner please send me on
View Tutorial          By: sumaiya pathan at 2010-10-15 21:34:23

9. thank you for this info. about the Java program;th
View Tutorial          By: Eljun at 2011-01-11 03:28:09

10. i want to the out put is
calander

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

11. thank you for posting that!!! it helped me a lot@_
View Tutorial          By: laila at 2011-10-06 00:07:43

12. i was after a user interactive calendar where they
View Tutorial          By: Harry at 2011-11-17 09:01:07

13. I discover unlimited calendar.this calendar use of
View Tutorial          By: Lalit Kumbhani at 2011-12-31 16:02:06

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

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

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

17. its good,,,can it be done more simpler without usi
View Tutorial          By: gahanaatherya at 2013-10-14 11:50:02

18. Thanks nice code..If anybody wants to know the int
View Tutorial          By: Anil Nivargi at 2014-07-26 10:12:22

19. nice code..for more Date and Calendar examples Ple
View Tutorial          By: Anil Nivargi at 2014-08-24 17:45:56

20. nice article...for more Date and Calendar examples
View Tutorial          By: Anil Nivargi at 2014-08-25 17:49:19

21. For more Java Date and Calendar Examples and Conve
View Tutorial          By: Anil Nivargi at 2014-09-07 10:10:15

22. This program is faulty .

Check for

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

23. I need program for this question
To accept

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