Search code examples
javaconverterstime-format

Convert time format to long variable in Java


Is there a possibility to convert a time format which is represented as a string (00:00:000) to long variable.


Solution

  • I'm not sure what do you mean by Convert to currentTimeMillis() or some float variable., but if you are simply looking to convert the given time to long then you can do something like this using simple split:

    String timeString = "01:00:100";
    int multiplier[] = {3600000, 60000, 100};
    String splits[] = timeString.split(":");
    long time = 0;
    for (int x = 0; x < splits.length; x++) {
        time += (Integer.parseInt(splits[x]) * multiplier[x]);
    }
    System.out.println(time);
    

    Here, the time is being represented in Milliseconds.

    Also, this is plain Java and nothing Android specific.