Posted By: Anonymous
How do I get the current date and time in Java?
I am looking for something that is equivalent to
DateTime.Now from C#.
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();
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. 🙂