在过去的几天里,我一直在寻找从 android 4.0 的日历视图中获取“可读”日期的方法。我无法找到适合我的问题的解决方案或示例。我确实以毫秒为单位得到了它,但不是以日期格式。
我的问题是:我有一个日历视图,我想要用户选择的日期,以日期格式 yy-mm-dd 显示在 logcat 中。
我已经习惯了android 2.2的datepicker,我对calendarview不熟悉,也找不到太多关于它的信息。有谁知道这个问题的解决方案?
好的,这就是如何做到这一点。当您触发日历视图活动或活动中的日历视图时,它将日期设置为当前日期(即今天)。要获取当前日期,只需使用Calendar
java api 提供的对象来获取此日期示例如下:
Calendar date = Calendar.getInstance();
// for your date format use
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
// set a string to format your current date
String curDate = sdf.format(date.getTime());
// print the date in your log cat
Log.d("CUR_DATE", curDate);
要更改日期,您必须执行此操作
CalendarView myCalendar = (CalendarView) findViewById(R.id.myCalenderid);
myCalendar.setOnDateChangeListener(myCalendarListener);
OnDateChangeListener myCalendarListener = new OnDateChangeListener(){
public void onSelectedDayChange(CalendarView view, int year, int month, int day){
// add one because month starts at 0
month = month + 1;
// output to log cat **not sure how to format year to two places here**
String newDate = year+"-"+month+"-"+day;
Log.d("NEW_DATE", newDate);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)