Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (2.6k points)
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
+1 vote
1 answer
0 votes
1 answer

Browse Categories

...