被谷歌这个BUG坑了一下午了。官方文档上说:
long getDate()Gets the selected date in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
说好的“selected”呢?
我测试的时候发现,setDate() 指定了什么时间,用户修改后,getDate() 仍然返回那个时间。你TM逗我?
幸亏有人发出了解决方案。万分感谢:
http://stackoverflow.com/questions/35728537/android-getdate-returning-wrong-date
大概这样
private long ttt;
下面的 cv 就是calendarview ttt = //默认值,也就是没有切换日期,确定时应该代表的值
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { Calendar c = Calendar.getInstance(); c.set(year,month,dayOfMonth); ttt = c.getTime().getTime(); } });也就是选择项改变的时候就保存到ttt里面,需要用的时候使用ttt代替getDate()。