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,040 points
59 views

1 answer 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,150 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,540 points
5,379 views
0 votes
2 answers

How do I get the size of a java.sql.ResultSet?

You could probably do rows-count. ResultSet rs = ...READ MORE

answered 6 hours ago in Java by Sirajul
• 19,030 points
1,002 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,540 points
82 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,040 points
376 views
+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 7,222 views
0 votes
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,050 points
1,820 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,050 points
4,758 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,050 points
415 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by parth295
• 4,600 points
258 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
78 views