Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Current Date and Time
3.
How to obtain Current Date and Time in Java
3.1.
Using Date Class
3.2.
Example
3.3.
Using Calendar Class
3.4.
Example
4.
Date/Time API
4.1.
LocalDateTime
4.2.
LocalDate
4.3.
LocalTime
4.4.
ZonedDateTime
5.
Examples
5.1.
Code 1
5.2.
Code 2
6.
Frequently Asked Questions
6.1.
What is the date and time in Java?
6.2.
How to display the current date in Java?
6.3.
How to represent the current date in Java?
7.
Conclusion
Last Updated: Mar 27, 2024

Java Current Date and time

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The date and time are crucial for organizing and arranging daily chores, maintaining records, and monitoring activity. You will need to use the date and time features in some capacity for every development project, whether it is a web development project or perhaps an entire program. In this article, we shall be learning more about it.

Also Read About, Multithreading in java

Java Current Date and Time

We can quickly obtain the current date and time in Java by utilizing SimpleDateFormat and the Date/Calendar class. We'll review how to use the Date and Calendar class to retrieve the current date and time and SimpleDateFormat to get it in your chosen format.

Read More About, Basics of Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to obtain Current Date and Time in Java

The following methods are used to obtain the current date in the Java program when the need arises:

Using Date Class

When establishing an instance of the SimpleDateFormat, the user specifies the needed pattern. The parameter is the date object passed to the format() function of the DateFormat class.

We will need to import "java.text.SimpleDateFormat" and "java.util.Date".

Example

import java.text.SimpleDateFormat;  
import java.text.DateFormat;
import java.util.Date;  
public class Main
{
	public static void main(String[] args) {
		System.out.println("Hello World");
		DateFormat dfrm = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
        Date dt = new Date();
        System.out.println(dfrm.format(dt));
	}
}

Output:

10/07/22 16:27:51

Using Calendar Class

The getInstance function is used to construct an object of the Calendar class (). The calendar is the name of the format method. The method accepts getTime() as an argument.

Example

import java.text.SimpleDateFormat;  
import java.text.DateFormat;
import java.util.Date;  
import java.util.Calendar;
public class Main
{
	public static void main(String[] args) {
		DateFormat dfrm = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
        Calendar dt = Calendar.getInstance();
        System.out.println(dfrm.format(dt.getTime()));

	}
}

Output:

10/07/22 16:27:17

Date/Time API

In Java 8, a new API was provided. To replace Java.util.Date and java.util.calendar was the only reason for doing this. The following classes make it up:

LocalDateTime

The LocalDateTime class instance is returned by this method, which also prints the current date and time.

System.out.println(java.time.LocalDateTime.now());

LocalDate

Simply put, this method returns the current Date.

LocalDate date = LocalDate.now();

LocalTime

The current time is all that this method returns, and it supports formatting.

LocalTime time = LocalTime.now();

ZonedDateTime

This approach can be utilized in the following ways depending on the time zone.

ZonedDateTime dateTime = ZonedDateTime.now();

Examples

Code 1

In this code, we will be getting the current date and time.

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


public class CurrentDateAndTime {


		public static void main(String[] args) {
		//Using the Date class, obtain the current date and time
		DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
		Date dateobj = new Date();
		System.out.println(df.format(dateobj));


		/*getting current date time using calendar class 
		* An Alternative of above*/
		Calendar calobj = Calendar.getInstance();
		System.out.println(df.format(calobj.getTime()));
	}
}

Output

06/07/22 20:35:27
06/07/22 20:35:27

In this example, we successfully get the current date and time.

Code 2

In this example, we shall get the current date and time in another time zone.

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


public class CurrentDateTimeinDifferentZone {


	public static void main(String[] args) {
		//"hh" in pattern is for 12 hour time format and "aa" is for AM/PM
		SimpleDateFormat dateTimeInGMT = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa");
		//Setting the time zone
		dateTimeInGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
		System.out.println(dateTimeInGMT.format(new Date()));
	}
}

Output

2022-Jul-06 03:11:15 pm

In this example, we have successfully gotten the current date and time of different time zone.

Practice it on online java compiler for better understanding.

Check out this article - Upcasting and Downcasting in Java

Frequently Asked Questions

What is the date and time in Java?

The current date and time are contained in the Date class, which Java offers and is included in the Java.util package. The following table displays the two constructors that the Date class provides. The current date and time are used to initialize the object in this function Object().

How to display the current date in Java?

Although there isn't a built-in Date class in Java, we may use the date and time API by importing the Java.time package. There are numerous date and time classes in the package.

How to represent the current date in Java?

The class Clock can also indicate the current date and time in UTC form.

Conclusion

In this article, we have covered the Java Current Date and time. We have discussed several ways how to get the current date and time. We also discussed how to get the different formats of dates. We also discussed the several Date/Time API. 

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!".

 

Previous article
Methods of Date Class
Next article
Compare Dates in Java
Live masterclass