0

After picking a time from a TimePicker I am displaying that using setText() , however , so far everything works well, however the problem is , if I choose 05:09 , I only get displayed 5:9 PM in TextView. I miss the preceding zeros. But I require that

Here is the Code I use :

public void onTimeSet(TimePicker timePicker,int selectedHour, int selectedMinute) 
{
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
    calendar.set(Calendar.MINUTE, selectedMinute);

    int c_hour,c_min;
    String format;
    c_hour=selectedHour;
    c_min=selectedMinute;

    if (c_hour == 0) {
          c_hour += 12;
          format = "AM";
    } 
    else if (c_hour == 12) {
          format = "PM";
    } 
    else if (c_hour > 12) {
          c_hour -= 12;
          format = "PM";
    } 
    else {
         format = "AM";
    }

TextView dimple = (TextView)findViewById(R.id.timeText);
dimple.setText(new StringBuilder().
            append(c_hour).append(" : ").append(c_min).append(" ").append(format));

What is the quick way to fix it ?

Suraj Palwe
  • 2,080
  • 3
  • 26
  • 42
OBX
  • 6,044
  • 7
  • 33
  • 77
  • You are not doing any calculations on the `c_min` `int` variable, so you can make it string. or you could convert it to string at the end and then if the length is 1 add a preceeding 0 – Rohit Vipin Mathews May 28 '15 at 11:49
  • This is because you use `variables` as `int` just make it `string` and when accessing it convert it to the `int` using parsing of the string method. – Suraj Palwe May 28 '15 at 11:49
  • see if you get help at http://www.mkyong.com/android/android-time-picker-example/ – Mithun May 28 '15 at 11:53

3 Answers3

5

You can get the time, by prefixing zeros.

String formatTime=String.format("%02d : %02d", hour, minute);

or

Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        String formatTime = new SimpleDateFormat("hh:mm a").format(cal.getTime());
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • clean line of code , +1 to that , But how to add AM and PM to that ? So I can call the formatTime from setText – OBX May 28 '15 at 11:56
  • @oblivion, you could use the following code: `String formatTime = String.format("%02d : %02d %s", hour, minute, format);` – Rajesh May 28 '15 at 12:02
1

You can use SimpleDateFormat for constructing time string as mentioned here:

Convert java.util.Date to String

In your case you can use "HH:mm a" as the format.

So your sample code should look like:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);

DateFormat dateFormat = new SimpleDateFormat("HH:mm a");

Date dateValue = calendar.getTime();
String requiredTime = dateFormat.format(dateValue);
TextView dimple = (TextView)findViewById(R.id.timeText);
dimple.setText(requiredTime);

Hope it helps :)

Community
  • 1
  • 1
Megh Vidani
  • 635
  • 1
  • 7
  • 22
0
String str_hour = c_hour<10 ? "0"+String.valueOf(c_hour):String.valueOf(c_hour);   

String str_min = c_min<10 ? "0"+String.valueOf(c_min):String.valueOf(c_min);  
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
2red13
  • 11,197
  • 8
  • 40
  • 52