在android中设置时间和日期到日期选择器和时间选择器。

时间:2022-09-26 11:05:21

I am using a date picker and time picker in my application. I want to set the date and time when the page loads. Is this possible? How so?

我在我的应用程序中使用日期选择器和时间选择器。我想设置页面加载时的日期和时间。这是可能的吗?所以如何?

5 个解决方案

#1


117  

I solved a similar problem of mine as follows

我解决了一个类似的问题,如下所示

Calendar cal = Calendar.getInstance();

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);

datePicker.updateDate(year, month, day);

timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);

#2


14  

Using JodaTime

JodaTime is an extremely popular and useful library for handling dates and times in Java. If you're not using it, I highly recommend taking a look at it.

JodaTime是一个非常流行和有用的库,用于处理Java中的日期和时间。如果您不使用它,我强烈建议您看看它。

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

这里有一个简单的例子,说明我如何从DateTime对象设置一个DatePicker和TimePicker,该对象可以是当前日期,也可以是过去或未来的任何日期(本例中的属性称为inspected_at):

DatePicker datePicker = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker timePicker = (TimePicker) findViewById(R.id.inspected_at_time);

DateTime inspected_at = DateTime.now()                // 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();

datePicker.updateDate(year, month, day);              // Set date.

timePicker.setCurrentHour(hour);                      // Set time.
timePicker.setCurrentMinute(minutes);

Hope that helps.

希望有帮助。

JP

摩根大通

#3


7  

There is a updateDate method for the DatePicker.

DatePicker有一个updateDate方法。

Here is what I use in one of my app, not using a dialog:

这是我在我的一个应用程序中使用的,而不是使用对话框:

//set datePicker to position
String date_saved = project_row.getString(
    project_row.getColumnIndex("project_startdate"));

int day  = Integer.parseInt(date_saved.substring(0,2));
int year = Integer.parseInt(date_saved.substring(5,9));
String month = date_saved.substring(2,5);
int month_code = getMonthInt(month);

project_startdate_data = (DatePicker) findViewById(R.id.project_startdate_data);
project_startdate_data.updateDate(year, month_code, day);

where project_row is my cursor, and the date is saved in the column project_startdate like 22MAY2012

project_row是我的光标,日期保存在project_startdate列中,比如2012年5月22日

and you need this:

你需要:

private int getMonthInt(String month) {
    if (month.equalsIgnoreCase("JAN")) return 0;
    if (month.equalsIgnoreCase("FEB")) return 1;
    if (month.equalsIgnoreCase("MAR")) return 2;
    if (month.equalsIgnoreCase("APR")) return 3;
    if (month.equalsIgnoreCase("MAY")) return 4;
    if (month.equalsIgnoreCase("JUN")) return 5;
    if (month.equalsIgnoreCase("JUL")) return 6;
    if (month.equalsIgnoreCase("AUG")) return 7;
    if (month.equalsIgnoreCase("SEP")) return 8;
    if (month.equalsIgnoreCase("OCT")) return 9;
    if (month.equalsIgnoreCase("NOV")) return 10;
    if (month.equalsIgnoreCase("DEC")) return 11;

    //return -1 if month code not found
    return -1;
}

#4


2  

Check this How to use date picker dialog. and use update function in onCreate(); if by page loads you mean activity starts.... hope it helps.

检查这个如何使用日期选择器对话框。在onCreate()中使用update函数;如果页面加载你的意思活动开始....希望它可以帮助。

#5


1  

Check this.

检查这个。

Use showDialog(TIME_DIALOG_ID); function in onCreate();

使用showDialog(TIME_DIALOG_ID);在onCreate()函数;

#1


117  

I solved a similar problem of mine as follows

我解决了一个类似的问题,如下所示

Calendar cal = Calendar.getInstance();

int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);

datePicker.updateDate(year, month, day);

timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);

#2


14  

Using JodaTime

JodaTime is an extremely popular and useful library for handling dates and times in Java. If you're not using it, I highly recommend taking a look at it.

JodaTime是一个非常流行和有用的库,用于处理Java中的日期和时间。如果您不使用它,我强烈建议您看看它。

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

这里有一个简单的例子,说明我如何从DateTime对象设置一个DatePicker和TimePicker,该对象可以是当前日期,也可以是过去或未来的任何日期(本例中的属性称为inspected_at):

DatePicker datePicker = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker timePicker = (TimePicker) findViewById(R.id.inspected_at_time);

DateTime inspected_at = DateTime.now()                // 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();

datePicker.updateDate(year, month, day);              // Set date.

timePicker.setCurrentHour(hour);                      // Set time.
timePicker.setCurrentMinute(minutes);

Hope that helps.

希望有帮助。

JP

摩根大通

#3


7  

There is a updateDate method for the DatePicker.

DatePicker有一个updateDate方法。

Here is what I use in one of my app, not using a dialog:

这是我在我的一个应用程序中使用的,而不是使用对话框:

//set datePicker to position
String date_saved = project_row.getString(
    project_row.getColumnIndex("project_startdate"));

int day  = Integer.parseInt(date_saved.substring(0,2));
int year = Integer.parseInt(date_saved.substring(5,9));
String month = date_saved.substring(2,5);
int month_code = getMonthInt(month);

project_startdate_data = (DatePicker) findViewById(R.id.project_startdate_data);
project_startdate_data.updateDate(year, month_code, day);

where project_row is my cursor, and the date is saved in the column project_startdate like 22MAY2012

project_row是我的光标,日期保存在project_startdate列中,比如2012年5月22日

and you need this:

你需要:

private int getMonthInt(String month) {
    if (month.equalsIgnoreCase("JAN")) return 0;
    if (month.equalsIgnoreCase("FEB")) return 1;
    if (month.equalsIgnoreCase("MAR")) return 2;
    if (month.equalsIgnoreCase("APR")) return 3;
    if (month.equalsIgnoreCase("MAY")) return 4;
    if (month.equalsIgnoreCase("JUN")) return 5;
    if (month.equalsIgnoreCase("JUL")) return 6;
    if (month.equalsIgnoreCase("AUG")) return 7;
    if (month.equalsIgnoreCase("SEP")) return 8;
    if (month.equalsIgnoreCase("OCT")) return 9;
    if (month.equalsIgnoreCase("NOV")) return 10;
    if (month.equalsIgnoreCase("DEC")) return 11;

    //return -1 if month code not found
    return -1;
}

#4


2  

Check this How to use date picker dialog. and use update function in onCreate(); if by page loads you mean activity starts.... hope it helps.

检查这个如何使用日期选择器对话框。在onCreate()中使用update函数;如果页面加载你的意思活动开始....希望它可以帮助。

#5


1  

Check this.

检查这个。

Use showDialog(TIME_DIALOG_ID); function in onCreate();

使用showDialog(TIME_DIALOG_ID);在onCreate()函数;