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
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