android datepicker设置日期,Android将DatePicker设置为特定日期

我有3个包含日,月和年值的字符串.例如:

String mday = "02";

String mmonth="07";

String myear="2013";

我需要将活动中的DatePicker设置为从上面的日期开始的一个月.我并不是说只是将1加到mmonth值……如果是第31天,我最终会得到一个无效的日期.

所以我需要一些方法来增加日期(有效的方式)并设置DatePicker的值.

我知道使用Int值设置datePicker是这样的:

DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);

datepicker.init(iYear,iMonth,iDay,null);

// where iYear,iMonth and iDay are integers

但是如何获得递增DATE的日,月和年的整数值一个月?

那么在第一个值(字符串)和递增日期的最终值(整数)之间我必须做出哪些步骤?

我想我必须使用日历.

所以我的代码应该是这样的:

Integer iYear,iDay = 0;

String mday = "02";

String mmonth="07";

String myear="2013";

Calendar cal = Calendar.getInstance();

cal.set(Integer.parseInt(myear),Integer.parseInt(mmonth),Integer.parseInt(mday));

cal.add(Calendar.MONTH,1);

// here I should get the values from cal inside the iYear,but I do not seem to succeed.

DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);

datepicker.init(iYear,null);

如果我做:

datepicker.init(cal.YEAR,cal.MONTH,cal.DATE,null);

然后应用程序崩溃.

我该怎么办?

如何将这个月份日期增加到我的DatePicker?

UPDATE

我将测试代码更改为:

Calendar cal = Calendar.getInstance();

cal.set(2013,05,23);

cal.add(Calendar.MONTH,1);

int xxday = cal.get(Calendar.DATE);

int xxmonth = cal.get(Calendar.MONTH);

int xxyear = cal.get(Calendar.YEAR);

datepicker.init(xxyear,xxmonth,xxday,null);

但是现在datePicker从现在开始设置为一个月,而不是从想要的日期开始一个月而不是(2013-06-23)我有(2013-09-23).我认为这是因为

int xxmonth = cal.get(Calendar.MONTH);

如何从Calendar cal获得真实的月份; ?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值