安卓CalendarView:我如何得到正确格式的日期? [英] Android CalendarView: How do I get the date in correct format?
问题描述
这几天我一直在寻找方法来从Android 4.0的得到一个可读约会我的calendarview的。我不能设法找到一个适合我的问题的解决方案或示例。我没有得到它以毫秒为单位而不是在一个日期格式。
The past few days I've been searching for ways to get a 'readable' date out of my calendarview from android 4.0. I can't manage to find a solution or example that suits my problem. I did get it in miliseconds but not in a date format.
我的问题是:我有一个calendarview,我想用户,在日期格式YY-MM-DD在logcat中所示的选择日期
My problem is: I have a calendarview and I want the selected date by the user, shown in logcat in a dateformat yy-mm-dd.
我是用的日期选择器的的Android 2.2和我不熟悉calendarview并不能找到太多关于它的。有谁知道一个解决方案?
I was used to the datepicker from android 2.2 and I'm not familiar with calendarview and can't find much about it either. Does anyone know a solution for this?
推荐答案
好了,所以这里是如何做到这一点。当您炒掉calendarview活动或活动内的calendarview它设置日期为当前日期(今天的意思)。为了得到这个当前日期只使用日历
由Java API提供的对象,将会在下面这个日期例如:
Okay so here is how to do this. When you fire your calendarview activity or a calendarview inside your activity it sets the date to the current date(meaning today). To get this current date just use the Calendar
object provided by the java api to get this date example below:
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);
要得到一个日期改变,你必须这样做。
to get a date changed you must do this
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);
}
}
这篇关于安卓CalendarView:我如何得到正确格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!