Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting yesterday - The method getDate() from the type Date is deprecated

Tags:

java

date

I try to get the date of yesterday. So I write the next function:

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

But it gives me the next warning:

The method getDate() from the type Date is deprecated

and it doesn't do it work.

Thank you for your help.

like image 404
Adam Sh Avatar asked Feb 18 '13 14:02

Adam Sh


People also ask

How can I get yesterday's date?

Discussion: To get yesterday's date, you need to subtract one day from today's date.

What is getDate () in Java?

The getDate() method of Java Date class returns the value between 1 to 31 which represents the day of the month by this date object. This method is deprecated as of JDK version 1.1 and replaced by Calender.get(Calender.DAY_OF_MONTH)

Is date class deprecated in Java?

Date Class. Many of the methods in java. util. Date have been deprecated in favor of other APIs that better support internationalization.

What can I use instead of getYear in Java?

getYear. Deprecated. As of JDK version 1.1, replaced by Calendar. get(Calendar.


1 Answers

Date#getDate() is a deprecated method after JDK 1.1. You should be using Calendar class instead to manipulate dates.

From API:

Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.

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

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH)

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return dateFormat.format(cal.getTime());
like image 50
PermGenError Avatar answered Oct 27 '22 13:10

PermGenError