3

I am using a timepicker to choose the time and then show it in a toast in 24 hour format. I am getting output in toast like "2:6" And I want to get it in proper time format like "02:06". How can I do that....

public void ShowTimePicker(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

}


public static class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();

        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

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

        Toast myToast = Toast.makeText(getContext(), hourOfDay + ":" + minute, Toast.LENGTH_SHORT);
        myToast.show();


    }
}                                      
Waqas Khan
  • 55
  • 1
  • 10
  • 1
    Possible duplicate of [How to format longs in android to always display two digits](http://stackoverflow.com/questions/20293887/how-to-format-longs-in-android-to-always-display-two-digits) – NSimon Jun 01 '16 at 10:04

8 Answers8

14

First need to create one function that check your input and convert it in String as per condition.

public String convertDate(int input) {
  if (input >= 10) {
    return String.valueOf(input);
  } else {
    return "0" + String.valueOf(input);
  }
}

Then you can call like this

txtTime.setText(convertDate(hourOfDay) + ":" + convertDate(minute));

And your Toast will be like this

Toast myToast = Toast.makeText(getContext(), convertDate(hourOfDay) + ":" + convertDate(minute), Toast.LENGTH_SHORT);
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
10

int is just an integer. Integers don't have leading zeros. They can only be displayed with leading zeros when you convert them to String objects. One way to do that is like this:

int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

String curTime = String.format("%02d:%02d", hour, minute);
Aown Raza
  • 2,023
  • 13
  • 29
2

Try this

private String fillText(int i){
    return i>9?i+"":"0"+i;
}

...

 Toast myToast = Toast.makeText(getContext(), fillText(hourOfDay) + ":" + filllText(minute), Toast.LENGTH_SHORT);
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
Mark Shen
  • 346
  • 1
  • 5
2

You can use SimpleDateFormat for date and time formatting:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); 
String string = sdf.format(callendar);

Information about time patterns you can find here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

1

You can Write this

  Toast myToast = Toast.makeText(getContext(), hourOfDay <10? "0"+hourOfDay :hourOfDay  + ":" + minute<10:"0"+minute:minute, Toast.LENGTH_SHORT);
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
1

You just need to use the String.format as follow :

String format = "%1$02d"; // two digits
Toast.makeText(getContext(), String.format(format, hourOfDay)+ ":" + String.format(format, minute, Toast.LENGTH_SHORT).show();
NSimon
  • 5,212
  • 2
  • 22
  • 36
1

Add this string ressource in your strings.xml

<string name="time_remaining">%1$02d:%2$02d</string>

In your onTimeSet function you can use it as follow:

String time = getActivity().getResources().getString(R.string.time_remaining, hourOfDay, minute);
Toast myToast = Toast.makeText(getContext(), time, Toast.LENGTH_SHORT);
myToast.show();
thushcapone
  • 162
  • 5
0

Kotlin v.1.4.x

This is an extension of Int:

fun Int.to2DString():String{
    return String.format("%02d",this)
}

Usage:

val formattedMinute = timePicker.minute.to2DString()
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133