如何在 Java 中存储花费的时间或总持续时间? [英] How to store time spent or total duration in Java?

查看:40
本文介绍了如何在 Java 中存储花费的时间或总持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 的 POI API 自动生成 excel 报告.我将使用 POI 阅读、处理它们并创建新的 excel 报告.

I am automating a excel report using the POI API for Java. I will have few reports in my disposal which I read using POI, process them, and create a new excel report.

excel 文件中的一列有总时间,也就是说,它可以采用以下格式,46:23:12 - 这读作 46 小时 23 分 12秒.

One of columns from an excel file has total time spent, that is, it could in following format, 46:23:12 - this reads as 46 hours, 23 minutes and 12 seconds.

我了解如何读取日期和时间值,我通常将它们存储在 java.util.Date 中.这里的问题是这是一个持续时间,花费的时间可能超过 24 小时.哪种数据类型适合于此?我如何使用 POI 阅读此内容?

I understand how to read date and time values and I usually store them in java.util.Date. The problem here is this is a time duration, the hours spent could be more than 24. Which datatype could be suitable for this? And how do I read this using POI?

推荐答案

在 Java 8 中,时间量由 class java.time.Duration.

In Java 8, an amount of time is represented by the class java.time.Duration.

我不确定 Apache POI 是否有直接处理 Duration 的方法,所以我认为最好的方法是将值作为 String 获取,然后解析此 String 以获取值并将这些值添加到 Duration:

I'm not sure if Apache POI has methods to deal directly with a Duration, so I believe that the best approach is to get the value as String, then parse this String to get the values and add those values to the Duration:

String s = "46:23:12";
String[] values = s.split(":");
// get the hours, minutes and seconds value and add it to the duration
Duration duration = Duration.ofHours(Integer.parseInt(values[0]));
duration = duration.plusMinutes(Integer.parseInt(values[1]));
duration = duration.plusSeconds(Integer.parseInt(values[2]));

duration 对象将包含与输入等效的数量(在本例中:46 小时 23 分 12 秒).如果您调用 duration.toString(),它会在 ISO 8601 中返回此持续时间格式:

The duration object will contain the amount equivalent to the input (in this case: 46 hours, 23 minutes and 12 seconds). If you call duration.toString(), it returns this duration in ISO 8601 format:

PT46H23M12S

PT46H23M12S

<小时>

当然,此代码假定输入 String 始终包含三个字段(小时、分钟和秒).但是您可以在解析值之前检查 values.length(并且仅当相应的值不为零时才调用 plus 方法).


Of course this code assumes that the input String always contains the three fields (hours, minutes and seconds). But you could check for values.length before parsing the values (and also call the plus methods only if the respective value is not zero).

如果您使用的是 Java <= 7,则可以使用 ThreeTenBackport,一个很棒的 Java 8 新日期/时间类的反向移植.对于 Android,还有 ThreeTenABP(更多关于如何使用它此处).

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

唯一的区别是包名(在 Java 8 中是 java.time 而在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp),但是类和方法名称相同.

The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

这篇关于如何在 Java 中存储花费的时间或总持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆