convert String To Milliseconds - Android java.util

Android examples for java.util:Millisecond

Description

convert String To Milliseconds

Demo Code


//package com.java2s;
import android.util.Log;

public class Main {
    public static long convertStringToMilliseconds(String time) {
        int seconds = getSecondsFromTimeString(time);
        int minutes = getMinutesFromTimeString(time);
        int hours = getHoursFromTimeString(time);

        return ((seconds * 1000) + (minutes * 60 * 1000) + (hours * 60 * 60 * 1000));
    }/*from   ww  w . j a  v a2 s .co  m*/

    public static Integer getSecondsFromTimeString(String time) {
        if (time.length() >= 2) {
            return Integer.parseInt(time.substring(time.length() - 2));
        } else if (time.length() > 0) {
            return Integer.parseInt(time);
        } else {
            return 0;
        }
    }

    public static Integer getMinutesFromTimeString(String time) {
        if (time.length() > 2) {
            if (time.length() >= 4) {
                return Integer.parseInt(time.substring(time.length() - 4,
                        time.length() - 2));
            } else {
                return Integer
                        .parseInt(time.substring(0, time.length() - 2));
            }
        } else {
            return 0;
        }
    }

    public static Integer getHoursFromTimeString(String time) {
        if (time.length() > 4) {
            return Integer.parseInt(time.substring(0, time.length() - 4));
        } else {
            return 0;
        }
    }
}

Related Tutorials