Date Time in Java

Date Class

Java provides Date class for this purpose which actually encapsulates the date and time in Java. This Date class is available in java.util.* package.

Print a Date in Console

Date class provides us two ways to declare a date in Java. Good to say that there are two constructors using which we can display date in Java.

import java.util.Date;


public class DateDemo {

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

}

the output for the above code is :

Sun Feb 01 20:54:34 CET 2015
Tue Jan 06 12:32:12 CET 1970


In the above code fragment we have used those 2 ways to create a date and we have displayed them using System.out.println.

new Date() ? this constructor creates an object instance of the Date class which holds the current date and time of your local machine.

new Date(473532239) ? this constructor accepts long value as a parameter and converts that log value to the corresponding date and time.

The long argument it takes is milliseconds which is passed since the midnight of 1st January, 1970.

Please note, as the current time of my machine is Sun Feb 01 20:54:34 CET, the first object (new Date()) displays the above mentioned by.

This time will definitely differ when you will run it. On the contrary, as the second argument takes the millisecond as an argument, the second output will be same always.

Methods in Date Class

  • after(Date date) & before(Date date)

Both of these above mentioned methods are used to compare two dates. Both these methods return Boolean true or false depending on the result.

Example:

public class DateDemo {

	public static void main(String[] args) {
		Date currentDate = new Date();
		Date newDate = new Date(76975485);
		System.out.println("Current time :: "+ currentDate);
		System.out.println("New time :: "+ newDate);
		System.out.println(currentDate.after(newDate));
		System.out.println(currentDate.before(newDate));
	}

}

The output of this code is :

Current time :: Sun Feb 01 23:29:45 CET 2015
New time :: Thu Jan 01 22:22:55 CET 1970
true
false

  • clone()

This method creates clone (exact same)  object of an existing object of Date class.

Example:

public class DateDemo {

	public static void main(String[] args) {
		Date currentDate = new Date();
                System.out.println(currentDate.equals((Date) 
                currentDate.clone()));

        }
}

The output will be true as we are comparing an object to its clone object.

  • int compareTo(Date date)

Well, this method is an interesting one! This method is also used to compare one date with another.

But, unlike after() or before() methods, this method can return any one of the following outputs for this code:
 
a.compareTo(b) where both a and b are objects of Date class.

1.    If ‘a’ is later than b, then return 1.
2.    If ‘a’ is equals to b, then return 0.
3.    If ‘a’ is earlier than b, return -1.

public class DateDemo {

	public static void main(String[] args) {
		Date dateA = new Date();
		Date dateB = new Date(865844);
		Date dateC = new Date(865844);
		
		//comparison
		System.out.println(dateA.compareTo(dateB));
		System.out.println(dateB.compareTo(dateC));
		System.out.println(dateC.compareTo(dateA));
	}
}
  • equals(Date date)

This method just compares two dates and return Boolean results, i.e. true if they are same and false, if they are not.

public class DateDemo {

	public static void main(String[] args) {
		Date dateA = new Date();
		Date dateB = new Date(865844);
		
		//comparison
		System.out.println(dateA.equals(dateB));
		
	}
}

As they dates are not same, the output of the program is false.

  • getTime()

This method returns the corresponding milliseconds for a date.

Example:

public class DateDemo {

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

The output of this code is : 865844

•    setTime()

This method helps us to explicitly set a time to a date.

Example:

public class DateDemo {

	public static void main(String[] args) {
		Date dateB = new Date(865844);
		System.out.println(dateB);
		
		// set the new value to the date
		dateB.setTime(9999999);
		System.out.println(dateB);
	}
}

The output will be :

Thu Jan 01 01:14:25 CET 1970
Thu Jan 01 03:46:39 CET 1970

  • toString()

This simple method just converts the date into a String.

Example:

String b = dateB.toString();

Formatting Dates

We all know that there are different ways to display dates. SimpleDateFormat is the class in Java which helps us to do that. A simple example for SimpleDateFormat:

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDemo {

	public static void main(String[] args) {
		Date dateA = new Date();
		System.out.println(dateA);
SimpleDateFormat format = new SimpleDateFormat("'Time 'hh:mm:ss '| Date 'E dd.MM.yyyy");
		System.out.println(format.format(dateA));
	}
}

The output for this program is:

Mon Feb 02 10:28:46 CET 2015
Time 10:28:46 | Date Mon 02.02.2015

Now, what we passed as an argument in new SimpleDateFormat() method provides the output for this code.

Now, is this the only way we can edit the format of the date?

NO!

Here is the list of some important formatting styles in a table we can use to format a date.


Next > < Prev
Scroll to Top