英文:
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.
- For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the
java.time
functionality to Java 6 & 7. - If you are working on an Android project and your Android API level is still not compliant with Java 8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
<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("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 ***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 & 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论