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...

Calendar Example Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!

List of Java Calendar tools/programs/applets.

First I list Java Calendar tools/programs/applets. Then you can find code example as well.

Calendar Component v5.0 - Professional Java ™ Technology Date Picker

This is a Java ™ Technology Calendar Component ready to use in Swing applications.
Either used as a simple date picker or as a professional calendar component, Calendar Component v5.1 Professional has all the features required to quickly integrate date selection in your application.

tCaldate Calendar Applet. This size is very small (7.9 Ko). tCalDate run on all browser with support JDK 1.02. All source available with a registration. (free use for home page).

Java Calendar Tools is a project to provide Java-based tools and components that will leverage industry standard calendar protocols (iCalendar in particular). There seems to be a lack of open source tools for using these protocols, and it slows adoption of the protocols. For example, the CAP protocol does not seem to be supported by and calendar applications.



 // -----------------------------------------------------------------------------
// CalendarExample.java
// -----------------------------------------------------------------------------

 * =============================================================================
 * Copyright (c) 1998-2005 Jeffrey M. Hunter. All rights reserved.
 * All source code and material located at the Internet address of
 * http://www.idevelopment.info is the copyright of Jeffrey M. Hunter, 2005 and
 * is protected under copyright laws of the United States. This source code may
 * not be hosted on any other site without my express, prior, written
 * permission. Application to host any of the material elsewhere can be made by
 * contacting me at jhunter@idevelopment.info.
 * I have made every effort and taken great care in making sure that the source
 * code and other content included on my web site is technically accurate, but I
 * disclaim any and all responsibility for any loss, damage or destruction of
 * data or any other property which may arise from relying on it. I will in no
 * case be liable for any monetary damages arising from such loss, damage or
 * destruction.
 * As with any code, ensure to test this code in a development environment
 * before attempting to run it in production.
 * =============================================================================

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;

 * -----------------------------------------------------------------------------
 * Used to provide an example that exercises most of the functionality of the
 * java.util.Calendar class.
 * @version 1.0
 * @author  Jeffrey M. Hunter  (jhunter@idevelopment.info)
 * @author  http://www.idevelopment.info
 * -----------------------------------------------------------------------------

public class CalendarExample {

     * Helper utility used to print a String to STDOUT.
     * @param s String that will be printed to STDOUT.
    private static void prt(String s) {

    private static void prt() {

     * Calendar's getTime() method returns a Date object. This can then be
     * passed to println() to print today's date (and time) in the traditional
     * (but non-localized) format.
    private static void doCalendarTimeExample() {
        prt("CURRENT DATE/TIME");
        Date now = Calendar.getInstance().getTime();
        prt("  Calendar.getInstance().getTime() : " + now);

     * Simple Date Format from java.text package.
    private static void doSimpleDateFormat() {
        prt("SIMPLE DATE FORMAT");

        // Get today's date
        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        prt("  It is now : " + formatter.format(now.getTime()));

     * Date Arithmetic function. Adds the specified (signed) amount of time to
     * the given time field, based on the calendar's rules.
     * The following examle:
     *   - Subtracts 2 years from the current time of the calendar
     *   - Adds 5 days from the current time of the calendar
    private static void doAdd() {

        prt("ADD / SUBTRACT CALENDAR / DATEs");

        // Get today's date
        Calendar now = Calendar.getInstance();
        Calendar working;
        SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

        working = (Calendar) now.clone();
        working.add(Calendar.DAY_OF_YEAR, - (365 * 2));
        prt("  Two years ago it was: " + formatter.format(working.getTime()));

        working = (Calendar) now.clone();
        working.add(Calendar.DAY_OF_YEAR, + 5);
        prt("  In five days it will be: " + formatter.format(working.getTime()));



    private static void doDateDifference() {
        Date startDate1 = new GregorianCalendar(1994, 02, 14, 14, 00).getTime();
        Date endDate1   = new Date();;

        long diff = endDate1.getTime() - startDate1.getTime();

        prt("  Difference between " + endDate1);
        prt("  and " + startDate1 + " is " + (diff / (1000L*60L*60L*24L)) + " days.");


    private static void doGetMethods() {

        prt("CALENDAR GET METHODS");
        Calendar c = Calendar.getInstance();

        prt("  YEAR                 : " + c.get(Calendar.YEAR));
        prt("  MONTH                : " + c.get(Calendar.MONTH));
        prt("  DAY_OF_MONTH         : " + c.get(Calendar.DAY_OF_MONTH));
        prt("  DAY_OF_WEEK          : " + c.get(Calendar.DAY_OF_WEEK));
        prt("  DAY_OF_YEAR          : " + c.get(Calendar.DAY_OF_YEAR));
        prt("  WEEK_OF_YEAR         : " + c.get(Calendar.WEEK_OF_YEAR));
        prt("  WEEK_OF_MONTH        : " + c.get(Calendar.WEEK_OF_MONTH));
        prt("  DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        prt("  HOUR                 : " + c.get(Calendar.HOUR));
        prt("  AM_PM                : " + c.get(Calendar.AM_PM));
        prt("  HOUR_OF_DAY (24-hour): " + c.get(Calendar.HOUR_OF_DAY));
        prt("  MINUTE               : " + c.get(Calendar.MINUTE));
        prt("  SECOND               : " + c.get(Calendar.SECOND));


    public static void main(String[] args) {




The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.






[ Go Back ]

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