Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Calendar

Problem :

How can I  get year, month, day, day of week, time zone etc. from system date or Calendar?

Solution :

There is a class java.util.Calendar that has all date related methods.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestCalendar.java  and copy below contents.

import java.util.*;

/*
 * A program that displays Calendar important methods
*/

public class TestCalendar {
   
    public static void main(String[] a) {
   
        //Display Current Time
        showDate();
       
        //Display day, month, year etc from a Calendar
        showCalendar();
       
        //Display current Time zone
        showTimeZone();
    }

    public static void showDate() {

        System.out.println("**************** Now Date **************");

        Date now = new Date(); // current Date and Time
        System.out.println("Date is " + now.toString());

        long t = now.getTime(); // Time in milliseconds
        System.out.println("Time since 01-Jan-1970 00:00:00 GMT: " + t
                + " milliseconds.");
    }

    public static void showCalendar() {

        Calendar c = new GregorianCalendar();
        // the current time represented in the Gregorian calendar
        // in local time zone and daylight saving adjustments

        System.out.println("\n****************Calendar **************");

        System.out.println("Year: " + c.get(Calendar.YEAR));
        System.out.println("Month: " + c.get(Calendar.MONTH));
        System.out.println("Date: " + c.get(Calendar.DATE));
        System.out.println("Day of year: " + c.get(Calendar.DAY_OF_YEAR));
        System.out.println("Day of week: " + c.get(Calendar.DAY_OF_WEEK));
        System.out.println("AM or PM: " + c.get(Calendar.AM_PM));
        System.out.println("Hour: " + c.get(Calendar.HOUR));
        System.out.println("Hour of day: " + c.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute: " + c.get(Calendar.MINUTE));
        System.out.println("Second: " + c.get(Calendar.SECOND));
        System.out.println("Millisecond: " + c.get(Calendar.MILLISECOND));
        System.out.println("Zone offset: " + c.get(Calendar.ZONE_OFFSET)
                / (60 * 60 * 1000));
        System.out.println("Daylight saving offset: "
                + c.get(Calendar.DST_OFFSET) / (60 * 60 * 1000));
    }

    public static void showTimeZone() {

        Calendar c = new GregorianCalendar();
        TimeZone tz = c.getTimeZone();

        System.out.println("\n**************** Time Zone **************");

        System.out.println("My time: " + c.get(Calendar.HOUR_OF_DAY) + ":"
                + c.get(Calendar.MINUTE));
        System.out.println("My time zone ID: " + tz.getID());

        // Change timezone to America/Los_Angeles
        tz = TimeZone.getTimeZone("America/Los_Angeles");
        c.setTimeZone(tz);

        System.out.println("Los Angeles time: " + c.get(Calendar.HOUR_OF_DAY)
                + ":" + c.get(Calendar.MINUTE));
    }
}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestCalendar.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestCalendar.java with help of 
    • javac TestCalendar.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of
    • java TestCalendar command.

Output

**************** Now Date **************
Date is Thu Sep 10 14:59:14 IST 2009
Time since 01-Jan-1970 00:00:00 GMT: 1252574954250 milliseconds.

****************Calendar **************
Year: 2009
Month: 8
Date: 10
Day of year: 253
Day of week: 5
AM or PM: 1
Hour: 2
Hour of day: 14
Minute: 59
Second: 14
Millisecond: 281
Zone offset: 5
Daylight saving offset: 0

**************** Time Zone **************
My time: 14:59
My time zone ID: Asia/Calcutta
Los Angeles time: 2:29

FAQ

What is java.util.Calendar class?

The Calendar class, java.util.Calendar, is an abstract base class, providing a foundation for subclasses to represent a specific instance in time, in a specific calendar system.

What is java.util.GregorianCalendar class?

The GregorianCalendar class, java.util.GregorianCalendar, is a concrete subclass of the Calendar class, representing a specific instance in time in Gregorian Calendar system with time zone and daylight saving adjustments. This abstract base class and concrete subclass structure is very useful for future implementations of other calendar systems, like the Chinese lunar calendar system.

What is java.util.TimeZone class?

The TimeZone class, java.util.TimeZone, is an abstract base class, providing a foundation for subclasses to represent a particular time zone system. The SimpleTimeZone class, java.util.SimpleTimeZone, is a concrete subclass of the TimeZone class, representing a time zone for use with the GregorianCalendar class.


<<Previous | Next>>
ċ
TestCalendar.java
(3k)
Unknown user,
Sep 10, 2009, 2:40 AM
Comments