如何从不推荐使用的Date类型中替换getDate()? [英] How to replace getDate() from the type Date which is deprecated?

查看:347
本文介绍了如何从不推荐使用的Date类型中替换getDate()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须纠正的现有程序。它包含以下几行:

I have an existing program that I have to correct . It contains these lines :

        Date startDate = new Date();
        int day = startDate.getDate() - 1;

getDate()类型为 Date 已过时,因此我必须使用 Calender 进行更改。我尝试了这个:

but getDate() from the type Date is deprecated so i have to change it using Calender. I tried this :

Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.DATE, -1);
int day= startDate.getTime();

但这会导致以下错误:


类型不匹配:无法从Date转换为int

Type mismatch: cannot convert from Date to int


推荐答案

如果您想获取月份中的某天,请使用以下命令:

If you want to get day in month use this:

int day= startDate.get(Calendar.DAY_OF_MONTH);

如果您想获取一周中的某天,请使用以下命令:

If you want to get day in week use this:

int day= startDate.get(Calendar.DAY_OF_WEEK);

也要注意星期几,因为第0天是星期天而不是星期一。

Also be careful about day of week, because day 0 is sunday not monday.


get和set的字段号指示星期几。
字段的值是星期日,星期一,星期二,星期三,星期四,
星期五和星期六。

Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

这篇关于如何从不推荐使用的Date类型中替换getDate()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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