【发布时间】:2017-03-03 22:20:56
【问题描述】:
我有一个 CalendarView,它允许用户选择日期并输入活动。
我有一个方法,每当所选日期更改时调用如下:
private void selectedDateChanged(CalendarView view, int year, int month, int dayOfMonth){
//note: _Calendar is set to the view when activity loads hence the
//the reason for not using view.getDate();
Long timeSinceEpoch = _Calendar.getDate();
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(timeSinceEpoch);
System.out.println(String.format("With gregorian calendar \n Day: %s \n Month: %s \n Year: %s",
calendar.DAY_OF_MONTH, calendar.MONTH, calendar.YEAR));
}
每次选择的日期更改时都会调用该方法,问题是每次调用该方法时 GregorianCalendar 都不会更新。每当我选择新的一天时,
I/System.out: With gregorian calendar
I/System.out: Day: 5
I/System.out: Month: 2
I/System.out: Year: 1
打印出来并且在选择新日期时不会更新。
我不知道如何强制 GregorianCalendar 更新,CalendarView 的 javadoc 说 getDate() 应该以毫秒为单位返回当前选择的日期,因为 unix epoch
【问题讨论】:
-
为什么不使用传递的参数
year、month、dayOfMonth? -
@petey 我需要从其他没有这些参数的方法中获取日期。这只是一个方便的测试场所
标签: java android gregorian-calendar calendarview