levonbars
levonbars

Reputation: 11

Making time string

Is it possible to make a string with 2 double digit numbers with ":" between them so it will look like 04:02? I tried this but it didn't work:

int h = 4;
int m = 2;
String time = new String(h+":"+m);    
time = String.format("%02d",h+":"+m);    

Upvotes: 0

Views: 33

Answers (1)

David.Jones
David.Jones

Reputation: 1411

Change

time = String.format("%02d",h+":"+m);  

to

time = String.format("%02d:%02d", h, m);

Also, you can combine line 3/4 into

String time = String.format("%02d:%02d", h, m);  

Run the code here

Upvotes: 3

Related Questions