For example, if it's 1 p.m (13:00) and I need to get the number 46800000 (13 hours from beginning o day) in milliseconds. Could anyone please help?
Asked
Active
Viewed 6,106 times
2 Answers
21
You can use a Calendar to calculate it. You set the time to the hour 0 and calculate the difference:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long millis = (System.currentTimeMillis() - c.getTimeInMillis());

Marcos Lima
- 761
- 1
- 10
- 26
-
1
-
Refer this: https://stackoverflow.com/questions/8826270/how-to-convert-hhmmss-sss-to-milliseconds – Rohit Jain May 17 '19 at 09:07
-
This answer is incorrect and doesn't answer OP's question for future readers – alfietap Feb 18 '22 at 18:02
0
Joda-Time
Using the Joda-Time 2.5 library, it is an easy one-liner.
long millisOfDay = DateTime.now( DateTimeZone.forID( "America/Montreal" ) ).getMillisOfDay();
Note how time zone is crucial. If omitted you implicitly rely on the JVM’s current default.

Basil Bourque
- 303,325
- 100
- 852
- 1,154