Search code examples
javaandroidtimestamptimepicker

Get timestamp from DatePicker AND TimePicker


I have an app in which a user sets a date and time using a DatePicker and a TimePicker. I am trying to save the information as a timestamp. I am currently getting the timestamp based on the date picked by the user (see code below):

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

This works fine, but since I don't give a time, it sets the time to the time I create the item. For example, say I set an event on 27/4/2019 at 19:00, and save it at 13:30, the timestamp recorded reads 27/4/2019 at 13:30.

The code for my TimePicker is below:

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

Is there a way to combine the date and time selected to create an accurate timestamp?


Solution

  • Have a class level Calendar instance and let's instantiate it the way you did it earlier:

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

    So when you select date, set value as you do:

    combinedCal.set(year, monthOfYear, dayOfMonth);
    

    When you select time, just set selected hours and minutes to the same instance:

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