java分钟转化为小时分钟,使用Java将x小时y分钟转换为z分钟

这篇博客讨论了如何使用Java将Google Maps提供的ETA(预计到达时间)从小时-分钟格式转换为纯分钟值。文章提供了一个解决方案,通过拆分字符串并根据时间单位(小时、分钟、天)进行计算,将例如4小时34分钟这样的值转换成分钟。还提到了如果可以将查找值放入映射中,可以进一步简化代码。
摘要由CSDN通过智能技术生成

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

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.

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

}

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值