0

I am currently using a TimePickerDialog to set the time and display it into a Edit Text .However, I am having problems with the formating.

I want to set the time in HH:MM format but it is set like H:mm format i want this 01:33 but my code giving me 1:33 output.

Here is the code :-

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

        String am_pm = "";

        Calendar datetime1 = Calendar.getInstance();
        datetime1.set(Calendar.HOUR_OF_DAY, hourOfDay);
        datetime1.set(Calendar.MINUTE, minute);

        if (datetime1.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (datetime1.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";

        String strHrsToShow1 = (datetime1.get(Calendar.HOUR) == 00)?"12":datetime1.get(Calendar.HOUR)+"";

        ((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow1+":"+datetime1.get(Calendar.MINUTE)+" "+am_pm);

    }
Andie
  • 96
  • 3
  • 21
  • may be not because that question not giving me the answer. – Andie Apr 21 '17 at 10:19
  • Try `((EditText)getActivity().findViewById(R.id.End_time)).setText( String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE)) +" "+am_pm);` – Dejvid Apr 21 '17 at 10:21
  • So have you tried the code in that answer? What happened? This definitely looks like a duplicate to me: in both cases, the code in the question just converts the integer to the string directly (so it won't be 0-padded) and the answer should either be to apply zero-padding yourself (to minutes as well as hours) or to use date/time formatting to do it for you. – Jon Skeet Apr 21 '17 at 10:26
  • @Dejvid giving me error like java.util.IllegalFormatConversionException: d != java.lang.String – Andie Apr 21 '17 at 10:31
  • @WealInfotech Probably because `datetime1.get(Calendar.MINUTE)` returns int instead of string. Try `((EditText)getActivity().findViewById(R.id.End_time)).setTex‌​t( String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE) + "") +" "+am_pm);` – Dejvid Apr 21 '17 at 10:37
  • @JonSkeet i tried that question's answer but none are helping me so that i put the question here. – Andie Apr 21 '17 at 10:41
  • @Dejvid giving me the same error – Andie Apr 21 '17 at 10:41
  • You should say exactly what you tried and what happened then. I very much doubt that the accepted answer on that question would give you the same behaviour. – Jon Skeet Apr 21 '17 at 10:56
  • @JonSkeet i tried that answer and giving me the same error java.util.IllegalFormatConversionException: d != java.lang.String – Andie Apr 21 '17 at 11:07
  • So will you please unmark my question from duplicate?@JonSkeet – Andie Apr 21 '17 at 11:12
  • No, because you should show exactly what you tried in the question. It sounds like you're trying to format values which are already strings, instead of formatting the integers. If you **show that code** we can help you fix it. It really *is* the same problem and it really *does* have the same solution, you've just not applied that solution properly. – Jon Skeet Apr 21 '17 at 11:45
  • okay @JonSkeet i got it... – Andie Apr 21 '17 at 11:50

1 Answers1

1

Put this line:

txtTime1.setText(String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE)));

Instead of:

((EditText)getActivity().findViewById(R.id.End_time)).setText( strHrsToShow1+":"+datetime1.get(Calendar.MINUTE)+" "+am_pm);
Fady Saad
  • 1,169
  • 8
  • 13
  • java.util.IllegalFormatConversionException: d != java.lang.String giving error like this – Andie Apr 21 '17 at 10:31
  • String tm = new SimpleDateFormat("HH:mm").format(new Date(time)); – sapan ravani Apr 21 '17 at 10:50
  • @sapanravani "56" what is (new Date(time))? – Andie Apr 21 '17 at 10:53
  • Date is inbuilt class in android..you have to pass time as a parameter and it will convert to the format passed in SimpleDateFormat("HH:mm"). – sapan ravani Apr 21 '17 at 10:55
  • @sapanravani please explain with some code 56 – Andie Apr 21 '17 at 11:14
  • but its a DatePicker and i am using TimePicker see my question and why don't you writing the answer instead of commenting? – Andie Apr 21 '17 at 11:21
  • just want to add one line int hour = hourOfDay % 12; ((EditText) getActivity().findViewById(R.id.End_time)).setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour, minute, hourOfDay < 12 ? "am" : "pm")); – Andie Apr 21 '17 at 11:36