Java Date - java.util.Date Class

Date Class

The Date class represents information about dates and times.

Date Constructors

Constructor Description
Date() Default constructor. Creates an object with the current date and time.
Date(long date) Creates an object representing the time date milliseconds have elapsed from GMT time.

Descriptions of constructors declared deprecated are omitted.

Main Date Methods

Method Description
long getTime() Returns the time from January 1, 1970 to the current time in milliseconds.
void setTime(long time) Sets the current object’s date and time as milliseconds since 1970.
boolean before(Date when) Returns true if this date/time is before the given when date/time object; otherwise returns false.
boolean after(Date when) Returns true if this date/time is after the given when date/time object; otherwise returns false.

Descriptions of methods declared deprecated are omitted.

Many constructors and methods in the Date class have been declared deprecated. In most cases, using the Calendar class is recommended.

Date Example

package com.devkuma.basic.datetime;

import java.util.Date;

public class DateClass {

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

        Date old = new Date(0);
        System.out.println("old : " + old);

        System.out.println("before : " + old.before(now));
        System.out.println("after : " + old.after(now));
    }
}

Result:

Now : Mon Sep 05 11:33:33 KST 2022
old : Thu Jan 01 09:00:00 KST 1970
before : true
after : false