Java convert any integer to 4 digits -


this seems easy question. 1 of assignments sends time in military format (like 1200, 2200, etc) class.

how can force integer converted 4 digits when it's received class? example if time being sent 300, should converted 0300.

edit: turns out didnt need problem had compare values. thanks

as simple that:

string.format("%04d", 300) 

for comparing hours before minutes:

int time1 =  350; int time2 = 1210; // int hour1 = time1 / 100; int hour2 = time2 / 100; int comparationresult = integer.compare(hour1, hour2); if (comparationresult == 0) {     int min1 = time1 % 100;     int min2 = time2 % 100;     comparationresult = integer.compare(min1, min2); } 

note:

integer.compare(i1, i2) has been added in java 1.7, previous version can either use integer.valueof(i1).compareto(i2) or

int comparationresult; if (i1 > i2) {     comparationresult = 1; } else if (i1 == i2) {     comparationresult = 0; } else {     comparationresult = -1; } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -