Java 从24小时算起有几分钟?

Java 从24小时算起有几分钟?,java,math,Java,Math,我将从一个例子开始:时间是20:00(2000),我想知道从09:00(0900)到现在已经有多少分钟了,我尝试使用mod 60,但这给了我一个休息时间 例如: Elapsed time between 1610 1700 1700-1610 = 90, this is obviously wrong (1700-1610)%60 = 30, 90-30等于60,但答案应该是50。我很困惑我应该做什么,我该如何用java处理这个问题?谢谢 将小时转换为分钟 1700 hours = 17*60

我将从一个例子开始:时间是20:00(2000),我想知道从09:00(0900)到现在已经有多少分钟了,我尝试使用mod 60,但这给了我一个休息时间

例如:

Elapsed time between 1610 1700
1700-1610 = 90, this is obviously wrong
(1700-1610)%60 = 30,

90-30等于60,但答案应该是50。我很困惑我应该做什么,我该如何用java处理这个问题?谢谢

将小时转换为分钟

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

你不能这样做,你在做这件事的时候把时间和分钟都当成了同一个单位。 你应该分秒必争才能得到好答案

例如:

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;

你把时间的两种表现形式混合在一起

  • 13:50
  • 1350(分钟)
13:50是13小时50分钟,但1350是1350/60=22小时,1350%60=30分钟

在你们能加、减、乘或除时间之前,你们必须把它转换成数字。就你而言:

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;
}
然后


要将24小时时间转换为分钟,您需要确定小时和分钟:

int hours = time / 100;
int minutes = time % 100;
然后计算午夜后的分钟数:

int minutesSinceMidnight = (hours * 60) + minutes;
您可以减去这两个时间的分钟数,得出以分钟为单位的时差。

您处理的是时间(60分钟),因此不能直接使用数学函数(以10为基数)。 因此,我建议您将数据转换为日期,然后进行处理

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

这个简单的例子应该可以完成这项工作:

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);
}
publicstaticvoidmain(字符串[]args){
=“2000”之后的字符串;
=“0900”之前的字符串;
整小时=(parseInt(之后)-parseInt(之前))/100;
int分钟=(parseInt(在%100之后)-(parseInt(在%100之前))%60;
如果(分钟<0)分钟+=60;
System.out.printf(“从%s传递到%s的分钟数为%d\n”,在小时数*60+分钟之前、之后);
}

如果我们假设用户总是以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.

谢谢你的回答,如果这是一个用户输入的,而我对《泰晤士报》一无所知,有没有办法将其拆分?然而,他们总是遵循XXXXformat@user3488485-String.valueOf(intXXXX).subString()将帮助您。您错过了一个
String data=userInput
@CladClad的末尾,它是伪代码。不管怎样,我纠正了它:)使用joda库中的duration/period和相应的解析器。我回到了问题。如果你想删除你的问题,有一个单独的按钮。但要小心,删除被否决的问题可能会让你在提出新问题时遇到麻烦。如果您在更新/更改问题时需要更多帮助,您可以随时询问。谢谢,不知怎的,您没有想到。正如我刚才所说的,我怎样才能在第二个数字后拆分为整数呢?由于输入将始终是ABCD,并且由于我需要分别处理它们,所以我需要AB和CD。谢谢没问题;)我更新了以再次解析youThanks的字符串。假设输入是int而不是字符串,这仍然可能吗?谢谢当然可以;)有两种方法可以做到这一点,我会更新我的代码来解释
Enter first time value: 1820
Enter second time value: 1610
Difference between 18:20 and 16:10 is 130 minutes.