【问题标题】:Setting time and date to date picker and time picker in android在android中设置时间和日期到日期选择器和时间选择器
【发布时间】:2011-08-14 15:26:51
【问题描述】:

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

【问题讨论】:

    标签: android timepicker android-datepicker


    【解决方案1】:

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

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

    【讨论】:

    • setHour() 和 setMinute() 自 Android API 23 起。
    • 更新月份是从 0(一月)到 11(十二月),但是天从 1 开始。
    【解决方案2】:

    使用JodaTime

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

    这是一个简单示例,说明我如何从 DateTime 对象设置 DatePickerTimePicker,这可能是当前日期或过去或未来的任何日期(本例中的属性称为 @987654326 @):

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

    希望对您有所帮助。

    日本

    【讨论】:

    • 如果您正在阅读此问题,但您不知道 JodaTime 是什么,请花点时间查看一下。它比 java.util.Calendar 有很多优点
    • 需要注意的是,Java8 引入了 java.time 包,它为处理日期和时间提供了出色的支持,而无需第三方依赖 JodaTime。
    【解决方案3】:

    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 是我的光标,日期保存在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;
    }
    

    【讨论】:

      【解决方案4】:

      检查此How to use date picker dialog。并在 onCreate() 中使用更新函数;如果按页面加载,您的意思是活动开始......希望它有所帮助。

      【讨论】:

        【解决方案5】:

        检查this

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

        【讨论】:

        • 我没有将日期时间选择器显示为对话框
        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2012-09-11
          • 2011-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多