如何根据时间变化[android]在日历中取回日期?

时间:2016-08-31 11:47:40

标签: android mysql date

我正在开发一个涉及机票预订的Android应用程序。不知何故,我正在导入Google日历和时钟[使用Android Studio v1.5中提供的默认版本]。 我的机票预订条件包括在选择取件日期和时间时减去时间和日期,以避免将来出现任何问题。 现在在这种情况下,我想改变以下事项:

反向时间:当用户选择特定时间[在24小时时钟]时,它为该用户提供新的拾取时间,该时间小于拾取时间的7小时。即如果我在下午2点选择我的时间,它应该在同一天的上午9点显示。我以某种方式设法应用每小时减去7小时的条件。但是我怎么能这样做,因为给一天中的每个小时都有条件是很头疼的。如果有解决方案吗?

更新:我找到了解决方案,但问题是,我选择AM时间显示时间 - [负]。

现在第二个问题是,如果有人在凌晨2点的任何一个月的第一天预订机票,我怎样才能将他的日期更改为上一个日期?因为我从取件日期减去7小时

例如 [我期待的正确条件] :如果有些用户在3月1日选择2AM,那么他/她的接送时间和日期应该是2月28日晚上9点。

请建议..!我附上了我正在使用的代码。

public class MainActivity extends AppCompatActivity implements
        View.OnClickListener {

    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime;
    private int mYear, mMonth, mDay, mHour, mMinute;
    TextView tvpick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnDatePicker = (Button) findViewById(R.id.btn_date);
        btnTimePicker = (Button) findViewById(R.id.btn_time);
        txtDate = (EditText) findViewById(R.id.in_date);
        txtTime = (EditText) findViewById(R.id.in_time);
        tvpick = (TextView) findViewById(R.id.tvpicktime);

        btnDatePicker.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v == btnDatePicker) {

            // 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) {

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

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }


    if (v == btnTimePicker) {

        // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
        //    c.add(Calendar.HOUR_OF_DAY, -6);
        //    Date picktime= c.getTime();
        //   tvpick.setText(String.valueOf(picktime));

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

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

                        txtTime.setText((hourOfDay) + ":" + minute);
                    //    tvpick.setText((hourOfDay+(-6)) + ":" + minute);

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

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

1 个答案:

答案 0 :(得分:1)

开始:Java日历对象很糟糕。话虽如此......你必须在这种情况下使用它们。

要从任何日期减去时间,只需执行以下操作:

    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, -7);
    date.setTime(calendar.getTimeInMillis());

这个输出可以是'date'对象减去7小时的日期/时间。这包括处理切换月和年份。即使是闰年!

要明确这就是我如何处理这个问题。这真的取决于你开始使用的日期格式与你想要的结果。