Calendar - sample program in Java

By: Lakshmi Emailed: 1766 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(13)


1. View Comment

this is the one of the best site to search java program.

View Tutorial          By: gautam gorai at 2009-01-13 06:44:15
2. View Comment

i Want the Flow Chart for the Calendar Demo Program

View Tutorial          By: Narayanan V at 2010-01-06 18:57:25
3. View Comment

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
4. View Comment

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
5. View Comment

,./;'wat i searching for is that, the complete java program of 2010 calendar,.;'which shows the ff.
month,year,days and the weekssss!!!!! but its okei at this tym<>?;'l;'


View Tutorial          By: chor at 2010-03-28 23:09:37
6. View Comment

i want java code for day planner please send me on my email
my email id is sumaiyapathan18@yahoo.com


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

i want to the out put is
calander


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

i was after a user interactive calendar where they can see the calendar for the entire month so that they can input data into each day.. is this possible?

email me on: harry.woodward92@gmail.com


View Tutorial          By: Harry at 2011-11-17 09:01:07
9. View Comment

I discover unlimited calendar.this calendar use of Javascript.This calendar is worlds first calendar.9999999999999999 Years of calendar.answer only one click.

View Tutorial          By: Lalit Kumbhani at 2011-12-31 16:02:06
10. View Comment

Very nice demo. thanks! :)

View Tutorial          By: Nikhil Gala at 2012-01-13 10:03:57
11. View Comment

can i also get the variable description for this program??

View Tutorial          By: Maya Rao at 2013-10-04 06:06:00
12. View Comment

its good,,,can it be done more simpler without using arrays because i find it difficult.......using arrays
please send me the answer...........


View Tutorial          By: gahanaatherya at 2013-10-14 11:50:02
13. View Comment

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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Lakshmi
Exporting a Certificate Authenticating Your Public Key example using keytool in Java
while and do-while example in Java
Common SQL Commands
Data Access Technologies in Java
perror() Function - example program in C
Struts-GUI and Struts Console
Variable-length Argument Lists sample program in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Use ImageButtonBean for Image based Form Submits in Struts
break and continue loops in C
Arguments - Call by Value in C
Using command-line arguments in C++
How to handle Exceptions in C++
Gauge sample program in J2ME
What is Reflection? - getConstructors(), getFields(), and getMethods()

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
A Serialization Example in Java
Using One-Dimensional Arrays in Java
Method Overriding in Java
indexOf( ) and lastIndexOf( ) in Java
The java Buzzwords
XML and Java - Parsing XML using Java Tutorial
Student marks calculation program in Java
Handling Fractions in Java
Calculate average sale of the week in Java
Write to a file in Java - Sample Program
How to create an array and method in JSP
How to use set, get basic and nested properties for Spring framework
Converting a number into its equalant value in words in Java
What is Java?
Applet Notinited : Applet xxxxxxxxxxxx notinited
Most Emailed Articles (in Java)
Converting a number into its equalant value in words in Java
java.lang.reflect package
Disadvantages of using Native methods in Java
What is UCS? What is ISO 10646?
Generating Your Key Pair example using keytool in Java
Operator Precedence in Java
PushbackReader sample program in Java
concat(), replace(), and trim() Strings in Java
instanceof sample program in Java
Use of - new - in Java
The Benefits of OOP
Increment and Decrement Operator
Characters in java
The continuing Revolution of java
Why java is important to the Internet