Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Calendar and Date: good to know facts and code examples

JavaFAQ Home » General Java Go to all tips in General Java

Bookmark and Share

Practical info about Java Calendar and Date

If you want to operate with different dates in your Java program you   will use Java Calendar class.

I will try to give you some overview of not widely known facts about Java Calendar and Date classes, working code examples, which you can try right away.

The basic information about Calendar class is provided by Java API. The Calendar class is about days, months and years. One could ask: is not Date class about the same? Not exactly...

What is difference between Java Date and Calendar classes?

The difference between Date and Calendar is that Date class operates with specific instant in time and Calendar operates with difference between two dates. The Calendar class gives you possibility for converting between a specific instant in time and a set of calendar fields such as HOUR, YEAR, MONTH, DAY_OF_MONTH. You can also manipulate with the calendar fields, for example getting the date of your grandmother birthday Smile.

I would like to point some things about Calendar and Date which you should know and which are not obvious...

Leap seconds.

Years, months, dates and hours are in "normal" range like:

  • A year  y - 1900.
  • A month from 0 to 11
  • A date (day of month) from 1 to 31 in the usual manner. calendar leap seconds
  • An hour 0 to 23.
  • A minute from 0 to 59 in the usual manner.

But, attention!! A second is represented by an integer from 0 to 61. Looks strange - 61 second, but do not forget about leap  second. About once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second.

Lenient fields.

Another funny feature is lenient and non-lenient fields in calendar. What is that? Example:

32 January 2006. Actually if you set your calendar lenient it will be 1 February 2006 and no problem for your program Smile. If it is non-lenient  ArrayIndexOutOfBoundsException exception will be thrown.

Another question is 00:00 end or beginning of day? Is 00:00 A.M. or P.M.? Are midnight and noon A.M. or P.M?

Answer: 23:59 is the last minute of the day and 00:00 is the first minute of the next day. Midnight belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm.

And probably last question: what is epoch? and why this Epoch since January 1, 1970 00:00:00.000 GMT.

Actually it is Unix time, or POSIX time, is a system for describing points in time: it is the number of seconds after 00:00:00 UTC, January 1, 1970.

Wait, one question more!

"If we use the time which is counted since Epoch, how can I know which years had leap seconds and which not?"

Answer: To make life easier leap seconds are not counted. Java Date class takes actual time from OS and most of modern computers can not use leap seconds, their's internal clocks are not so precised. That's why periodical time synchronization is required.

All Java code examples with Date class. (Calendar class examples follow below...)

Example Name
Date / Time Example
Calendar Example
Month View
Getting and Setting the Modification Time of a File or Directory
Formatting Output with PrintWriter (Logger1.java)
Connection Options (i.e. SYSDBA)
Query Example
Beep / Date Example
Java Statement Runner
Trivial class to show use of Date & Calendar objects
Show dates before 1970, in JDK1.0/1.1 when Date was broken
Show some date uses
Create SimpleDateFormats from a string read from a file
DateDiff -- compute the difference between two dates
Compare Dates
An Applet to display the current time
The Date we are about to format
Read a file of reminders, sleep until each is due, beep.
Read a file of reminders, run each when due using java.util.Timer
Beep every 5 minutes.
Request and/or provide time service, per RFC 868. RFC 868
Test interrupt and async close semantics of InterruptibleChannel objects.
Entity Resolver, with Chaining
Event Bean
Transparent Background
Time Server
Item MIDlet example
Chat Applet
Html Template class implements a simple HTML template.
Send Email
Time Client
Site Redirector
Request Processor
UDP Time Client
Time Server
Last 24 (retrieves file if it's modified since ...)
Daytime Server
Dayclient and Dayserver - implement the Unix 'daytime' protocol.
Try Date Format - enter a time value and a SimpleDateFormat specification.
Calendar Hack
Triangle Square Windows Corner Icon
Single Wave form Panel
Weather Panel
Waveform Display Simulator
MIDlet exampleS to invoke a servlet.
Four execution Midlets

All Java code Examples with Calendar class

Example Name
Calendar Example
Month View
Trivial class to show use of Date & Calendar objects
Show dates before 1970, in JDK1.0/1.1 when Date was broken
Print a month page. Only works for the Western calendar.
The Date we are about to format
Show some calendar calculations.
An Applet to display a "Cal" calendar component
Bean to display a month calendar in a JPanel
Show use of Calendar objects
Compute the day of the year that Easter falls on.
Show use of Calendar get() method with various parameters.
Time Server
Item MIDlet example
Time Client
Calendar Hack

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy