In this tutorial, you will learn-

How to use Date in Java

Let us first understand the parameters that consist of a Date.

How to use Date in Java

It will primarily contain -

  • The year (in either 2 or 4 digits)
  • The month (in either 2 digits, First 3 letters of the month or the entire word of the month).
  •  The date (it will be the actual date of the month).
  • The day (the day at the given date – like Sun, Mon, Tue etc).

In terms of computer systems, there are quite a lot of parameters that can be used to associate with a date. We shall see them in the later parts of this topic.

Displaying a Date in Java

Now let us see how Java provide us the Date, first we shall see how to get the current date-

Java provides a Date class under the java.util package, The package provides several methods to play around with the date.

You can use the Date object by invoking the constructor of Date class as follows:

import java.util.Date; 
class Date_Ex1 {
  public static void main(String args[]) {
    // Instantiate a Date object by invoking its constructor
    Date objDate = new Date();
    // Display the Date & Time using toString()

Show the date in the specified format:

You all must have learnt the alphabets in your kindergarten ….

Let us now learn the ABC’s of the date format.

How to use Date in Java

Don’t worry, you don’t need to remember all of these, they can be referred anytime you need to format a particular date.

How Dates Formats can be used?

Java provides a class called a SimpleDateFormat that allows you to format and parse dates in the as per your requirements.

You can use the above characters to specify the format-

For example:
  1. Date format required: 2012.10.23 20:20:45 PST

The appropriate date format specified will be- yyyy.MM.dd HH:mm:ss zzz 2 .  

Date format required:09:30:00 AM 23-May-2012

The appropriate date format specified will be-hh:mm:ss a dd-MMM-yyyy

Tip: Be careful with the letter capitalization . If you mistake M with m , you will undesired results!

Lets learn this with a code example

import java.text.SimpleDateFormat;
import java.util.Date;
 class TestDates_Format {
   public static void main(String args[]) {
     Date objDate = new Date( ); // Current System Date and time is assigned to objDate
     String strDateFormat = "hh:mm:ss a dd-MMM-yyyy";//Date format is Specified
     SimpleDateFormat objSDF = new SimpleDateFormat (strDateFormat);//Date format string is passed as an argument to the Date format object
     System.out.println(objSDF.format(objDate));//Date formatting is applied to the current date

Comparison of Dates

The most useful method of comparing dates is by using the method – compareTo()

Let us take a look at the below code snippet-

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
class TestDates_Compare{
public static void main(String args[]) throws ParseException{
SimpleDateFormat objSDF = new SimpleDateFormat("dd-mm-yyyy");
Date dt_1 = objSDF.parse("20-08-1981");
Date dt_2 = objSDF.parse("12-10-2012");
System.out.println("Date1 : " + objSDF.format(dt_1));
System.out.println("Date2 : " + objSDF.format(dt_2));
if (dt_1.compareTo(dt_2)>0){
System.out.println("Date 1 occurs after Date 2");
}// compareTo method returns the value greater than 0 if this Date is after the Date argument.
else if (dt_1.compareTo(dt_2)<0){
System.out.println("Date 1 occurs before Date 2");
}// compareTo method returns the value less than 0 if this Date is before the Date argument;
else if (dt_1.compareTo(dt_2)==0){
System.out.println("Both are same dates");
}// compareTo method returns the value 0 if the argument Date is equal to the second Date;
System.out.println("You seem to be a time traveller !!");