Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current date and time

Tags:

java

datetime

How do I get the current date and time in Java?

I am looking for something that is equivalent to DateTime.Now from C#.

like image 206
mrblah Avatar asked Jan 06 '10 00:01

mrblah


People also ask

How do I get the current date and time in python?

today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

How can I get current date and time in Android?

Calendar Date currentTime = Calendar. getInstance(). getTime();


1 Answers

Just construct a new Date object without any arguments; this will assign the current date and time to the new object.

import java.util.Date;  Date d = new Date(); 

In the words of the Javadocs for the zero-argument constructor:

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Make sure you're using java.util.Date and not java.sql.Date -- the latter doesn't have a zero-arg constructor, and has somewhat different semantics that are the topic of an entirely different conversation. :)

like image 93
delfuego Avatar answered Oct 04 '22 13:10

delfuego