Kim*_*rly 0 android date-format calendarview
在过去的几天里,我一直在寻找从android 4.0中获取日历视图中"可读"日期的方法.我无法找到适合我的问题的解决方案或示例.我确实在几毫秒内得到它但不是日期格式.
我的问题是:我有一个日历视图,我希望用户选择日期,在日志格式yy-mm-dd中以logcat显示.
我习惯了android 2.2中的datepicker,我不熟悉calendarview,也找不到它.有谁知道这方面的解决方案?
好的,这是如何做到这一点.当您在活动中触发日历视图活动或日历视图时,它会将日期设置为当前日期(意味着今天).要获取当前日期,只需使用Calendarjava 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);
Run Code Online (Sandbox Code Playgroud)
要更改日期,您必须这样做
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9210 次 |
| 最近记录: |