我必须计算两个日期之间的差异,其中一个来自用户的输入,另一个是当前时间。以下是我到目前为止的情况:
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);
答案 0 :(得分:2)
试试这个:
System.out.print("Enter Time-in(hh:mm)");
String start=input.next(); //make sure it have "hh:mm" format
LocalTime userTime = LocalTime.parse(start);
LocalTime currentTime = LocalTime.now();
long diff = ChronoUnit.HOURS.between(currentTime, userTime);