gpt4 book ai didi

java - 如何从安卓日历中获取日期?

转载 作者:行者123 更新时间:2023-11-30 03:12:57 25 4
gpt4 key购买 nike

请帮我从日历中获取日期。想法是用户必须选择日期。之后日期返回到主要 Activity 。我正在尝试这样做:

Calendar today = Calendar.getInstance();

Uri uriCalendar = Uri.parse("content://com.android.calendar/time/" + String.valueOf(today.getTimeInMillis()));
Intent intentCalendar = new Intent(Intent.ACTION_DATE_CHANGED,uriCalendar);
startActivity(intentCalendar);

但它只能打开日历,我无法选择日期。谢谢 =)

最佳答案

我正在使用它,它对我有用另一个提示,对于复杂的日期操作,我更喜欢 Joda Time 而不是 Calendar。我真的推荐它。

  change_date_but = (Button) findViewById(R.id.c_change_button_id);
change_date_but.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// DatePickerDialog DPD = new DatePickerDialog(this,
// mDatesetlistener, mYear, mMonth, mDay);
DatePickerDialog DPD = new DatePickerDialog(
AddEditChildren.this, mDateSetListener, mYear, mMonth,
mDay);
DPD.show();

}
});

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};

//我用它来显示一个漂亮的日期格式

private void updateDisplay() {
// TODO Auto-generated method stub
if(mMonth+1<10 && mDay >10)
date.setText(new StringBuilder()
.append(mYear).append("-").append("0").append(mMonth + 1).append("-").append(mDay));
else if(mMonth+1>10 && mDay<10)
date.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-").append(mMonth + 1).append("-").append("0").append(mDay));

else if(mMonth+1<10 && mDay<10)
date.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-").append("0").append(mMonth + 1).append("-").append("0").append(mDay));
else
date.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-").append(mMonth + 1).append("-").append(mDay));
}

关于java - 如何从安卓日历中获取日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com