从 DatePicker 和 TimePicker 获取时间戳

Get timestamp from DatePicker AND TimePicker

我有一个应用程序,用户可以在其中使用 DatePicker 和 TimePicker 设置日期和时间。我正在尝试将信息保存为时间戳。我目前正在根据用户选择的日期获取时间戳(请参阅下面的代码):

                Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
                final Calendar c = cal.getInstance();
                int mYear = c.get(Calendar.YEAR); // current year
                int mMonth = c.get(Calendar.MONTH); // current month
                int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                // date picker dialog
                DatePickerDialog datePickerDialog = new DatePickerDialog(AddEventActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                // set day of month , month and year value in the edit text
                                txtDate.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);
                                c.set(year, monthOfYear, dayOfMonth);
                                timestamp = c.getTimeInMillis();
                            }

这很好用,但由于我没有给出时间,所以它将时间设置为我创建项目的时间。 例如,假设我在 27/4/2019 19:00 设置了一个事件,并将其保存在 13:30,记录的时间戳记为 27/4/2019 在 13:30.

我的 TimePicker 的代码如下:

        txtTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar mcurrentTime = Calendar.getInstance();
                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                int minute = mcurrentTime.get(Calendar.MINUTE);

                TimePickerDialog mTimePicker;
                mTimePicker = new TimePickerDialog(AddEventActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                        //adding extra 0 to the front of 1 digit hours and minutes so format is consistent.
                        String hourFormat = String.format("%02d", selectedHour);
                        String minFormat = String.format("%02d", selectedMinute);
                        txtTime.setText(hourFormat + ":" + minFormat);
                    }
                }, hour, minute, true);
                mTimePicker.show();
            }
        });

有没有办法结合所选的日期和时间来创建准确的时间戳?

我在获取日期时遇到了类似的问题,所以我所做的是创建一个字符串,我用它来选择当前日期和时间,然后将其合并以创建一个完美的时间戳,就像这样

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(ServiceManDetails.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                String month,day;
                                if(monthOfYear<9){
                                    month="0"+ (monthOfYear + 1);
                                    if(dayOfMonth<10){
                                        day="0"+dayOfMonth;
                                    }else{
                                        day=dayOfMonth+"";
                                    }
                                }else{
                                    month= (monthOfYear + 1)+"";
                                    if(dayOfMonth<10){
                                        day="0"+dayOfMonth;
                                    }else{
                                        day=dayOfMonth+"";
                                    }
                                }
                                //String selectedday=dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;

                                pickedDate=year + "/" + month + "/" + day;



                                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(ServiceManDetails.this,
                                        new TimePickerDialog.OnTimeSetListener() {

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

                                                String hr,min;
                                                if(hourOfDay<10){
                                                    hr="0"+hourOfDay;
                                                    if(minute<10){
                                                       min="0"+minute;
                                                    }else{
                                                        min=minute+"";
                                                    }
                                                }else{
                                                    hr=hourOfDay+"";
                                                    if(minute<10){
                                                        min="0"+minute;
                                                    }else{
                                                        min=minute+"";
                                                    }
                                                }
                                                time=hr + ":" + min + ":00";
                                                date=pickedDate+" "+time;

                                                scheduleDialog(date);
                                            }
                                        }, mHour, mMinute, true);
                                timePickerDialog.show();

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

我也想设置毫秒所以我设置日期自动设置秒为 00 你也可以添加毫秒为 000

有一个 class 级别的 Calendar 实例,让我们按照您之前的方式实例化它:

Calendar combinedCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

因此,当您 select 约会时,请像您一样设置值:

combinedCal.set(year, monthOfYear, dayOfMonth);

当您 select 时间时,只需将 selected 小时和分钟设置为同一实例:

combinedCal.set(Calendar.HOUR_OF_DAY, selectedHour);
combinedCal.set(Calendar.MINUTE, selectedMinute);

时间戳包含您从日期选择器和时间选择器中选择的日期和时间

    timepicker = (Timepicker)findViewById(R.id.timepicker);
    datepicker = (Datepicker)findViewById(R.id.datepicker);

    int year = datepicker.getYear();
    int month = datepicker.getMonth();
    int day = datepicker.getDay();
    int hour= timepicker.getCurrentHour();
    int minute = timepicker.getCurrentMinute();


    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    Long timestamp = calendar.getTimeInMills();


    System.out.println(timestamp);