How do I get the current date and time using Java

0 votes
How do I print the current date and time, if I am working with real time applications?
Apr 13, 2018 in Java by Daisy
• 8,140 points
• 5,483 views

2 answers to this question.

0 votes

To print current date and time using Java, you have to import libraries:

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;    

Then to print date and time, you have to write the code given below.

DateTimeFormatter dt = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  

LocalDateTime now = LocalDateTime.now();  

System.out.println(dt.format(now)); 
Output will be:
2018/04/13 16:18:28
answered Apr 13, 2018 by Akrati
• 3,190 points
0 votes

If you require a time stamp in format YYYY.MM.DD-HH.MM.SS :

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())
answered Aug 23, 2019 by Sirajul
• 59,190 points

Related Questions In Java

0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,600 points
• 29,611 views
0 votes
0 answers

How to get the current London time and date using javascript

Using the code below, I attempted to& ...READ MORE

Dec 7, 2022 in Java by Nicholas
• 7,760 points
• 3,201 views
0 votes
1 answer

How to get the current date/time in Java?

It depends on what form of date/time ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,730 points
• 3,071 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
• 2,004 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar • 90,706 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
• 11,138 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
• 20,986 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz • 7,774 views