简体   繁体   English

计算两个小时的字符串之间的差异

[英]Calculate difference between two hour's strings

I have a string that contain certain hour ex. 我有一个包含某些小时前的字符串。 14:34 , and now I want to calculate the difference between the current hour ex. 14:34 ,现在我想计算当前小时前的时差。 21:36-14:34 =7 hours 2 minutes (or something like that.) Can someone explain me how can I do that? 21:36-14:34 = 7小时2分钟(或类似的时间。)有人可以向我解释我该怎么做?

It's very easy: You need to separate the string in terms you can add or substract: 这很简单:您需要使用可以添加或减去的术语来分隔字符串:

   String timeString1="12:34";
   String timeString2="06:31"; 

   String[] fractions1=timeString1.split(":");
   String[] fractions2=timeString2.split(":");
   Integer hours1=Integer.parseInt(fractions1[0]);
   Integer hours2=Integer.parseInt(fractions2[0]);
   Integer minutes1=Integer.parseInt(fractions1[1]);
   Integer minutes2=Integer.parseInt(fractions2[1]);      
   int hourDiff=hours1-hours2;
   int minutesDiff=minutes1-minutes2;
   if (minutesDiff < 0) {
       minutesDiff = 60 + minutesDiff;
       hourDiff--;
   }
   if (hourDiff < 0) {
       hourDiff = 24 + hourDiff ;
   }
   System.out.println("There are " + hourDiff + " and " + minutesDiff + " of difference");

UPDATE: 更新:

I'm rereading my answer and I'm surprised is not downvoted. 我正在重新阅读我的答案,但我感到惊讶的是我没有被否决。 My fault. 我的错。 I wrote it without any IDE check. 我写它时没有任何IDE检查。 So, the answer should be minutes1 and 2 for the minutesDiff and obviously and a check to carry the hour difference if the rest of minutes is negative, making minutes (60+minutesDiff). 因此,答案应该是minutesDiff的minutes 1和2,并且显然应该是剩余的分钟数是否定的,如果小时数为负,则进行检查以携带小时差,从而得出分钟数(60 + minutesDiff)。 If minutes is negative, rest another hour to the hourDiff. 如果分钟为负数,则将小时休息一小时。 If hours become negative too, make it (24+hourDiff). 如果小时数也变为负数,则设为(24 + hourDiff)。 Now is fixed. 现在是固定的。

For the sake of fastness, I'm using a custom function. 为了牢固起见,我使用了自定义函数。 For the sake of scalability, read Nikola Despotoski answer and complete it with this: 为了实现可伸缩性,请阅读Nikola Despotoski的答案并通过以下步骤完成它:

System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.println(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");

I would start by using the .split method to get the string into its two components (minutes and hours) then I would convert both times into minutes by mutliplying the hours by 60 and then adding the minutes 我将首先使用.split方法将字符串分为两个部分(分钟和小时),然后将小时数乘以60,然后再加上分钟,从而将两个时间都转换为分钟

String s = "14:34";
String[] sArr = s.split(",");
int time = Integer.parseInt(sArr[0]);
time *= 60;
int time2 = Integer.parseInt(sArr[1]);
time = time + time2;

do this for both strings and then subtract one from the other. 对两个字符串都执行此操作,然后从另一个字符串中减去一个。 You can convert back to normal time by using something like this 您可以使用以下方法将时间恢复为正常时间

int hours = 60/time;
int minutes = 60%time;

The answer labeled as correct will not work. 标记为正确的答案将不起作用。 It does not account for if the first time is for example 3:17 and the second is 2:25. 它不考虑第一次是例如3:17,第二次是2:25。 You end up with 1 hour and -8 minutes! 您最终将花费1小时-8分钟!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM