我将从一个例子开始:时间是20:00(2000),我想找到自09:00(0900)以来已经过多少分钟,我尝试使用60的mod,但这给了我一个休息时间。
示例:
Elapsed time between 1610 1700
1700-1610 = 90, this is obviously wrong
(1700-1610)%60 = 30,
90-30等于60,但答案应该是50.我对我应该做的事情非常困惑,我将如何在java中处理这个问题?谢谢!
答案 0 :(得分:2)
将小时数转换为分钟数。
1700 hours = 17*60 minutes
1610 hours = 16*60 minutes + 10 minutes
找出差异,简单的减法就能完成这项工作
17*60 - 16*60 - 10
更新
假设用户输入0000(hhmm)格式,您可以简单地按大小分割
//psuedo code
String data = userInput;
int hours = Integer.parseInt(data.split(0,2));
int mins = Intger.parseInt(data.split(2,4));
答案 1 :(得分:1)
你混合了两个时间表示
在添加,减去,乘以或除以必须将其转换为数字的时间之前。在你的情况下:
16:10 17:00之间的经过时间
16:10 = 16*60+10 = 970
17:00 = 17*60+00 = 1020
17:00 - 16:10 = 1020 - 970 = 50
在Java中你可以写:
public int minutesFromTime(String time) {
String [] parts = time.split(":");
int hours = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);
return hours * 60 + minutes;
}
然后
System.out.println("Difference is "
+ (minutesFromTime("17:00") - minutesFromTime("16:10"))
+ " minutes");
答案 2 :(得分:0)
你不能这样做,你把小时和分钟混淆为你做同样的方式。 你应该分开小时和分钟来得到好的答案
例如:
String time1 = "1700";
int hour1 = Integer.parseInt(time1.substring(0,2));
int minute1 = Integer.parseInt(time1.substring(2,4));
从int
到String
解析
int timeInt = 1715;
String time1 = ""+timeInt;
int hour1 = Integer.parseInt(time1.substring(0,2));
int minute1 = Integer.parseInt(time1.substring(2,4));
或留在int
int timeInt = 1715;
int hour = timeInt / 100;
int min = timeInt - hour*100;
答案 3 :(得分:0)
要将24小时时间转换为分钟,您需要确定小时和分钟:
int hours = time / 100;
int minutes = time % 100;
然后计算自午夜以来的分钟数:
int minutesSinceMidnight = (hours * 60) + minutes;
您可以减去minutesSinceMidnight两次,以达到分钟的时差。
答案 4 :(得分:0)
你正在处理时间(60分钟),所以你不能直接使用数学函数(10个基数)。 因此,我建议您将数据转换为Date,然后再处理它。
DateFormat dateFormat = new SimpleDateFormat( "HHmm");
Date date = dateFormat.parse("1610");
Date date2 = dateFormat.parse("1700");
long diff = date2.getTime() - date.getTime();
long diffMinutes = diff / (60 * 1000);
答案 5 :(得分:0)
这个简单的例子可以完成这项工作:
public static void main(String[] args) {
String after = "2000";
String before = "0900";
int hours = (parseInt(after) - parseInt(before)) / 100;
int minutes = (parseInt(after) % 100) - (parseInt(before) % 100) % 60;
if (minutes < 0) minutes += 60;
System.out.printf("Minutes passed from %s to %s are %d\n", before, after, hours * 60 + minutes);
}
答案 6 :(得分:0)
如果我们假设用户将始终以XXXX(hhmm)格式输入,您可以尝试使用此代码:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter first time value: ");
int firstTime = userInput.nextInt();
System.out.print("Enter second time value: ");
int secondTime = userInput.nextInt();
int hoursFirst = (int)(firstTime/100);
int minutesFirst = firstTime%100;
int hoursSecond = (int)(secondTime/100);
int minutesSecond = secondTime%100;
int difference = Math.abs((hoursSecond - hoursFirst - 1) * 60 + (60 - minutesFirst) + minutesSecond);
System.out.println("Difference between " + hoursFirst + ":" + minutesFirst
+ " and " + hoursSecond + ":" + minutesSecond + " is " + difference + " minutes.");
}
输出示例:
Enter first time value: 1820
Enter second time value: 1610
Difference between 18:20 and 16:10 is 130 minutes.