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?
-
10How 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
-
6Oh 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 Answers
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.

- 27,862
- 20
- 113
- 121

- 3,722
- 1
- 14
- 15
-
9No, this should not be accepted answer , because DAYS.toMillis takes only long argument and OP has days in double – Ľubomír Mar 08 '17 at 00:27
-
5this answer is the answer most people who come here are looking for, but not the answer to OP – OsaSoft Oct 11 '19 at 12:03
-
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.

- 266,786
- 75
- 396
- 414
Won't days * 24 * 60 * 60 * 1000
suffice?

- 10,602
- 2
- 36
- 38
-
2He 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
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.

- 1
- 1

- 957
- 1
- 11
- 23
-
2Just if someone is interested — there is going to be a leap second in 2016 – Rustem Mustafin Jul 20 '16 at 09:18
24 hours = 86400 seconds = 86400000 milliseconds. Just multiply your number with 86400000.

- 5,380
- 2
- 29
- 36
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);
}

- 115,165
- 71
- 313
- 417
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

- 6,084
- 3
- 42
- 42
int day = 5;
long dayInMilliseconds = day * org.apache.commons.lang.time.DateUtils.MILLIS_PER_DAY

- 3,418
- 11
- 21
- 28

- 11
- 1
-
1it'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