我有一个日历选择器,允许用户选择开始日期和结束日期。
我想从一个月中提取一天,使用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;
答案 0 :(得分:6)
使用Calendar 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));
答案 1 :(得分:1)
使用Calendar对象:
试试这个:
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;