如何将毫秒转换为天、小时、分钟 [英] How do I convert milliseconds to days, hour, minutes

查看:66
本文介绍了如何将毫秒转换为天、小时、分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这样做:

        long Plptime = player.getStatistic(Statistic.PLAY_ONE_TICK)*50L; //from ticks to ms(1 tick (20 each sec) by 50 gives aprox the ms)

        SimpleDateFormat formatter = new SimpleDateFormat("dd 'days,' HH 
        'hours and' mm 'minutes'", Locale.getDefault());

        Date date = new Date(Plptime);
        String result1 = formatter.format(date);

但是当它向玩家(顺便说一下我的世界)发送字符串消息时,小时和天从 1 开始,而分钟从 0 开始,例如,当有人刚刚加入他的游戏时间时,他的时间是 01 天 01 小时 00 分钟.任何解决方案?谢谢

But when it messages the String to the player (minecraft by the way), the hours and days start on 1 while the min start on 0, for example right when someone just joins his playtime will be 01days 01 hours 00 min. Any solutions? Thanks

推荐答案

Java 9 或更高版本

让我们首先声明几个有用的常量.

Java 9 or later

Let’s first declare a couple of helpful constants.

private static final int TICKS_PER_SECOND = 20;
public static final Duration ONE_TICK = Duration.ofSeconds(1).dividedBy(TICKS_PER_SECOND);

现在做:

    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    Duration plpTime = ONE_TICK.multipliedBy(ticks);
    String result1 = String.format(Locale.ENGLISH, "%02d days %02d hours and %02d minutes",
            plpTime.toDays(), plpTime.toHoursPart(), plpTime.toMinutesPart());

    System.out.println(result1);

这会打印一个字符串

00 天 17 小时 08 分钟

00 days 17 hours and 08 minutes

可能每秒滴答数 (20) 已经在 Bukkit 的某处声明为常量,我不知道.如果是,就拿那个,而不是声明你自己的.

Possibly the number of ticks per second (20) is already declared as a constant somewhere in Bukkit, I don’t know. If it is, take that one rather declaring your own.

我使用的 toXxxPart 方法是在 Java 9 中引入的.如果没有它们,我们需要像这样计算各个部分:

The toXxxPart methods I used were introduced in Java 9. Without them we need to calculate the individual parts like this:

    long days = plpTime.toDays();
    plpTime = plpTime.minusDays(days);
    long hours = plpTime.toHours();
    plpTime = plpTime.minusHours(hours);
    long minutes = plpTime.toMinutes();
    String result1 = String.format(Locale.ENGLISH, "%02d days %02d hours and %02d minutes",
            days, hours, minutes);

结果和上面一样.

我使用的 Duration 类是 java.time 的一部分,现代 Java 日期和时间 API

The Duration class that I am using is part of java.time, the modern Java date and time API

  • 在 Java 8 及更高版本以及更新的 Android 设备上(据我所知,从 API 级别 26 开始)java.time 是内置的.
  • 在 Java 6 和 7 中获得 ThreeTen Backport,即新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接).
  • 在(旧版)Android 上使用 ThreeTen Backport 的 Android 版本.它被称为 ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) java.time comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

为什么 小时 似乎从 1(而不是 0)开始:这是您的时区.当您从毫秒创建 Date 时,您会在定义为 1970 年 1 月 1 日 UTC 时间 00:00 的纪元之后许多毫秒获得时间点(当问题是何时有玩家加入).如果您的时区在 1970 年冬天比 UTC 早 1 小时(例如中欧时间),那么此时已经是 1 点钟,因此小时数从那里开始计算.

Why the hours seem to start at 1 (not 0): It’s your time zone. When you create a Date from your milliseconds, you get the point in time that many milliseconds after the epoch defined as 00:00 UTC on Jan 1, 1970 (which conceptually is quite misleading when the question was when a player joined). If your time zone was 1 hour ahead of UTC in the winter of 1970 (like Central European time, for example), it was already 1 o’clock at the epoch, so the hours count from there.

因为是 1 月 1 日,所以 day 当然是 1.奇怪的是,如果您在格林威治标准时间以西的时区(美国/洛杉矶仅举一个例子),日期仍然是 1969 年 12 月 31 日,也就是纪元后的第一个小时,因此新加入的玩家可能看起来像例如,已经在那里呆了 31 天 16 小时 00 分钟.

And since it was January 1, the day is given as 1, of course. Curiously, if you had been in a time zone west of GMT (America/Los_Angeles to give just one example), the date would still have been December 31, 1969 in the first hours after the epoch, so the newly joined player might appear to have been there for 31 days, 16 hours and 00 minutes, for example.

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.timeto Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于如何将毫秒转换为天、小时、分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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