在android中将时间和日期设置为日期选择器和时间选择器

发布于 2024-11-04 07:44:36 字数 56 浏览 4 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

徒留西风 2024-11-11 07:44:36

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

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

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);
笛声青案梦长安 2024-11-11 07:44:36

使用 JodaTime

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

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

希望有帮助。

太平绅士

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.

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

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

只有一腔孤勇 2024-11-11 07:44:36

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

There is a updateDate method for the DatePicker.

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

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;
}
孤芳又自赏 2024-11-11 07:44:36

查看如何使用日期选择器对话框。并在onCreate()中使用更新函数;如果页面加载意味着活动开始......希望它有帮助。

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.

梅窗月明清似水 2024-11-11 07:44:36

检查这个

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

Check this.

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

无法回应 2024-11-11 07:44:36

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

Use this code in 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
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文