繁体   English   中英

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

[英]Setting time and date to date picker and time picker in android

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

我解决了我的类似问题如下

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

使用乔达时间

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

这是一个简单的示例,说明我如何从DateTime object 中设置DatePickerTimePicker ,这可能是当前日期或过去或未来的任何日期(本例中的属性称为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);

希望有帮助。

J.P

DatePicker 有一个updateDate方法。

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

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

其中project_row是我的 cursor,日期保存在project_startdate列中,如22MAY2012

你需要这个:

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

检查此如何使用日期选择器对话框 并在 onCreate() 中使用更新 function; 如果按页面加载,您的意思是活动开始......希望它有所帮助。

检查这个

使用showDialog(TIME_DIALOG_ID); function 在onCreate();

在 kotlin 中使用此代码

private fun setTime(hours: Int, minutes: Int) {
        @Suppress("DEPRECATION")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            binding.timePickerLayout.timePicker.hour = hours
            binding.timePickerLayout.timePicker.minute = minutes
        } else {
            binding.timePickerLayout.timePicker.currentHour = hours
            binding.timePickerLayout.timePicker.currentMinute = minutes
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM