将android datepicker中的日期初始化到特定日期,这不是当前日期。 [英] Initialize date in android datepicker to a specific date, that is not the current date.

查看:450
本文介绍了将android datepicker中的日期初始化到特定日期,这不是当前日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将一个日期保存在远程数据库中,我希望日期选择器被设置为。我已经研究过,只发现通过Calender java util设置datepicker今天的日期的例子。例如:

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);

生成说不能将int转换为void的错误。

Generates error that says cannot convert int to void.

如何使用日历将其设置为特定日期?根据google的开发者网站,我应该能够做到这一点。
http://developer.android.com/reference/java/util /Calendar.html

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

示例:

set(Calendar.MONTH, Calendar.SEPTEMBER)

我希望在datepicker中显示日期从服务器作为默认值。

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

推荐答案

U可以使用 updateDate(year,month,day_of_month);
date选择器返回日,月和年的整数值。所以参数必须是整数值。并且日期选择器中的月份的整数值为0.

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));
}

这篇关于将android datepicker中的日期初始化到特定日期,这不是当前日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆