How do I get the current date and time in Java?
I am looking for something that is equivalent to DateTime.Now
from C#.
datetimejava
How do I get the current date and time in Java?
I am looking for something that is equivalent to DateTime.Now
from C#.
Best Answer
Just construct a new
Date
object without any arguments; this will assign the current date and time to the new object.In the words of the Javadocs for the zero-argument constructor:
Make sure you're using
java.util.Date
and notjava.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. :)