Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert HH:mm:ss.SSS to milliseconds?

Tags:

I have a String 00:01:30.500 which is equivalent to 90500 milliseconds. I tried using SimpleDateFormat which give milliseconds including current date. I just need that String representation to milliseconds. Do I have to write custom method, which will split and calculate milliseconds? or Is there any other way to do this? Thanks.

I have tried as follows:

        String startAfter = "00:01:30.555";         SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");         Date date = dateFormat.parse(startAfter);         System.out.println(date.getTime()); 
like image 989
Ahamed Avatar asked Jan 11 '12 20:01

Ahamed


People also ask

How do you convert HH MM SS to milliseconds?

You can use SimpleDateFormat to do it. You just have to know 2 things. . getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC.

How do you convert HH MM SS to seconds?

To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 .

How do I convert timestamps to milliseconds in Excel?

Select a blank cell besides the first time cell, enter the formula =RIGHT(TEXT(A2, "hh:mm:ss. 000"),3)/1000 (A2 is the first time cell in the list) into it, and then drag the Fill Handle down to the range as you need. Now you will get the calculation results showing as time with milliseconds as above screenshot shown.


2 Answers

You can use SimpleDateFormat to do it. You just have to know 2 things.

  1. All dates are internally represented in UTC
  2. .getTime() returns the number of milliseconds since 1970-01-01 00:00:00 UTC.
package se.wederbrand.milliseconds;  import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone;  public class Main {             public static void main(String[] args) throws Exception {         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));          String inputString = "00:01:30.500";          Date date = sdf.parse("1970-01-01 " + inputString);         System.out.println("in milliseconds: " + date.getTime());             } } 
like image 134
Andreas Wederbrand Avatar answered Oct 01 '22 22:10

Andreas Wederbrand


If you want to parse the format yourself you could do it easily with a regex such as

private static Pattern pattern = Pattern.compile("(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})");  public static long dateParseRegExp(String period) {     Matcher matcher = pattern.matcher(period);     if (matcher.matches()) {         return Long.parseLong(matcher.group(1)) * 3600000L              + Long.parseLong(matcher.group(2)) * 60000              + Long.parseLong(matcher.group(3)) * 1000              + Long.parseLong(matcher.group(4));      } else {         throw new IllegalArgumentException("Invalid format " + period);     } } 

However, this parsing is quite lenient and would accept 99:99:99.999 and just let the values overflow. This could be a drawback or a feature.

like image 24
Roger Lindsjö Avatar answered Oct 01 '22 23:10

Roger Lindsjö