Java Date Time - Java Legacy Date Calendar








An object of the Date class stores the number of milliseconds elapsed since the epoch, midnight January 1, 1970 UTC. and represents an instant in time.

The Date class default constructor creates a Date object with the current system datetime.

Example

The following code shows how to use Date class.

import java.util.Date;
/*from   ww w.  ja  v  a  2 s  . co  m*/
public class Main {

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

    long millis = currentDate.getTime();
    System.out.println("Current  datetime  in millis: " + millis);

  }

}

The code above generates the following result.





Legacy Calendar

Calendar is an abstract class. GregorianCalendar class extends Calendar class.

Constructor

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

GregorianCalendar class also defines constructors we can use to create a specific date.

We can also create date in a particular time zone.

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/*  w w  w.  j  av a  2s .  co  m*/
public class Main {
  public static void main(String[] args) {
    // Get the current date in the system default time zone
    GregorianCalendar currentDate = new GregorianCalendar();
    System.out.println(currentDate.getTime());
    // Get GregorianCalendar object representing March 21, 2014 07:30:45 AM
    GregorianCalendar someDate = new GregorianCalendar(2014, Calendar.MARCH,
        21, 7, 30, 45);
    System.out.println(someDate.getTime());
    // Get Indian time zone, which is GMT+05:30
    TimeZone indianTZ = TimeZone.getTimeZone("GMT+05:30");
    GregorianCalendar indianDate = new GregorianCalendar(indianTZ);
    System.out.println(indianDate.getTime());
  }

}

The code above generates the following result.





Field

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

get() with requested field returns the value of a field in a datetime.

import java.util.Calendar;
import java.util.GregorianCalendar;
// w  ww  .  j  a v a  2  s  .  c  o m
public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    // current year value
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);
    // current month value
    int month = gc.get(Calendar.MONTH);
    System.out.println(month);
    // day of month
    int day = gc.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);
    // hour value
    int hour = gc.get(Calendar.HOUR);
    System.out.println(hour);
    // minute value
    int minute = gc.get(Calendar.MINUTE);
    System.out.println(minute);
    // second values
    int second = gc.get(Calendar.SECOND);
    System.out.println(second);
  }

}

The code above generates the following result.

add()

add() adds an value to a date. The amount may be negative or positive. The Calendar knows how to adjust.

import java.util.Calendar;
import java.util.GregorianCalendar;
/*from  w w w  .  ja v a 2s.  com*/
public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc  = new GregorianCalendar(2014, Calendar.DECEMBER,  1);
    gc.add(Calendar.MONTH,  5);
    System.out.println(gc.getTime());
  }
}

The code above generates the following result.

roll()

roll() adds a amount to the specified calendar field without changing larger fields.

It is an overloaded method.

void  roll(int field, int amount)
void  roll(int field, boolean up)

Suppose we have a GregorianCalendar set to August 31, 1999. Calling roll(Calendar.MONTH, 8) sets the calendar to April 30, 1999. DAY_OF_MONTH field cannot be 31 in the month April. DAY_OF_MONTH is set to the closest possible value, 30. The YEAR field maintains the value of 1999 because it is a larger field than MONTH.

roll(Calendar.MONTH, 1) is the same as roll(Calendar. MONTH, true).

roll(Calendar.MONTH, -1) is the same as roll(Calendar.MONTH, false).

import java.util.Calendar;
import java.util.GregorianCalendar;
/*from w  ww .  ja  v  a  2s.  com*/
public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    System.out.println("Current  Date: " + gc.getTime());
    // Add 1 year 
    gc.add(Calendar.YEAR, 1);
    System.out.println(gc.getTime());

    // Add 15 days 
    gc.add(Calendar.DATE, 15);
    System.out.println(gc.getTime());
  }
}

The code above generates the following result.