使用Java将x小时y分钟转换为z分钟 [英] Convert x hour(s) y min(s) into z minutes using Java

查看:563
本文介绍了使用Java将x小时y分钟转换为z分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以 x小时y分钟的格式获取了某些路线的google maps ETA(即持续时间),如果x > 24,则此格式更改为 u天v小时

I have fetched google maps ETAs (that is, durations) for some routes in the format of x hour(s) y min(s), and also if x > 24 then this format changes into u day(s) v hour(s).

现在我要将这些值与其他一些ETA进行比较,因此我要做的就是将这些格式值转换为仅分钟数的值。

Now I want to compare these values to some other ETAs so all I need to do is convert these format value into a minutes-only value.

例如,我的值是4小时34分钟,我想使用Java将其更改为分钟,或将1小时1分钟更改为分钟,有些记录中的小时表示为1小时和3小时,分钟和天相同。

Such as I have a value as 4 hours 34 mins, I want to change it into minutes using Java or such as 1 hour 1 min to minutes, there are records where hour indicated as 1 hour and 3 hours and same for mins and days.

推荐答案

您可以在空间上拆分它...

You could split it on space...

String s[]="3 hours 23 mins".split(" ")

然后我将所有内容标准化为分钟

Then I'd just normalize everything to minutes

int minutes=0;
for(int i=0;i<=2;i+=2) {
   if(s[i+1].startsWith("min"))
       minutes+=s[i]
   if(s[i+1].startsWith("hour"))
       minutes+=s[i]*60
   if(s[i+1].startsWith("day"))    
       minutes+=s[i]*60*24
}

如果您将查找值放入地图中,则可以从该循环中剪切2/3,我会在Groovy中这样做,因为它的附加语法很少,但是在Java中,如果添加了尴尬,则为50/50降低了冗余,值得使用。

If you were to put the lookup values into a map you could cut 2/3 from that loop, I'd do it that way in Groovy where the additional syntax would be minimal but in java it's a 50/50 if the added awkwardness loweres the redundancy enough to be worth it.

这篇关于使用Java将x小时y分钟转换为z分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆