Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get today's Date?

Tags:

java

date

In other words, I want functionality that provides Joda-Time:

today = today.withTime(0, 0, 0, 0); 

but without Joda-Time, only with java.util.Date.

Methods such as .setHours() and etc. are deprecated. Is there are more correct way?

like image 429
Plastic Rabbit Avatar asked Feb 18 '11 21:02

Plastic Rabbit


People also ask

How do I get today's date in Excel?

Insert a static date or time into an Excel cell On a worksheet, select the cell into which you want to insert the current date or time. Do one of the following: To insert the current date, press Ctrl+; (semi-colon).

How do I get todays date in python?

today() The today() method of date class under DateTime module returns a date object which contains the value of Today's date. Returns: Return the current local date.

How do I get today's date in JavaScript?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.


2 Answers

Date today = new Date(); today.setHours(0); //same for minutes and seconds 

Since the methods are deprecated, you can do this with Calendar:

Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 0); // same for minutes and seconds 

And if you need a Date object in the end, simply call today.getTime()

like image 137
Bozho Avatar answered Sep 30 '22 14:09

Bozho


Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH); 

DateUtils from Apache Commons-Lang. Watch out for time zone!

like image 23
Tomasz Nurkiewicz Avatar answered Sep 30 '22 16:09

Tomasz Nurkiewicz