我该如何减去两个不同日期中的两次,例如2019/07/24的23:20:45和2019/07/25的00:10:32。 它应该还给我这两个小时:分钟:秒之间的差额。
我必须记下晚上登录系统的人员持续时间,但是随着日期的更改,这样做有点困难,因此我需要使用Java中的一些代码,该代码将以小时:分钟:秒为单位返回准确的时间
答案 0 :(得分:1)
您的日志应使用UTC的时刻记录用户的活动,并以标准ISO 8601格式写为文本。末尾的Z
表示与UTC的时差为零小时-分钟-秒,并且被称为“祖鲁语”。例如:2019-07-24T23:20:45Z
。
Instant
类代表UTC的时刻。
Instant instant = Instant.now() ;
String output = instant.now();
解析。
Instant start = Instant.parse( "2019-07-24T23:20:45Z" ) ;
计算经过的时间。
Duration d = Duration.between( start , stop ) ;
以标准ISO 8601文本PnYnMnDTnHnMnS
报告持续时间。
String output = d.toString() ;
答案 1 :(得分:1)
LocalDateTime from = LocalDateTime.parse("2019/07/24 23:20:45", formatter);
LocalDateTime to = LocalDateTime.parse("2019/07/25 00:10:32", formatter);
System.out.println(Duration.between(to.toLocalTime(),from.toLocalTime()).getSeconds());
因此,由于需求最近发生了变化,现在它是大约到某个时刻之间的总持续时间,而不是仅是时间部分之间的持续时间,因此省略了日期部分,整个事情变得更简单了,那就是只是
System.out.println(Duration.between(to,from).getSeconds());
...和一个重复的问题...