Java Date-Time - W3points | w3schools | TutorialPoint | | w3school

Java Date-Time

Java offers the Data class in java.util package, this data class encapsulates the current time and date.

Data class supports two constructors Date() and date( Long millisec)

See the below table to know in detail:

Constructor & Description

  1. Date( )

    This type of constructor set the object with the current date and time.

  2. Date(long millisec)

    This constructor will accepts an argument that similar to the number of milliseconds that have elapsed since midnight, January 1, 1970.

Methods of Data Class:

Method & Description

  1. boolean after(Date date)

    Returns true if the invoking Date object carry a date that is not earlier than the one specified by date, so, it returns false.

  2. boolean before(Date date)

    Returns true only if the invoking Date object carry a date that is not later than the one specified by date, so it result in false.

  3. Object clone( )

    It Duplicates the invoking Date object.

  4. int compareTo(Date date)

    Do the comparison of the value of invoking object with that mentioned date. Returns 0 if the values are same identical. Gives a negative value if the invoking object is not later than than date. Gives A positive value if the invoking object is not earlier than the date.

  5. int compareTo(Object obj)

    Operates equally to compareTo(Date) if object is as class Date. If not than, it throws a ClassCastException.

  6. boolean equals(Object date)

    Returns true- if the invoking Date object have same time and date as the one mentioned by date, any other way, it returns false.

  7. long getTime( )

    Returns the number -of milliseconds that have elapsed from January 1, 1970.

  8. int hashCode( )

    Gives a hash code for the invoking object.

  9. void setTime(long time)

    Sets the date and time as set by time, which shows a pass time in milliseconds from midnight, January 1, 1970.

  10. String toString( )

    Changes the invoking Date object in a string and gives the result.

Getting Current time and Date:

This is extremely simple way to get current time and date in java. You can just use a date object with the help of tostring() this way to print the time and date as follow.


import java.util.Date;
public class DateDemo {

public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();

// display time and date using toString()

This will result in:


on May 04 09:51:52 CDT 2009