How to format the result of a date?


I have developed a code where I put in an EditText the time after selecting it, with this following code the problem I have is when I select a time like 05:08 I get in the EditText 5: 8 (because it is in AM) but If I was in PM, the same time I got bounced: 17: 8. and also it fulfills everything well when the chosen time is 17:54 for example. thanks!

 public void metodo_horaSalida(View v){
    final Calendar c= Calendar.getInstance();

    TimePickerDialog timePickerDialog =new TimePickerDialog(this, 3,new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Here is a good example

But when choosing this time in PM

Boot like this: This is in PM  

Boot like this: This is in AM  

asked by Pedro Narrea 21.12.2018 в 18:58

1 answer


Get the values and add them to your TextView or editText in this way assigning a format which would ensure always showing two digits:

ethorasalida.setText(String.format("%02d:%02d", hourOfDay, minute));

in this way, instead of showing:


would show

answered by 21.12.2018 / 19:07