101

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?

nabster
  • 1,561
  • 2
  • 20
  • 32
maas
  • 1,183
  • 4
  • 10
  • 9
  • 10
    How many hours in a day? How many minutes in an hour? How many seconds in a minute? How many milliseconds in a second? – Rup Aug 08 '11 at 09:54
  • 1
    `0.2444` what does it signifies ? – jmj Aug 08 '11 at 09:55
  • @Jigar Joshi it probably means .2444 of a day aka 24.44% or 5.86 hours. – Daniel Ryan Aug 08 '11 at 10:03
  • 6
    Oh and don't forget that some days are 25h, some 23h, and some even 23h59min59sec (leap second). – giraff Aug 08 '11 at 10:23
  • @giraff - I think you have to ignore that got the question to make sense. The is .244 of a generic day, not a specific day. – Stephen C Aug 08 '11 at 10:56
  • @Stephen: I slightly disagree: it depends on how it is used. If it is added to a specific day, then 0.5 days may not be 12 hours. – giraff Aug 11 '11 at 10:18
  • 1
    @giraff - a function that converts a fraction of a specific day to milliseconds has to take two arguments, or an implicit argument (like "today"). I have difficulty believing that it is what the OP's application really needs. (For example, nobody in their right mind would require a user to enter a fraction of a day into a timesheet **taking account of the number of hours in the specific day**. Now we don't **know** that's what the OP's application is, but ...) – Stephen C Aug 12 '11 at 07:46
  • @StephenC Thank you for brightening my day. Lawyers typically charge in 6-minute increments. But I've always wanted a contract where I can fill in my timesheet then charge by the millisecond. – Dawood ibn Kareem May 02 '14 at 22:00
  • There are actually 86400002 milliseconds in a day, if your app is going to be independent upon updating it's time (no internet service/no sim card on old phone for examples) for extended periods of time those 2 milliseconds add up noticably over even a few hours. – Wraithious Jul 19 '18 at 21:04

8 Answers8

316

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.
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
E_X
  • 3,722
  • 1
  • 14
  • 15
126

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
25

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

Vlad
  • 10,602
  • 2
  • 36
  • 38
  • 2
    He probably wants it as a long, so you might need to round it from a double at the end too. – Rup Aug 08 '11 at 09:57
13

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.

Community
  • 1
  • 1
Rustem Mustafin
  • 957
  • 1
  • 11
  • 23
13

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

Gaurav Gupta
  • 5,380
  • 2
  • 29
  • 36
3
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);
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

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

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
1
int day = 5;
long dayInMilliseconds = day * org.apache.commons.lang.time.DateUtils.MILLIS_PER_DAY
barbsan
  • 3,418
  • 11
  • 21
  • 28
  • 1
    it's always better to explain what your answer means. Please take few minutes to illustrate how this code works – DaFois May 29 '19 at 10:40