如何在"时间"中存储小时和分钟?宾语

时间:2016-02-19 14:47:07

标签: android time timepicker

我使用android studio,我有一个时间选择器,用户将选择(小时和分钟)我应该将这个时间信息存储在(时间)对象中以将其传递给数据库 如何在时间对象中存储小时和分钟? 我希望它清楚

hourOfDay //将存储用户选择的小时数。 分钟//将存储用户选择的分钟

pickerTime = (TimePicker)findViewById(R.id.pickertime);
        Calendar now = Calendar.getInstance();

        pickerTime.setHour(now.get(Calendar.HOUR_OF_DAY));
        pickerTime.setMinute(now.get(Calendar.MINUTE));

        pickerTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(getApplicationContext(),
                        "onTimeChanged", Toast.LENGTH_SHORT).show();

                info.setText(
                        "Hour of Day: " + hourOfDay + "\n" +
                                "Minute: " + minute);
            }});

2 个答案:

答案 0 :(得分:1)

我认为您需要在数据库HH:MM

中的正式时间进行设置
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

         // date is final String variable               
           date = String.valueOf(hourOfDay).toString() + ":" + String.valueOf(minute).toString();
        }

这是用一个字符串HH:MM认为你需要的时间

答案 1 :(得分:0)

待办事项

    pickerTime = (TimePicker)findViewById(R.id.pickertime);
    Calendar now = Calendar.getInstance();

    pickerTime.setHour(now.get(Calendar.HOUR_OF_DAY));
    pickerTime.setMinute(now.get(Calendar.MINUTE));

    pickerTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            Toast.makeText(getApplicationContext(),
                    "onTimeChanged", Toast.LENGTH_SHORT).show();


            now.set(Calendar.HOUR_OF_DAY, hourOfDay);
            now.set(Calendar.MINUTE, minute);

            Date date = now.getTime();

            info.setText("Hour of Day: " + hourOfDay + "\n" +
                            "Minute: " + minute);
        }});