Java - Legacy Date Time Classes

Introduction

The main legacy classes are Date, Calendar, and GregorianCalendar which are in the java.util package.

Date Class

Date class represents an instant in time.

A Date object stores the number of milliseconds elapsed since the epoch, midnight January 1, 1970 UTC.

The default constructor of the Date class is used to create a Date object with the current system datetime.

The following code shows how to use Date class.

Demo

import java.util.Date;

public class Main {
  public static void main(String[] args) {
    // Create a new Date object
    Date currentDate = new Date();
    System.out.println("Current date: " + currentDate);

    // Get the milliseconds value of the current date
    long millis = currentDate.getTime();
    System.out.println("Current datetime in millis: " + millis);
  }/*from   ww  w .  j  a  v a 2 s  . co  m*/
}

Result

Date object works with a 1900-based year.

Calendar Class

Calendar is an abstract class. The GregorianCalendar class is a concrete class, which inherits the Calendar class.

Calendar class declares some final static fields to represent date fields.

Calendar.JANUARY can be used to specify the January month in a date.

GregorianCalendar class has a default constructor, which create an object to represent the current datetime.

Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
  public static void main(String[] args) {

    // Get the current date in the system default time zone
    GregorianCalendar currentDate = new GregorianCalendar();

    // Get GregorianCalendar object representing March 26, 2018 06:30:45 AM
    GregorianCalendar someDate = new GregorianCalendar(2018, Calendar.MARCH,
        26, 6, 30, 45);/*  w w  w.  j ava 2 s .c  o m*/

    // Get Indian time zone, which is GMT+05:30
    TimeZone indianTZ = TimeZone.getTimeZone("GMT+05:30");

    // Get current date in India
    GregorianCalendar indianDate = new GregorianCalendar(indianTZ);

    // Get Moscow time zone, which is GMT+03:00
    TimeZone moscowTZ = TimeZone.getTimeZone("GMT+03:00");

    // Get current date in Moscow
    GregorianCalendar moscowDate = new GregorianCalendar(moscowTZ);
  }
}

The month part of a date ranges from 0 to 11. That is, January is 0, February is 1, and so on.

You should use Calendar.JANUARY constant to represent the January month in your program instead of a 0.

Demo

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
  public static void main(String[] args) {
    // Create a GregorianCalendar object
    GregorianCalendar gc = new GregorianCalendar();

    // year will contain the current year value
    int year = gc.get(Calendar.YEAR);

    // month will contain the current month value
    int month = gc.get(Calendar.MONTH);

    // day will contain day of month of the current date
    int day = gc.get(Calendar.DAY_OF_MONTH);

    // hour will contain hour value
    int hour = gc.get(Calendar.HOUR);

    // minute will contain minute value
    int minute = gc.get(Calendar.MINUTE);

    // second will contain second values
    int second = gc.get(Calendar.SECOND);
  }//  w w  w.  ja  v  a 2  s  . co  m
}

The following code illustrates the use of some of the methods of the GregorianCalendar class.

Demo

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    System.out.println("Current Date: " + getStr(gc));

    // Add 1 year
    gc.add(Calendar.YEAR, 1);/*from ww  w.j ava  2 s  .c o m*/
    System.out.println("After adding a year: " + getStr(gc));

    // Add 15 days
    gc.add(Calendar.DATE, 15);
    System.out.println("After adding 15 days: " + getStr(gc));

    long millis = gc.getTimeInMillis();
    Date dt = gc.getTime();
    System.out.println("Time in millis: " + millis);
    System.out.println("Time as Date: " + dt);
  }

  public static String getStr(GregorianCalendar gc) {
    int day = gc.get(Calendar.DAY_OF_MONTH);
    int month = gc.get(Calendar.MONTH);
    int year = gc.get(Calendar.YEAR);
    int hour = gc.get(Calendar.HOUR);
    int minute = gc.get(Calendar.MINUTE);
    int second = gc.get(Calendar.SECOND);

    String str = day + "/" + (month + 1) + "/" + year + " " + hour + ":"
        + minute + ":" + second;
    return str;
  }
}

Result

Related Topics

Exercise