1

I have created my own method to transform hours, minutes and seconds in milliseconds. It seems to work fine.

I would prefer to use some Java/Android API to do this task.

TimeUnit provides me the reverse: milliseconds to hours, minutes and seconds.

Here goes my code:

class SomeUtilsClass {
    public static long toMilliseconds(int hours, int minutes, int seconds) {
        return ((hours * 60 * 60) + (minutes * 60) + seconds) * 1000;
    }
}
Ramsharan
  • 2,054
  • 2
  • 22
  • 26
Thom Thom Thom
  • 1,279
  • 1
  • 11
  • 21
  • 1
    Have you tried `TimeUnit.MINUTES.toMillis(minutes);` , `TimeUnit.HOURS.toMillis(hours);` and `TimeUnit.SECONDS.toMillis(seconds);` ? – Rami Apr 12 '15 at 13:17
  • Actually no. What about something like my function. It passes all the three units ... (I know, I'm wanting too much :P) – Thom Thom Thom Apr 12 '15 at 13:26
  • @Thom Thom Thom can you check answer and let me know if any issues. – Prags Dec 08 '17 at 16:37

2 Answers2

0

Your code looks fine and should work perfectly, but if you want to use Java/Android API to do this task you can change your function like this:

    public static long toMilliseconds(int hours, int minutes, int seconds) {
            return (TimeUnit.HOURS.toMillis(hours) + TimeUnit.MINUTES.toMillis(minutes) + TimeUnit.SECONDS.toMillis(seconds));
    }

PS: You'll have the same result with both functions.

Rami
  • 7,879
  • 12
  • 36
  • 66
0

You can use calendar API for get time TimeInMillis. using java API

import java.util.Calendar;

final Calendar calEventstart=Calendar.getInstance();
calEventstart.set(mYear, mMonth, mDay, mHour, mMinute);
calEventstart.getTimeInMillis();
Prags
  • 2,457
  • 2
  • 21
  • 38