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

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

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,在那里您会找到以下内容:

// 假设日期为d1和d2

Duration duration = Duration.between(d1, d2);

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:

// assuming both dates are in d1 and d2

Duration duration = Duration.between(d1, d2);

long hours = duration.toHours(); 

Hope that helps.

答案2

得分: 0

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dt1 = LocalDateTime.parse("2020-04-03 01:29:27", formatter);
        LocalDateTime dt2 = LocalDateTime.parse("2020-04-03 01:29:37", formatter);
        System.out.printf("%.4f Hour(s)", Duration.between(dt1, dt2).toSeconds() / 3600.0);
    }
}

Output:

0.0028 Hour(s)

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


<details>
<summary>英文:</summary>

## *java.time*

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

**Output:**

    0.0028 Hour(s)

Learn more about ***java.time*** API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.
 - 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.
 - 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).


  [1]: https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html


</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:

确定