android 用年月日初始化date_将android datepicker中的日期初始化到特定日期,这不是当前日期。...

bd96500e110b49cbb3cd949968f18be7.png

In my app I have a date saved in a remote database that I want the date picker to be set to. I've researched and only found examples of setting the datepicker today's date via Calender java util. Example:

final Calendar c = Calendar.getInstance();

year = c.get(Calendar.YEAR);

month = c.get(Calendar.MONTH);

day = c.get(Calendar.DAY_OF_MONTH);

How can I use the Calendar to display my date from the database and not today's date? Do you have any suggestions or examples I can follow?

Update:

After experimenting with Calendar I tried to use

// set Date

String eventYear =date.substring(0,4);

String eventDay =date.substring(5,7);

String eventMonth =date.substring(8,10);

//convert string to int for because calendar only takes int: set(int,int)

int month = Integer.parseInt(eventMonth);

final Calendar c = Calendar.getInstance();

mMonth=c.get(c.set(Calendar.MONTH, Calendar.month));

// or mMonth=c.get(Calendar.MONTH, Calendar.month);

Generates error that says cannot convert int to void.

How can I use Calendar to set it to a specific date? According to google's developers site I should be able to do this.

http://developer.android.com/reference/java/util/Calendar.html

example:

set(Calendar.MONTH, Calendar.SEPTEMBER)

I'd like the date to be display in the datepicker from the server as a default value.

解决方案

U can use the updateDate(year, month, day_of_month);

date picker returns integer values of day, month and year. so the parameters must be integer values. and the integer value for the month jan in the date picker is 0.

i needed to put the date extracted from a database into a datepicker. I wrote the following code and it works.

DatePicker DOB;

SQLiteDatabase db;

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

db = openOrCreateDatabase("BdaySMS", SQLiteDatabase.CREATE_IF_NECESSARY, null);

Cursor cur = db.rawQuery("select * from BdaySMS where ph='"+pn+"';", null);//pn is the phone no.

if(cur.moveToFirst())

{

name.setText(cur.getString(0));

phone.setText(cur.getString(1));

DOB.updateDate(Integer.parseInt(cur.getString(4)),Integer.parseInt(cur.getString(3)),Integer.parseInt(cur.getString(2)));

message.setText(cur.getString(5));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值