从天转换为毫秒

发布于 2024-11-28 13:26:05 字数 52 浏览 3 评论 0原文

我想创建一个将天数转换为毫秒的函数。天的格式存储为0.2444,那么如何将其转换为毫秒呢?

I want to create a function that will convert the days into milliseconds. The days format is stored as 0.2444, so how to convert this to milliseonds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

花海 2024-12-05 13:26:05

我认为最好的做法是:

TimeUnit.DAYS.toMillis(1);     // 1 day to milliseconds.
TimeUnit.MINUTES.toMillis(23); // 23 minutes to milliseconds.
TimeUnit.HOURS.toMillis(4);    // 4 hours to milliseconds.
TimeUnit.SECONDS.toMillis(96); // 96 seconds to milliseconds.

The best practice for this, in my opinion is:

TimeUnit.DAYS.toMillis(1);     // 1 day to milliseconds.
TimeUnit.MINUTES.toMillis(23); // 23 minutes to milliseconds.
TimeUnit.HOURS.toMillis(4);    // 4 hours to milliseconds.
TimeUnit.SECONDS.toMillis(96); // 96 seconds to milliseconds.
梦言归人 2024-12-05 13:26:05

除了其他答案之外,还有 TimeUnit 类允许您将一个持续时间转换为另一个持续时间。例如,要找出一天由多少毫秒组成:

TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS); //gives 86400000

请注意,此方法需要很长的时间,因此如果您有一天的一小部分,则必须将其乘以毫秒数有一天。

In addition to the other answers, there is also the TimeUnit class which allows you to convert one time duration to another. For example, to find out how many milliseconds make up one day:

TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS); //gives 86400000

Note that this method takes a long, so if you have a fraction of a day, you will have to multiply it by the number of milliseconds in one day.

ゃ懵逼小萝莉 2024-12-05 13:26:05

天 * 24 * 60 * 60 * 1000 还不够吗?

Won't days * 24 * 60 * 60 * 1000 suffice?

〗斷ホ乔殘χμё〖 2024-12-05 13:26:05

24 小时 = 86400 秒 = 86400000 毫秒。只需将您的数字乘以 86400000 即可。

24 hours = 86400 seconds = 86400000 milliseconds. Just multiply your number with 86400000.

清晰传感 2024-12-05 13:26:05

值得一提的是,这种方法每 4-5 年可能会出现 1 秒的错误,因为存在闰秒 (http://www.nist.gov/pml/div688/leapseconds.cfm),当天的正确公式为:

(24*60*60 + 1) * 1000

有一个问题 日历是否支持闰秒? 答案是否定的。

因此,如果您正在设计超级依赖时间的软件,请小心这个公式。

Its important to mention that once in 4-5 years this method might give a 1 second error, becase of a leap-second (http://www.nist.gov/pml/div688/leapseconds.cfm), and the correct formula for that day would be

(24*60*60 + 1) * 1000

There is a question Are leap seconds catered for by Calendar? and the answer is no.

So, if You're designing super time-dependant software, be careful about this formula.

时光瘦了 2024-12-05 13:26:05

您可以使用此实用程序类 -

public class DateUtils
{
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
    public static final long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;
}

如果您正在使用Android框架,那么只需导入(也称为DateUtilspackage android.text.format

You can use this utility class -

public class DateUtils
{
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
    public static final long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;
}

If you are working on Android framework then just import it (also named DateUtils) under package android.text.format

恰似旧人归 2024-12-05 13:26:05
public static double toMilliSeconds(double day)
{
    return day * 24 * 60 * 60 * 1000;
}

或者作为long

public static long toMilliSeconds(double day)
{
    return (long) (day * 24 * 60 * 60 * 1000);
}
public static double toMilliSeconds(double day)
{
    return day * 24 * 60 * 60 * 1000;
}

or as long:

public static long toMilliSeconds(double day)
{
    return (long) (day * 24 * 60 * 60 * 1000);
}
只想待在家 2024-12-05 13:26:05
int day = 5;
long dayInMilliseconds = day * org.apache.commons.lang.time.DateUtils.MILLIS_PER_DAY
int day = 5;
long dayInMilliseconds = day * org.apache.commons.lang.time.DateUtils.MILLIS_PER_DAY
月光色 2024-12-05 13:26:05

对 E_x 答案进行一些修改:

public static long convertTimeInMillisSeveral(String which, int howMany) {
        long millis=0;
        switch (which) {
            case "day":
                millis = TimeUnit.DAYS.toMillis(howMany); 
                break;
            case "minute":
                millis = TimeUnit.MINUTES.toMillis(howMany);
                break;
            case "hours":
                millis = TimeUnit.HOURS.toMillis(howMany);
                break;
            case  "seconds":
                millis = TimeUnit.SECONDS.toMillis(howMany);
                break;
        }
        return  millis ; 
    }

示例: 致电 1 天:

long oneDayInMillis = convertTimeInMillisSeveral("day", 1); 

Some Modification of E_x Answer :

public static long convertTimeInMillisSeveral(String which, int howMany) {
        long millis=0;
        switch (which) {
            case "day":
                millis = TimeUnit.DAYS.toMillis(howMany); 
                break;
            case "minute":
                millis = TimeUnit.MINUTES.toMillis(howMany);
                break;
            case "hours":
                millis = TimeUnit.HOURS.toMillis(howMany);
                break;
            case  "seconds":
                millis = TimeUnit.SECONDS.toMillis(howMany);
                break;
        }
        return  millis ; 
    }

Example : Call for 1 day :

long oneDayInMillis = convertTimeInMillisSeveral("day", 1); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文