Java current date - How to get current date in Java

This article explains how to get the current date in Java. There are many ways you can get the current date in Java.

Java current date - How to get current date in Java

java current date

In this section, you will learn about displaying current date and time in Java.

In Java, current date time can be displayed using two Java classes : Date and Calender. Later, you can convert this current date into user friendly format or according to your need.

Current date using Date class

You can get the current date using java.util.Date class as follows :

import java.util.Date;

public class CurrentDateTime {
	public static void main(String[] args) {
		//get current date time using java.util.Date class
		Date date = new Date();
		System.out.println(date);
	}
}

The output of the following code is given below :

Mon Jan 28 10:38:31 GMT+05:30 2013

Current date using Calendar class

You can get the current date using java.util.Calendar class as follows :

import java.util.Calendar;

public class CurrentDateTime {
	public static void main(String[] args) {
	
	// get current date time with jav.util.Calendar class
	Calendar cal = Calendar.getInstance();
	System.out.println(cal.getTime());
	
	}
}

Formatting Date using  SimpleDateFormat class

You can format java.util.Date as follows :

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

public class CurrentDateTime {
	public static void main(String args[]){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		//get current date time with java.util.Date
		Date date = new Date();
		
		//formatting date time
		System.out.println(dateFormat.format(date));
	}
}

java.util.Calendar can be formatted as follows :


import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CurrentDateTime {
	public static void main(String args[]){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		//get current date time using java.util.Calender
		Calendar cal = Calendar.getInstance();
		
		//formatting date time
		System.out.println(dateFormat.format(cal.getTime()));
	}
}

Output in both the cases will be :

2013/01/28 11:16:24

Format String Date into another format

You can format string date into another format in two ways, given one by one:

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

public class FormatDateTime {
	public static void main(String args[]) throws ParseException{
		DateFormat outputFormat = new SimpleDateFormat("MM/yyyy");
		DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String inputText = "2013-01-28 11:16:24";
		Date date = inputFormat.parse(inputText);
		String outputText = outputFormat.format(date);
		System.out.println(outputText);
	}
}

Output : 01/2013

Another example is :

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FormatDateTime {
	public static void main(String args[]) throws ParseException{
	
		String date_time="2013-01-28 11:16:24";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateFormat.parse(date_time));
		
		DateFormat newdateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		System.out.println("Formatted Date is :"+newdateFormat.format(cal.getTime()));
	}
}

Output : Formatted Date is :28/01/2013 11:16:24

Add one day or Subtract one day to Date

You can modify the date by adding one day as follows :

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class AddDateTime {
	public static void main(String args[]) throws ParseException{
	
		String date_time="2013-01-28 11:16:24";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateFormat.parse(date_time));
		System.out.println("Current Date is :"+date_time);
		
		cal.add(Calendar.DATE, 1);
		date_time = dateFormat.format(cal.getTime());
		
		System.out.println("Modified Date is :"+date_time);
	}
}

Output :

Current Date is :2013-01-28 11:16:24
Modified Date is :2013-01-29 11:16:24

Similarly, you can add 1 hour as follows :

cal.add(Calendar.HOUR, 1);

Also, 1 minute can be added as follows :

cal.add(Calendar.MINUTE, 1);

1 second can be added as follows :

cal.add(Calendar.SECOND, 1);

Similarly, you can subtract one day using the same add() method as follows :

cal.add(Calendar.DATE, -1);

Also, you can subtract 1 hour as follows :

cal.add(Calendar.HOUR, -1);

1 minute can be subtracted as follows :

cal.add(Calendar.MINUTE, -1);

1 second can be subtracted as follows :

cal.add(Calendar.SECOND, -1);

Compare two Dates

You can compare two dates in variety of ways, some are given below :

(1) using before() method

Using before() method, you can compare two dates by testing whether this date is before specified date or not. This method returns true/false.

Given below a sample example :

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


public class BeforeMethod {
	public static void main(String args[]) throws ParseException{
		String first_time="2013-01-28 11:16:24";
		String second_time="2013-01-29 11:16:24";
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date first_date=dateFormat.parse(first_time);
		Date second_date=dateFormat.parse(second_time);
		if(second_date.before(first_date)){
		System.out.println("The second date is before first date");
		}else{
		System.out.println("The first date is before second date");
		}
	}
}

Output: The first date is before second date.

(2) using after() method

Using after() method, you can compare two dates by testing whether this date is after specified date or not. This method returns true/false.

Given below sample code :

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


public class AfterMethod {
	public static void main(String args[]) throws ParseException{
		String first_time="2013-01-28 11:16:24";
		String second_time="2013-01-29 11:16:24";
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date first_date=dateFormat.parse(first_time);
		Date second_date=dateFormat.parse(second_time);
		if(second_date.after(first_date)){
		System.out.println("The second date is after first date");
		}else{
		System.out.println("The first date is after second date");
		}
		
	}
}

Output: The second date is after first date.

(3) using equals() method

This method is used to compare two dates for equality. This method returns true if the two dates are equal upto the millisecond , else return false.

Given below sample code :

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


public class EqualMethod {
	public static void main(String args[]) throws ParseException{
		String first_time="2013-01-28 11:16:24";
		String second_time="2013-01-28 11:16:24";
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date first_date=dateFormat.parse(first_time);
		Date second_date=dateFormat.parse(second_time);
		if(second_date.equals(first_date)){
		System.out.println("The two dates are equal");
		}else{
		System.out.println("The two dates are not equal");
		}
		
	}
}

Output : The two dates are equal

(4) compareTo() method

This method returns 0 if the two dates are equal, returns value less than 0 if this Date is before the argument Date , returns value greater than 0 if this Date is after the argument Date.

Given below sample code :

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


public class compareToMethod{
	public static void main(String args[]) throws ParseException{
		String first_time="2013-01-28 11:16:24";
		String second_time="2013-01-29 11:16:24";
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date first_date=dateFormat.parse(first_time);
		Date second_date=dateFormat.parse(second_time);
		
		int compare=second_date.compareTo(first_date);
		if(compare==0){
		System.out.println("The two dates are equal");
		}else if (compare<0) {
		System.out.println("This Date is before the argument Date ");
		}else if(compare>0){
		System.out.println("This Date is after the argument Date");
		}
	
	}
}