Java 8 – How to get current Date in different ways ?

In this article, we will learn how to get current Date in different ways

Get Current Date :

  1. Using Date object
  2. Using Calendar instance

1. Using Date object :

CurrentDateTimeInJavaUsingDate.java

package in.bench.resources.java8.current.date.time;

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

public class CurrentDateTimeInJavaUsingDate {

	public static void main(String[] args) {

		// 1. current date
		Date date = new Date();
		System.out.println("Current Date/Time"
				+ " using Date object :- \n" + date);


		// 1.1 format
		DateFormat dateFormat =  new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String str = dateFormat.format(date);
		System.out.println("\nFormatted Date/Time in dd-MM-yyyy HH:mm:ss format"
				+ " using Date() :- \n" + str);



		// 2. current date
		Date date2 = new Date(System.currentTimeMillis());
		System.out.println("\n\nCurrent Date/Time"
				+ " using Date(System.currentTimeMillis()) :- \n" + date2);


		// 2.1 format
		DateFormat dateFormat2 =  new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		str = dateFormat2.format(date2);
		System.out.print("\nFormatted Date/Time in dd.MM.yyyy HH:mm:ss format"
				+ " using Date(System.currentTimeMillis()) :- \n" + str);
	}
}

Output:

Current Date/Time using Date object :- 
Mon Sep 12 16:02:22 IST 2022

Formatted Date/Time in dd-MM-yyyy HH:mm:ss format using Date() :- 
12-09-2022 16:02:22


Current Date/Time using Date(System.currentTimeMillis()) :- 
Mon Sep 12 16:02:22 IST 2022

Formatted Date/Time in dd.MM.yyyy HH:mm:ss format using Date(System.currentTimeMillis()) :- 
12.09.2022 16:02:22

2. Using Calendar instance :

  • Calendar.getInstance() method returns Calendar instance which has many useful methods and one such method is getTime() which returns current System Date/Time in the default (EEE MMM dd HH:mm:ss zzz yyyy) format
  • We can convert default format to custom format using DateFormat/SimpleDateFormat classes

CurrentDateTimeInJavaUsingCalendar.java

package in.bench.resources.java8.current.date.time;

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

public class CurrentDateTimeInJavaUsingCalendar {

	public static void main(String[] args) {

		// 1. current date
		Calendar calendar = Calendar.getInstance();
		System.out.println("Current Date/Time"
				+ " using Calendar :- \n" + calendar.getTime());


		// 2. format dd-MM-yyyy HH:mm:ss
		DateFormat dateFormat =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String str = dateFormat.format(calendar.getTime());
		System.out.print("\nFormatted Date/Time"
				+ " using Calendar :- \n" + str);
	}
}

Output:

Current Date/Time using Calendar :- 
Mon Sep 12 16:02:38 IST 2022

Formatted Date/Time using Calendar :- 
12/09/2022 16:02:38

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to get current Timestamp in different ways ?
Java 8 – How to get all dates between two Dates ?