Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display two numbers in minute in android

I´m trying to display the "two numbers" of minute in my code with an Android TimePicker. But I didn´t get it yet... The time is only displayed in this format X:X. For example 9:05 will show in my app 9:5.

Can anyone help me please?

This is my code...

idtime.setText(new StringBuilder()
         .append(String.valueOf(mHour)).append(":")
         .append(String.valueOf(mMinute)).toString());
like image 577
user1629045 Avatar asked Dec 20 '22 17:12

user1629045


1 Answers

Use SimpleDateFormat.

Example:

Suppose you are displaying the current time:

Date date = Calendar.getInstance().getTime();

SimpleDateFormat sdf = new SimpleDateFormat("HH:MM");
String output = sf.format(date).toString();

idtime.setText(output);

Another easier method to do zero-padding, you can use String.format:

String output = String.format("%02d:%02d", mHour, mMinute);
idtime.setText(output);
like image 75
xandy Avatar answered Jan 03 '23 02:01

xandy