Can't format time from Time Picker

577 Views Asked by At

Currently trying to get two different values from the time picker reminderHour, which is the selectedHour and reminderMin which is the selectedMin. Thought I had it working perfectly but found another error this morning.

eReminderTime.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Calendar mcurrentTime = Calendar.getInstance();
                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                int minute = mcurrentTime.get(Calendar.MINUTE);
                TimePickerDialog mTimePicker;
                mTimePicker = new TimePickerDialog(AddPayment.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {



                        reminderHour = selectedHour;
                        reminderMin = selectedMinute;


                        if (selectedHour < 10)
                        {
                            if(selectedMinute == 0)
                            {
                                eReminderTime.setText("0" + selectedHour + ":00" + selectedMinute);
                            }
                            else if(selectedMinute < 10)
                            {
                                eReminderTime.setText("0" + selectedHour + ":0" + selectedMinute);
                            }
                            else
                            {
                                eReminderTime.setText("0" + selectedHour + ":" + selectedMinute);
                            }
                        }

                        if (selectedHour == 0)
                        {
                            if(selectedMinute == 0)
                            {
                                eReminderTime.setText("0" + selectedHour + ":00" + selectedMinute);
                            }
                            else if(selectedMinute < 10)
                            {
                                eReminderTime.setText("0" + selectedHour + ":0" + selectedMinute);
                            }
                            else
                            {
                                eReminderTime.setText("0" + selectedHour + ":" + selectedMinute);
                            }
                        }

                        if(selectedMinute == 0)
                        {
                            eReminderTime.setText("" + selectedHour + ":00" + selectedMinute);
                        }
                        if(selectedMinute < 10)
                        {
                            eReminderTime.setText("" + selectedHour + ":0" + selectedMinute);
                        }
                        else
                        {
                            eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
                        }


                    }
                }, hour, minute, true);

                mTimePicker.setTitle("Select Time");
                mTimePicker.show();

            }
        });

That's the code for my Time Picker. I don't want the time joined up all together because I need the hour value stored separately from the minute value.

I tried looking up SimpleDateFormat but I couldn't get it working. This is the code I tried

                    SimpleDateFormat simpleHour = new SimpleDateFormat("kk");
                    SimpleDateFormat simpleMin = new SimpleDateFormat("mm");

                    formatHour = simpleHour.format(selectedHour);
                    formatMin = simpleMin.format(selectedMinute);

                    reminderHour = Integer.parseInt(formatHour);
                    reminderMin = Integer.parseInt(formatMin);

                    Toast.makeText(getApplicationContext(), formatHour, 0).show();
                    Toast.makeText(getApplicationContext(), formatHour, 0).show();
2

There are 2 best solutions below

3
On

That's because individual hh and mm aren't valid formats. Try to combine the minute and hour values together and when you receive the combined value, seperate them via split()

e.g;

 SimpleDateFormat simpleHour= new SimpleDateFormat("hh:mm");
 String strTime = sdf.format(new Date());
 String[] pieces=strTime.split(":"); 
 Log.i("Hour: ",pieces[0]);
 Log.i("Miniute: ",pieces[1]);
3
On

What output do you want exactly? If it's just something like:

01:32

Then it's simple to generate a string yourself rather than worrying about date formats etc.

//Add leading zeros if required
String hours = (String) ((selectedHour < 10) ? "0" + selectedHour 
                                             : Integer.toString(selectedHour));
String minutes = (String) ((selectedMinute < 10) ? "0" + selectedMinute 
                                             : Integer.toString(selectedMinute));
String prettyTime = hours + ":" + minutes;