将 android datepicker 中的日期初始化为特定日期,即不是当前日期。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11028386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:47:32  来源:igfitidea点击:

Initialize date in android datepicker to a specific date, that is not the current date.

androiddatecalendarinitializationandroid-datepicker

提问by Leoa

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:

在我的应用程序中,我在远程数据库中保存了一个日期,我希望将日期选择器设置为该日期。我已经研究过并且只找到了通过 Calender java util 设置日期选择器今天日期的示例。例子:

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.

生成错误,提示无法将 int 转换为 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

如何使用日历将其设置为特定日期?根据谷歌的开发者网站,我应该能够做到这一点。 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.

我希望日期作为默认值显示在服务器的日期选择器中。

采纳答案by SABYASACHI

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.

你可以使用updateDate(year, month, day_of_month); 日期选择器返回日、月和年的整数值。所以参数必须是整数值。日期选择器中 jan 月份的整数值为 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));
}

回答by Joshua Pinter

Use JodaTime

使用JodaTime

Here's a simple example of how I set a DatePickerand TimePickerfrom a DateTimeobject, which could be the current date or any date from the past or future (the attribute in this case is called inspected_at):

这是我如何从对象设置 aDatePicker和的简单示例,该对象可以是当前日期或过去或未来的任何日期(本例中的属性称为):TimePickerDateTimeinspected_at

DatePicker dp = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker tp = (TimePicker) findViewById(R.id.inspected_at_time);

DateTime inspected_at = DateTime.now().minusYears(1); // Typically pulled from DB.

int year    = inspected_at.getYear() ;
int month   = inspected_at.getMonthOfYear() - 1;      // Need to subtract 1 here.
int day     = inspected_at.getDayOfMonth();  
int hour    = inspected_at.getHourOfDay();
int minutes = inspected_at.getMinuteOfHour();

dp.updateDate(year, month, day);
tp.setCurrentHour(hour);
tp.setCurrentMinute(minutes);

Hope that helps.

希望有帮助。

JP

J.P