【发布时间】:2017-07-19 23:35:55
【问题描述】:
我必须计算两个日期之间的差异,其中一个来自用户输入,另一个来自当前时间。这是我目前所拥有的:
long time2 = System.currentTimeMillis();
System.out.print("Enter Time-in(hh:mm)");
String start = input.next();
String newTime[] = start.split(":");
String h = newTime[0];
String m = newTime[1];
String s = newTime[2];
int newH = Integer.parseInt(h);
int newM = Integer.parseInt(m);
LocalTime time1 = LocalTime.now();
long hr=ChronoUnit.HOURS.between(time1,time2);
System.out.println("Total number of hours: " + hr);
【问题讨论】:
-
从用户输入的时间值开始创建一个新的
LocalTime(或LocalDateTime) -
LocalTime().of(newH, newM)例如,你就有了可以比较的东西 -
你能把
javascript标签去掉吗? -
@shmosel(像这样?) LocalTime time1 = LocalTime.now(); LocalTime.parse(开始); long hr=ChronoUnit.HOURS.between(time1,time2); System.out.println("总小时数:"+hr);
-
关于 Stack Overflow 的下一个问题的提示,如果您清楚准确地解释您的代码如何无法解决您的问题,它会更容易帮助您;也就是说,期望行为与程序观察到的行为之间的差异。
标签: java time java-time timedelta localtime