Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中以人类可读的格式显示时间?_Java_Calendar_Logic - Fatal编程技术网

如何在Java中以人类可读的格式显示时间?

如何在Java中以人类可读的格式显示时间?,java,calendar,logic,Java,Calendar,Logic,我在做一个逻辑,在一个方法中,我得到的时间是毫秒。 我想展示如下内容: if time is b/w 1 minutes - 60 minutes, lets say 25 minutes it will show - 25 minute if time is 1 hour and 59 minutes it will show - 1h+ later if its 2 hour than it will show - 2h+ this will continue to 24 hour. lat

我在做一个逻辑,在一个方法中,我得到的时间是毫秒。 我想展示如下内容:

if time is b/w 1 minutes - 60 minutes, lets say 25 minutes it will show - 25 minute if time is 1 hour and 59 minutes it will show - 1h+ later if its 2 hour than it will show - 2h+ this will continue to 24 hour. later if its one day and few hours than it will show - 1d+ this will be continue till week and later on week, months and year like 1h+..... 1d, 1d+..... 1w, 1w+..... 1m, 1m+..... 1y, 1y+.. 如果时间是1分钟-60分钟,假设是25分钟,它将显示-25分钟 如果时间为1小时59分钟,则显示-1h+ 如果延迟2小时,则会显示-2h+ 这将持续24小时。 如果晚一天和几个小时,它将显示-1d+ 这将持续到下一周,下一周、下一个月、下一年 1h+。。。。。1d, 1d+。。。。。1w, 1w+。。。。。100万, 1m+。。。。。1y,1y+。。
我不能再多用了。我需要这种方法的良好性能

您可以编写一些if并创建毫秒范围的表,就像使用具有键和值的concurrenthashmap一样。如果您的毫秒范围在0到60000毫秒之间,则在哈希映射中创建一个秒键和值,类似地为60000到3600000,在其中创建一个分钟键和值。同样,您可以选择适当的范围和值。

您可以使用java.util.concurrent.TimeUnit类及其方法toSeconds(长持续时间),toMinutes(长持续时间)、toHiurs(长持续时间)…将毫秒转换为秒、分钟、小时、天等


示例:`TimeUnit.millises.toMinutes(millis);

一个“else if”列表对于不同的范围来说,这似乎是一种可行的方法。它们有什么问题吗?您将比较长时间,这是非常快的。与构建输出和打印输出的成本相比,更是如此,这将需要更长的时间。一个月有多少周?这可能会有所帮助