简体   繁体   中英

How to set date and time 6 hour before to picked time and date?

I am developing an app, here I want to use Date and time picker, To pick a date and time but I want to set result time 6 hours before picked time. How do I set this time 6 hour before the pick time with appropriate date with respect to time? Example: if i am pick date suppose "30sep" and time is "1:00am" then date and time I would like to display will be "29sep" and "7:00pm". How do I do? I am using following code:

 @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();
        }

      }

In your code you are removing 7 hours and you are not changing the day (if it's 1 AM, you should go in the previous day, but anyway you can simply call this instead of your switch statement:

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

This will add -6 hours so it will remove 6 hours

To show result in a textfield

SimpleDateFormat simpleDate =  new SimpleDateFormat("dd/MM/yyyy");
myTextField.setText(simpleDate.format(c.getTime()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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