我们在数据库中存储了以秒为单位的间隔(两个 oracle 时间戳之间的经过时间),我们在前端使用 Java 对其进行格式化。
我们要在报告中实现的是“HH:MM”或“HH:MM:SS”形式的格式,时间分隔符“:”本地化为日期和时间信息,即'.'。意大利语和 ':' 英语。
不幸的是,与日期相关的格式化类(如 SimpleDateFormat)不起作用**,因为我们可以预期持续时间超过 24 小时。我们也不想使用 3rdy 方库。
你知道我们如何解决这个问题吗?
TIA
如果您想要超过 24 小时,您可以单独打印。
int hour = time / 3600000;
String duration = time + new SimpleDateFormat(":mm:ss").format(new Date(time));
要支持其他语言环境,您可以执行这个更复杂的示例。
int hour = time / 3600000;
String duration = hour
+ DateFormat.getDateInstance(DateFormat.MEDIUM, locale)
.format(new Date(time % 3600000).substring(1);
这将做的是使用特定于语言环境的格式作为小时+分钟+秒的最后一位数字,并在小时的其他数字前面加上。注意:这不适用于负数。
You can construct a date object, say "2000-01-01", then add your seconds to it. Then you can use this to extract the localized version of the time:
DateFormat format = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
String formatted_time_part = format.format(some_date_object);
Finally you still need to add elapsed days! I'm affraid that localization of long intervals (days, months, years, centuries) has no corresponding API in Java but I might be wrong. So you will have to figure out that for yourself.