-1

我有一个日历选择器,允许用户选择开始日期和结束日期。

我想从一个月中提取一天,使用 getDay 的旧方法会从一个月中产生错误的一天。有没有其他方法可以用来从一个月中获取日期并将其放入 int 类型?

      Date date_from = HolidayForm.pickerFrom.getDate();
      Date date_to = HolidayForm.pickerTo.getDate();

      //getDay is deprecated 
      int from = date_from.getDay();
      int to = date_to.getDay();

      //so i can do to find difference. 
      int diff = to-from;
4

2 回答 2

6

使用日历 API

类中的方法java.util.Date大多已被弃用。您必须使用java.util.calendar类才能对日期进行操作。

Date d = new Date(); 
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        System.out.println(cal.get(Calendar.DAY_OF_MONTH));
于 2013-02-28T00:02:50.490 回答
1

使用日历对象:

尝试这个 :

Date date_from = HolidayForm.pickerFrom.getDate();
Date date_to = HolidayForm.pickerTo.getDate();

Calendar calFrom = Calendar.getInstance();
calFrom.setTime(date_from);
int from = calFrom.get(Calendar.DAY_OF_MONTH);

Calendar calTo = Calendar.getInstance();
calTo.setTime(date_to);
int to = calTo.get(Calendar.DAY_OF_MONTH);

int diff = to-from;
于 2013-02-28T00:05:30.970 回答