How to display two numbers per minute in android

I am trying to display the “two numbers” of a minute in my code using Android TimePicker. But I still do not understand ... Time is displayed only in this format X: X. For example, 9:05 will be shown in my application 9: 5.

Can anybody help me?

This is my code ...

idtime.setText(new StringBuilder() .append(String.valueOf(mHour)).append(":") .append(String.valueOf(mMinute)).toString()); 
+4
source share
4 answers

Use SimpleDateFormat .

Example:

Suppose you show the current time:

 Date date = Calendar.getInstance().getTime(); SimpleDateFormat sdf = new SimpleDateFormat("HH:MM"); String output = sf.format(date).toString(); idtime.setText(output); 

Another easy way to make a null padding, you can use String.format :

 String output = String.format("%02d:%02d", mHour, mMinute); idtime.setText(output); 
+9
source

You can do it...

 if (minute < 10) { hour.setText(hour + ":0" + minute); } else { hour.setText(hour + ":" + minute); } 

inside TimePicker

+1
source
 public static String getDuration(long milliseconds) { long sec = (milliseconds / 1000) % 60; long min = (milliseconds / (60 * 1000))%60; long hour = milliseconds / (60 * 60 * 1000); String s = (sec < 10) ? "0" + sec : "" + sec; String m = (min < 10) ? "0" + min : "" + min; String h = "" + hour; String time = ""; if(hour > 0) { time = h + ":" + m + ":" + s; } else { time = m + ":" + s; } return time; } 
0
source

You can format the date with a simple formatter, it will return the value as a two-digit number

  val date = formatter.parse(start) var output = "" val formatter1 = SimpleDateFormat("hh:mm") output = formatter1.format(date) 
0
source

All Articles