This question already has an answer here:
for comparison hours I need to get them out Date(). I have two dates I need to find the difference in them exactly in the hours
Date date1 = new Date();
Date date2 = new Date();
System.out.println(date1.getHours()-date2.getHours());
I understand it is not correct code, but need something like that
Mathematical manipulation of date/times values is ignorant and possibly the worst thing you can do. There are a lot of rules which surround date/time manipulation, which is best left to a decent and well tested API.
Since Java 8, you should be using the java.time
API or it's backport for earlier versions.
For example...
Instant now = Instant.now();
Instant then = now.minus(2, ChronoUnit.DAYS);
System.out.println(Duration.between(then, now).toHours());
which prints 48
Get the seconds difference, and then just convert seconds to hours.
var startDate = new Date();
var endDate = new Date();
var seconds = (endDate.getTime() - startDate.getTime()) / 1000;
// 3.557 seconds difference between the two times
var hourDiff = seconds / 60 / 60;
// 0.0009880555555555556 hours..
Wrapped into function:
function getHourDiff(date1, date2) {
var seconds = (date1.getTime() - date2.getTime()) / 1000;
return seconds / 60 / 60;
}
how to make req.query only accepts date format like yyyy-mm-dd
Start Application class A from different Application class B in same Android bundle
How can I get data for select box and set the data while using searching?
Is it possible to log the bytes which a server sends? [closed]
So, I'm creating an N Queens problem solver with BacktrackingThe twist however is that the first queen is placed by the user and then the backtracking is ran to process the other 7 queens
I have a log as a string, and I am trying to capture the error message from itthe regex I tried did not work
Suppose this is the matrixI want fitness of this matrix for solving Sudoku
I currently have a RestTemplate Response Object with String fields to get the Response dataI want to send InputStream in the same Object