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,050 points
77 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,150 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 by Sirajul
• 25,420 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
6,135 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 Aug 22 in Java by Sirajul
• 25,420 points
1,114 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
90 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,050 points
464 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 8,089 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,090 points
1,972 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,090 points
5,146 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,090 points
496 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,620 points
379 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
89 views