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

后端 未结 5 633
面向向阳花
面向向阳花 2020-12-20 15:56

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

public  String getYestrday() {
    DateFormat dateFormat = new SimpleDateFormat(\"yyyy-MM-d         


        
相关标签:
5条回答
  • 2020-12-20 16:31

    Use java.util.Calendar to do it. Or try JODA.

    0 讨论(0)
  • 2020-12-20 16:41

    you can use Calendar class to do the same task:

    Calendar c = new Calendar();
    //c.add(Calendar.DAY_OF_MONTH, -1);
    Date d = c.getTime();
    
    0 讨论(0)
  • 2020-12-20 16:42

    Avoid java.util.Date & .Calendar

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

    Separate Date-Time Manipulation From Formatting

    Also, the code in the question mixes date-time work with formatting. Separate those tasks to make your code clear and testing/debugging easier.

    Time Zone

    Time zone is critical in date-time work. If you ignore the issue, the JVM's default time zone will be applied. A better practice is to always specify rather than rely on default. Even when you want the default, explicitly call getDefault.

    The beginning of the day is defined by the time zone. A new day dawns earlier in Paris than in Montréal. So if by "yesterday" you mean the first moment of that day, then you should (a) specify a time zone, and (b) call withTimeAtStartOfDay.

    Joda-Time

    Example code in Joda-Time 2.3.

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

    Or convert from a java.util.Date object.

    DateTime today = new DateTime( myJUDate, timeZone );
    

    Subtract a day to get to yesterday (or 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 a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd). Such a formatter is already defined in Joda-Time.

    String outputDatePortion = ISODateFormat.date().print( yesterdayStartOfDay );
    
    0 讨论(0)
  • 2020-12-20 16:45

    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());
    
    0 讨论(0)
  • 2020-12-20 16:53

    Following works for me

    int date = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    
    0 讨论(0)
提交回复
热议问题