I have a timestamp as a String from a Json in the form of 11/02/2017 17:25:20
as actualArrivalTime and 11/02/2017 17:25:22
as actualScheduledTime. I would like to calculate delayTime by subtracting the two timestamps to get the difference, which will in this case be +2 or -2 seconds.
You first need to parse the strings into a date - in your case, the LocalDateTime
class seems appropriate. You can then calculate the difference:
String actualArrivalTime = "11/02/2017 17:25:20";
String actualScheduledTime = "11/02/2017 17:25:22";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
LocalDateTime arrival = LocalDateTime.parse(actualArrivalTime, fmt);
LocalDateTime scheduled = LocalDateTime.parse(actualScheduledTime, fmt);
long seconds = ChronoUnit.SECONDS.between(arrival, scheduled);
System.out.println("Time difference in seconds: " + seconds);
var actualArrivalTime = new Date("11/02/2017 17:25:20");
var actualScheduledTime = new Date("11/02/2017 17:25:22");
var delayTime = (actualArrivalTime - actualScheduledTime) / 1000; //-2
Depending on which order you need, you can switch the two vars around.
var date1=new Date("11/02/2017 17:25:20"); var date2=new Date("11/02/2017 17:25:22"); var diff = Math.abs(date1 - date2); console.log(diff/1000);
Try this:
var arrivalTime = new Date("11/02/2017 17:25:20");
var scheduledTime = new Date("11/02/2017 17:25:22");
var diff = arrivalTime.getTime() - scheduledTime.getTime();
var diffInMs = (diff/1000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.