开发者

How to convert Time in String format ("12:45" or it can be "725:00" ) to minutes in java?

i trying to convert time in string format to minutes. i can use substring and find the minutes but there is a problem because there is a chance of getting time in the format 720:00 ho开发者_StackOverflow中文版urs. (as i am calculating time for many days) pls help with me.... Thanks in advance........

Hussain


As far as I understood, you need to calculate how many minutes are in the given hours and minutes. Well, I'd do it fast and simple:

String time = "725:00";
String[] parts = time.split(":",2);
int hours = Integer.valueOf(parts[0]);
int minutes = Integer.valueOf(parts[1]);
return hours*60+minutes;


With Groovy, you could do something like this:

def time = '720:00'
def minutes = time.split(':').with {
  (h,m) = it as int[]
  h * 60 + m
}


In Java use String.split(":") and take the second element of the array.

Assuming that the time is in "hour:minute" format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜