Convert from days to milliseconds

JavaDatetime Conversion

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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 https://stackoverflow.com/questions/14065355/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.

Solution 5 - Java

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

Solution 6 - Java

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);
}

Solution 7 - Java

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

Solution 8 - Java

int day = 5;
long dayInMilliseconds = day * org.apache.commons.lang.time.DateUtils.MILLIS_PER_DAY

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaasView Question on Stackoverflow
Solution 1 - JavaE_XView Answer on Stackoverflow
Solution 2 - JavadogbaneView Answer on Stackoverflow
Solution 3 - JavaVladView Answer on Stackoverflow
Solution 4 - JavaRustem MustafinView Answer on Stackoverflow
Solution 5 - JavaGaurav GuptaView Answer on Stackoverflow
Solution 6 - JavaEng.FouadView Answer on Stackoverflow
Solution 7 - JavaGk Mohammad EmonView Answer on Stackoverflow
Solution 8 - Javauser11572247View Answer on Stackoverflow