我正试图通过输入时间格式在java中计算12小时内的时差,当我输入开始时间11:58:10 pm
和结束时间12:02:15 am
时,它运行良好。但当我输入凌晨12:00:00和下午12:00:00时,它给出了0分钟的差异。不知道为什么。下面是我的代码,请告诉我哪里错了。
Scanner input = new Scanner(System.in);
System.out.print("Enter start time (HH:mm:ss aa): ");
String starttime = input.nextLine();
System.out.print("Enter end time (HH:mm:ss aa): ");
String endtime = input.nextLine();
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(starttime);
d2 = format.parse(endtime);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
System.out.print(diffMinutes + " minutes and "+diffSeconds + " seconds.");
} catch (Exception e) {
System.out.println("Invalid fromat");
}
新的java.time
方法LocalTime
、DateTimeFormatter
和Duration
为处理此问题提供了更好的方法。例如:
DateTimeFormatter format = DateTimeFormatter.ofPattern("h:m:s a");
LocalTime time1 = LocalTime.parse("12:00:00 am", format);
LocalTime time2 = LocalTime.parse("2:00:20 pm", format);
Duration dur = Duration.between(time1, time2);
System.out.println(dur.toMinutes() + " minutes " + dur.toSecondsPart() + " seconds");
注意:Duration.toSecondsPart
需要Java 9或更高版本,此代码的其余部分需要Java 8或更高级别。
问题出在以下行:
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss aa");
应该是:
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
符号 12小时您对 按如下操作: 样本运行: 注意: 考虑日期和时间的程序: 样本运行:H
用于24小时格式的时间,而h
用于diffMinutes
的计算也是错误的。import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter start time (hh:mm:ss aa): ");
String starttime = input.nextLine();
System.out.print("Enter end time (hh:mm:ss aa): ");
String endtime = input.nextLine();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(starttime);
d2 = format.parse(endtime);
// in milliseconds
long diff = Math.abs(d2.getTime() - d1.getTime());
long diffSeconds = (diff / 1000) % 60;
long diffMinutes = (diff / (60 * 1000));
System.out.print(diffMinutes + " minutes and " + diffSeconds + " seconds.");
} catch (Exception e) {
System.out.println("Invalid fromat");
}
}
}
Enter start time (hh:mm:ss aa): 10:20:30 am
Enter end time (hh:mm:ss aa): 10:20:13 pm
719 minutes and 43 seconds.
Math.abs
为您提供了一个数字的绝对值,该数字最适合查找两个量之间的差import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter start time (dd.MM.yyyy at hh:mm:ss aa): ");
String starttime = input.nextLine();
System.out.print("Enter end time (dd.MM.yyyy at hh:mm:ss aa): ");
String endtime = input.nextLine();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy 'at' hh:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(starttime);
d2 = format.parse(endtime);
// in milliseconds
long diff = Math.abs(d2.getTime() - d1.getTime());
long diffSeconds = (diff / 1000) % 60;
long diffMinutes = (diff / (60 * 1000));
System.out.print(diffMinutes + " minutes and " + diffSeconds + " seconds.");
} catch (Exception e) {
System.out.println("Invalid fromat");
}
}
}
Enter start time (dd.MM.yyyy at hh:mm:ss aa): 03.03.2020 at 11:58:10 pm
Enter end time (dd.MM.yyyy at hh:mm:ss aa): 04.03.2020 at 12:02:15 am
4 minutes and 5 seconds.