Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)
recategorized by
How do I get the current date in Java?

In C# it is DateTime.Now.

1 Answer

0 votes
by (46k points)

Just create a new Date object externally any arguments; this will assign the prevailing date and time to the new object.

import java.util.Date;

Date d = new Date();

In the terms 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 certain you're using java.util.Date and not java.sql.Date -- the latter doesn't possess a zero-arg constructor, and has moderately different semantics that is the subject of an completely different discussion. 

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...