user1629045
user1629045

Reputation: 43

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());

Upvotes: 4

Views: 4193

Answers (4)

Rohini
Rohini

Reputation: 21

you can format the date using simple formatter it will return the value in double digit

            val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val date = formatter.parse(start)
            var output = ""
            val formatter1 = SimpleDateFormat("hh:mm")
            output = formatter1.format(date)

Upvotes: 0

Juan Labrador
Juan Labrador

Reputation: 1214

You can do this...

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

inside TimePicker

Upvotes: 0

Gautam Pansheriya
Gautam Pansheriya

Reputation: 1

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;
    }

Upvotes: 0

xandy
xandy

Reputation: 27411

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);

Upvotes: 11

Related Questions