在Java中计算两个日期之间的持续时间。

huangapple go评论111阅读模式
英文:

Duration between two dates Java

问题

我有一个日期时间字符串:2020-04-03 01:29:27 和 2020-04-03 01:29:37
我想获取小时数的持续时间。我已经尝试了很多方法,但是找不到任何帮助。

英文:

I have DateTime string 2020-04-03 01:29:27 and 2020-04-03 01:29:37
I want to get duration in hours. I have tried many things but this but cant find any help

答案1

得分: 5

我已经尝试了很多方法,但是仍然没有找到任何帮助。

这些“很多方法”是否包括查阅Javadoc,在那里您会找到以下内容:

  1. // 假设日期为d1和d2
  2. Duration duration = Duration.between(d1, d2);
  3. long hours = duration.toHours();

希望能对您有所帮助。

英文:

> I have tried many things but this but cant find any help

Do these "many things" include consulting the javadoc where you would find that:

  1. // assuming both dates are in d1 and d2
  2. Duration duration = Duration.between(d1, d2);
  3. long hours = duration.toHours();

Hope that helps.

答案2

得分: 0

  1. import java.time.Duration;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main {
  5. public static void main(String[] args) {
  6. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  7. LocalDateTime dt1 = LocalDateTime.parse("2020-04-03 01:29:27", formatter);
  8. LocalDateTime dt2 = LocalDateTime.parse("2020-04-03 01:29:37", formatter);
  9. System.out.printf("%.4f Hour(s)", Duration.between(dt1, dt2).toSeconds() / 3600.0);
  10. }
  11. }

Output:

  1. 0.0028 Hour(s)

Learn more about the java.time API from the Trail: Date Time.

  1. <details>
  2. <summary>英文:</summary>
  3. ## *java.time*
  4. import java.time.Duration;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. public class Main {
  8. public static void main(String[] args) {
  9. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;);
  10. LocalDateTime dt1 = LocalDateTime.parse(&quot;2020-04-03 01:29:27&quot;, formatter);
  11. LocalDateTime dt2 = LocalDateTime.parse(&quot;2020-04-03 01:29:37&quot;, formatter);
  12. System.out.printf(&quot;%.4f Hour(s)&quot;, Duration.between(dt1, dt2).toSeconds() / 3600.0);
  13. }
  14. }
  15. **Output:**
  16. 0.0028 Hour(s)
  17. Learn more about ***java.time*** API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.
  18. - For any reason, if you have to stick to Java 6 or Java 7, you can use [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) which backports most of the *java.time* functionality to Java 6 &amp; 7.
  19. - If you are working for an Android project and your Android API level is still not compliant with Java-8, check [Java 8+ APIs available through desugaring](https://developer.android.com/studio/write/java8-support-table) and [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project).
  20. [1]: https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
  21. </details>

huangapple
  • 本文由 发表于 2020年4月4日 00:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61015925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定