Wednesday, December 13, 2017

Convert Time In String To Second In java

 In this java program calculate the second from given time in string. For example time="00:2:30:22" so the second of given time is 150 sec.


class ConvertTimeToSecond {

    public int getSecond() {
       //Time is string
        String timestampStr = "00:01:40.70";
        timestampStr = timestampStr.substring(0, timestampStr.lastIndexOf("."));
        String[] tokens = timestampStr.split(":");
        int hours = Integer.parseInt(tokens[0]);
        int minutes = Integer.parseInt(tokens[1]);
        int seconds = Integer.parseInt(tokens[2]);
        int duration = 3600 * hours + 60 * minutes + seconds;
        //System.out.println(duration);

        return duration;
    }

    public static void main(String arg[]) {

        ConvertTimeToSecond obj = new ConvertTimeToSecond();

        int totalSecond = obj.getSecond();

        System.out.println("Total Second " + totalSecond);

    }
}

The output is :
javac ConvertTimeToSecond.java
 java ConvertTimeToSecond

Total Second 100

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...