如何在选择时间和日期前6小时设置日期和时间?

时间:2016-09-26 10:38:59

标签: java android

我正在开发一个应用程序,这里我想使用日期和时间选择器,选择日期和时间,但我想在选择时间前6小时设置结果时间。如何在选择时间前6小时设置此时间,并在适当的时间内设置时间? 例: 如果我选择日期假设“30sep”并且时间是“凌晨1:00”那么我想要显示的日期和时间将是“29sep”和“7:00 pm”。 我该怎么办? 我使用以下代码:

 @Override
    public void onClick(View v) {

        if (v == pdate) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            eddate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == ptime) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {


                            edtime.setText(hourOfDay + ":" + minute);
                            if(hourOfDay==00)
                            {
                                tvpicktime.setText((17) + ":" + minute);

                            }
                            else if(hourOfDay==1)
                            {
                                tvpicktime.setText((18) + ":" + minute );
                            }
                            else if(hourOfDay==2)
                            {
                                tvpicktime.setText((19) + ":" + minute );
                            }
                            else if(hourOfDay==3)
                            {
                                tvpicktime.setText((20) + ":" + minute );
                            }
                            else if(hourOfDay==4)
                            {
                                tvpicktime.setText((21) + ":" + minute);
                            }
                            else if(hourOfDay==5)
                            {
                                tvpicktime.setText((22) + ":" + minute );
                            }
                            else if(hourOfDay==6)
                            {
                                tvpicktime.setText((23) + ":" + minute);
                            }
                            else if(hourOfDay==7)
                            {
                                tvpicktime.setText((00) + ":" + minute);
                            }
                            else if(hourOfDay==8)
                            {
                                tvpicktime.setText((01) + ":" + minute);
                            }
                            else if(hourOfDay==9)
                            {
                                tvpicktime.setText((02) + ":" + minute);
                            }
                            else if(hourOfDay==10)
                            {
                                tvpicktime.setText((03) + ":" + minute);
                            }
                            else if(hourOfDay==11)
                            {
                                tvpicktime.setText((04) + ":" + minute);
                            }
                            else if(hourOfDay==12)
                            {
                                tvpicktime.setText((05) + ":" + minute);
                            }
                            if(hourOfDay==13)
                            {
                                tvpicktime.setText((06) + ":" + minute);
                            }
                            else if(hourOfDay==14)
                            {
                                tvpicktime.setText((07) + ":" + minute );
                            }
                            else if(hourOfDay==15)
                            {
                                tvpicktime.setText((8) + ":" + minute );
                            }
                            else if(hourOfDay==16)
                            {
                                tvpicktime.setText((9) + ":" + minute);
                            }
                            else if(hourOfDay==17)
                            {
                                tvpicktime.setText((10) + ":" + minute );
                            }
                            else if(hourOfDay==18)
                            {
                                tvpicktime.setText((11) + ":" + minute);
                            }
                            else if(hourOfDay==19)
                            {
                                tvpicktime.setText((12) + ":" + minute);
                            }
                            else if(hourOfDay==20)
                            {
                                tvpicktime.setText((13) + ":" + minute);
                            }
                            else if(hourOfDay==21)
                            {
                                tvpicktime.setText((14) + ":" + minute);
                            }
                            else if(hourOfDay==22)
                            {
                                tvpicktime.setText((15) + ":" + minute);
                            }
                            else if(hourOfDay==23)
                            {
                                tvpicktime.setText((16) + ":" + minute);
                            }
                        }
                    }, mHour, mMinute, false);

            timePickerDialog.show();
        }

      }

1 个答案:

答案 0 :(得分:0)

在您的代码中,您要删除7个小时并且您没有更改当天(如果它在凌晨1点,您应该在前一天进行,但无论如何您只需调用此代替您的switch语句:< / p>

$myVariable[0] 

这将增加-6小时,因此它将删除6小时

在文本字段中显示结果

c.add(Calendar.HOUR_OF_DAY, -6);