Getting yesterday - getDate () method of type Date is deprecated

I am trying to get the date of yesterday. Therefore, I am writing the following function:

public String getYestrday() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); return dateFormat.format(date.getDate() - 1); } 

But this gives me the following warning:

 The method getDate() from the type Date is deprecated 

and it does not work.

Thank you for your help.

+8
java date
source share
5 answers

Date # getDate () is an obsolete method after JDK 1.1. You must use Calendar to manage dates.

From the API :

Prior to JDK 1.1, the Date class had two additional functions. This made it possible to interpret dates as year, month, day, hour, minute, and second values. It also allowed formatting and parsing the date of a string. Unfortunately, the API for these functions did not lend itself to internationalization. Starting with JDK 1.1, the Calendar class should be used to convert between date and time fields and the DateFormat class should be used for formatting and parsing date strings. Appropriate methods on Date are out of date.

It is also clearly documented in the API using Date # getDate () to use Calendar#get(Calendar.DATE);

Outdated. Starting with version 1.1 of the JDK, replaced by Calendar.get (Calendar.DAY_OF_MONTH)

 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return dateFormat.format(cal.getTime()); 
+16
source share

Use java.util.Calendar for this. Or try JODA .

+1
source share

Avoid java.util.Date and .Calendar

The answer is accepted correctly. However, the java.util.Date and .Calendar classes are notoriously unpleasant. Avoid them. Use Joda-Time or the new java.time package (in Java 8).

Separate manipulation of time dates from formatting

In addition, the code in the question mixes date-time with formatting. Separate these tasks to make your code clean and testing / debugging easier.

Timezone

The time zone is critical for working with a date. If you ignore the problem, the default JVM time zone will be applied. Best practice is to always indicate, not rely on default. Even if you want the default, explicitly call getDefault .

The start of the day is determined by the time zone. A new day comes earlier in Paris than in Montreal. Therefore, if β€œyesterday” you mean the first moment of this day, then you must (a) specify the time zone and (b) call withTimeAtStartOfDay .

Joda time

Sample code in Joda-Time 2.3.

 DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); DateTime today = DateTime.now( timeZone ); 

Or convert from java.util.Date object.

 DateTime today = new DateTime( myJUDate, timeZone ); 

Subtract the day to get to yesterday (or the day before).

 DateTime yesterday = today.minusDays( 1 ); DateTime yesterdayStartOfDay = today.minusDays( 1 ).withTimeAtStartOfDay(); 

By default, Joda-Time and java.time parse / generate strings in ISO 8601 format .

 String output = yesterdayStartOfDay.toString(); // Uses ISO 8601 format by default. 

Use the formatter for the full date as a four-digit year, a two-digit month of the year, and a two-digit day of the month (yyyy-MM-dd). Such a formatter is already defined in Yoda-Time.

 String outputDatePortion = ISODateFormat.date().print( yesterdayStartOfDay ); 
+1
source share

you can use the Calendar class to accomplish the same task:

 Calendar c = new Calendar(); //c.add(Calendar.DAY_OF_MONTH, -1); Date d = c.getTime(); 
0
source share

Following work for me

 int date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 
0
source share

All Articles