class Date Methods


class Date Methods

Summary: By the end of this tutorial "class Date Methods", you will come to know how to use Java Date class methods.

Introduction to class Date Methods

java.util.Date class constructor is used to print the system date. It comes with many methods to manipulate the date. But these methods are deprecated in favor of java.util.Calendar class introduced in JDK 1.1. Calendar class supports DateFormat class needed for internalization.

To use Date class, following concepts are to be remembered.

  1. Add 1900 (known as epoch year) to the year returned by the Date class to get the correct year. That is, for 2011, the date returns 111.
  2. Months are numbered to 0 to 11. That is 0, returned by the Date class, should be learnt as January.
  3. Month dates are numbered 1 to 31.
  4. Hours are given as 0 to 23. The early morning 1.00 AM is returned as 0.0 hour.
  5. Minutes are numbered as 0 to 59.
  6. Seconds are printed as 0 to 60 (if 61 is printed, think it is a leap second).

Following is the class signature as defined in the java.util package.

public class Date extends Object implements Serializable, Cloneable, Comparable

Following program explains a few class Date Methods.

import java.util.Date;
public class DateInfo
{
  public static void main(String args[])
  {
    Date today = new Date();
    System.out.println("Today Particulars: " + today);
    System.out.println("Hours Part of today: " + today.getHours());
    System.out.println("Minutes part of today: " + today.getMinutes());
    System.out.println("Seconds part of today: " + today.getSeconds());  			
    System.out.println("Month part of today: " + today.getMonth());
    System.out.println("Date part of today: "  + today.getDate());
    System.out.println("Day part of today: " + today.getDay());
    System.out.println("Year part of today: " + today.getYear());
    System.out.println("Milliseconds representation of today (from epoch time 01-01-1900: " + today.getTime());
  }
}

class Date Methods

The month is printed as 0; it is January (0+1). The day 3 means, Wednesday. The year 111 means 2011.

Observe the compilation warning message in the screenshot. It is a deprecation warning and an advice not use Date class and use Calendar class.

Leave a Comment

Your email address will not be published.