获取昨天 - 不推荐使用类型为Date的getDate()方法 [英] Getting yesterday - The method getDate() from the type Date is deprecated

查看:1339
本文介绍了获取昨天 - 不推荐使用类型为Date的getDate()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着找到昨天的日期。所以我写下一个函数:

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

但它给我下一个警告:

 不推荐使用类型Date的getDate()方法

,它不会工作。



感谢您的帮助。

解决方案

Date#getDate ) 是JDK 1.1之后不推荐使用的方法。您应该使用 日历 类操纵日期。



API


在JDK 1.1之前,类Date有两个附加功能。它
允许以年,月,日,小时,分钟,
和第二个值的形式解释日期。它还允许格式化和解析日期
字符串。不幸的是,这些功能的API不符合国际化的
。从JDK 1.1开始,Calendar类应该是
,用于在日期和时间字段之间进行转换,并且DateFormat类
应用于格式化和解析日期字符串。日期中相应的
方法已被弃用。


在API中也清楚地记录了使用 日期#getDate() 使用日历#get(Calendar.DATE);


已弃用。从JDK 1.1版开始,由
替换Calendar.get(Calendar.DAY_OF_MONTH)




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


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.

解决方案

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());

这篇关于获取昨天 - 不推荐使用类型为Date的getDate()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆