Home

Date and Time Values in Java Applications

  

Introduction

A date is a measure of non-spatial units that have elapsed in a set period. To perform this measure, a certain point is set and called midnight. From that point, a number of units are counted until the next corresponding point is reached. Because the light in daily life changes from one midnight unit to the next midnight unit, the sub-point when the light shows up, or starts, is called sunrise.

The sub-point when the light diminishes or starts disappearing is called sunset. The midpoint between the sunrise and the sunset is called noon. The first part, from midnight to noon is referred to as AM in US English. The other part is referred to as PM in US English.

The group of units between two midnight points is called a day. The days are counted from 0, 1, and up. A group of seven consecutive days is called a week. The weeks are counted from 1 and up. To make it easy to identify a day, the days of a week are named in US English as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. These are referred to as long names. The corresponding short names in US English are Mon, Tue, Wed, Thu, Fri, Sat, and Sun. The day considered as the first of the week depends on the language and/or some other considerations. For example, in US English, Sunday is usually considered the first day of the week.

Most of the time, a group of four weeks is called a month. The months are counted from 1 and up. To make it easy to identify a month, each holds a long name. In US English, they are January, February, March, April, May, June, July, August, September, October, November, and December. Their short names in US English are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. Except for the month of February that depends on a factor named Leap Year, the maximum days of each month are January=31, March=31, April=30, May=31, June=30, July=31, August=31, September=30, October=31, November=30, and December=31.

A group of three months is called a trimester. A group of four months is called a quarter. A group of six months is called a semester. A group of twelve months can be called a year. The years are counted from 1 and up (some compilers or applications specify when the years start; for example, some applications start their count at 1901 and cannot process years below that number; some other applications would not process a year beyond a certain year).

In a regular year, the months are counted from 1 to 12. The technique or expression used to identify a particular day during a year is called a date. The days are counted from 1 to 365 or 366 (depending on a factor called a leap year). A date can be the combination of a day, its month, and the year. There are other aspects that can be taken into consideration.

A unit of measure in a day is called a second. The seconds are counted from 0 to 59 or from 0, 1, 2, and so on. In some applications, when precision is particularly important, a second is considered a group of 1000 portions called milliseconds. The milliseconds are counted from 0 to 999.

A group of 60 seconds is called a minute. A group of 60 minutes is called an hour. A group of 24 hours is called a day. The technique or expression used to identify a particular unit of measure during a day is called the time. It can be the combination of the hour, the minute, and the second. Some other aspects may be taken into consideration.

Date Time Value Creation

To represent the dates, computers, applications, and compilers are configured with specific techniques and rules. To support date and time values, the java.util package is equipped with a class named Date. To assist you with initializing the variable, the Date class is equipped with various constructors.

The default constructor allows you to create a date or a time object without specifying its details. This would be done as follows:

import java.util.Date;

public class Exercise {
    public static void main(String[] args) {
	Date tm = new Date();
    }
}

After declaring a Date variable and initializing it, it holds all necessary pieces of information about its date and its time values.

The Current Date and Time

The default constructor of the Date class initializes the Date variable with today's date and time (at the time the variable was declared). This is illustrated in the following program:

import java.util.Date;

public class Exercise {
    public static void main(String[] args) {
	Date tm = new Date();

        System.out.println("Default Date and Time: " + tm);
    }
}

Based on the day and time this lesson was written, it produced:

Default Date and Time: Tue Jan 06 11:12:38 EST 2009
 
 
 
 
 

Previous Copyright © 2009-2012, FunctionX, Inc. Next