In this Java tutorial, you will learn how to access date and time from system, or from a given time-stamp string, with examples.

Date and Time in Java

Date and Time in Java – The time at some point in the middle of the program in Java could be captured or time from another source like time-stamp could be stored and processed using the class java.util.Date.

Example

In the following program, we print time, to be precise, the system time when executing the statement new Date().

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date.toString());
	}
}

Output

Mon Mar 27 00:04:46 IST 2017

Some of the methods offered by java.util.Date() class :

java.util.Date().toString()

As we have seen this method in the above program, DateTimeDemo.java, java.util.Date().toString() prints the time in the following format.

dow mon dd hh:mm:ss zzz yyyy
dow : Day Of Week
mon : Month
dd  : Day of Month
hh  : Hour of the day
mm  : Minute of the hour
ss  : Second of the minute
zzz : TimeZone
yyyy: Year

How to call java.util.Date().toString() in your program.

String dateTime = new java.util.Date().toString();
System.out.println(dateTime);

Output

Mon Mar 27 00:04:46 IST 2017
ADVERTISEMENT

Create date from milliseconds

This constructor of java.util.Date() class, creates a new Date() object and initializes it to the time “value milliseconds” after 00:00 GMT hours, Jan 1st 1970 or 05:30 IST hours, Jan 1st 1970.

Example

java.util.Date(4752) would initialize the object to 4752( = 4seconds + 752milliseconds) after 00:00 GMT hours, Jan 1st 1970 which is Thu Jan 01 00:00:04 IST 1970 or Thu Jan 01 05:30:04 IST 1970.

In the following program, we will call java.util.Date() with 4752 passed as argument.

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(4752L);
		System.out.println(date1.toString());
	}
}

Output

Thu Jan 01 05:30:04 IST 1970

Compare dates

This method compares if the date is after or before or equals the specified date.

  • Negative return value mean that the date is prior to the speicified date.
  • Positive return value mean that the speicified date is prior to the date.
  • Zero return value mean that the speicified date is same as that of the date, to milliseconds.

Example

In the following example, we have date1 and date2, and we shall try the below cases :

  1. date1.compareTo(date2);
  2. date1.compareTo(date2);
  3. date1.compareTo(date1);

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(1490554798133L);
		Date date2 = new Date(1490554855875L);
		
		System.out.println("date1 : "+date1.toString());
		System.out.println("date2 : "+date2.toString());
		
		System.out.println("\nIs date2 prior to date1 : "+date1.compareTo(date2));
		System.out.println("Negative value of date1.compareTo(date2) means the date2 is not prior to date1.");
		System.out.println("\nIs date1 prior to date2 : "+date2.compareTo(date1));
		System.out.println("Positive value of date1.compareTo(date2) means the date2 is prior to date1.");
		System.out.println("\nIs date1 same as date1 : "+date1.compareTo(new Date(date1.getTime())));
		System.out.println("Zero value of date1.compareTo(new Date(date1.getTime())) means the date1 has same time as that of date1, which is by the way evident in this case");
	}
}

Output

date1 : Mon Mar 27 00:29:58 IST 2017
date2 : Mon Mar 27 00:30:55 IST 2017

Is date2 prior to date1 : -1
Negative value of date1.compareTo(date2) means the date2 is not prior to date1.

Is date1 prior to date2 : 1
Positive value of date1.compareTo(date2) means the date2 is prior to date1.

Is date1 same as date1 : 0
Zero value of date1.compareTo(new Date(date1.getTime())) means the date1 has same time as that of date1, which is by the way evident in this case

Check if the date is after a specific date

This method checks if the Date is after the specified date. If yes, returns a true, else returns a false.

Example

The following program shows the working of method java.util.Date().after(Date date)

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(1490554798133L);
		Date date2 = new Date(1490554855875L);
		
		System.out.println("date1 : "+date1);
		System.out.println("date2 : "+date2);
		System.out.println();
		System.out.println("Is date1 after date2 ? "+date1.after(date2));
		System.out.println("Is date2 after date1 ? "+date2.after(date1));
	}
}

Output

date1 : Mon Mar 27 00:29:58 IST 2017
date2 : Mon Mar 27 00:30:55 IST 2017

Is date1 after date2 ? false
Is date2 after date1 ? true

Check if the date is before a specific date

This method checks if the Date is before the specified date. If yes, returns a true, else returns a false.

Example

The following program shows the working of method java.util.Date().before(Date date)

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(1490554798133L);
		Date date2 = new Date(1490554855875L);
		
		System.out.println("date1 : "+date1);
		System.out.println("date2 : "+date2);
		System.out.println();
		System.out.println("Is date1 before date2 ? "+date1.before(date2));
		System.out.println("Is date2 before date1 ? "+date2.before(date1));
	}
}

Output

date1 : Mon Mar 27 00:29:58 IST 2017
date2 : Mon Mar 27 00:30:55 IST 2017

Is date1 before date2 ? true
Is date2 before date1 ? false

Check if two dates are equal

Returns true if the date is same as the specified date, to milliseconds. Else the method returns false.

Example

Following is a sample program to show the working of java.util.Date().equals(Date date) method.

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(1490554798133L);
		Date date2 = new Date(1490554855875L);
		Date date3 = new Date(1490554798133L);
		
		System.out.println("Is date1 equals date2 ? "+date1.equals(date2));
		System.out.println("Is date1 equals date3 ? "+date1.equals(date3));
	}
}

Output

Is date1 equals date2 ? false
Is date1 equals date3 ? true

Clone date

The clone method creates a new object, an exact copy of the specified Date.

Example

In the following example, we shall clone date1 to create a new date2.

DateTimeDemo.java

import java.util.Date;

public class DateTimeDemo {

	public static void main(String[] args) {
		Date date1 = new Date(1490554798133L);
		Date date2 = (Date) date1.clone();
		System.out.println("Is date1 equals date2 ? "+date1.equals(date2));
	}
}

Output

Is date1 equals date2 ? true

Conclusion

Concluding this Java Tutorial, Date and Time in Java, we have seen how to create a java.util.Date() object, get the local time from it using toString() method, compare it to an other Date, check if the date is before or after a specified date, if two dates are equal in value.