import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.text.DecimalFormat;
import
java.text.NumberFormat;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
java.util.regex.Pattern;
public
class Main {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String line =
"01:00:17,213 --> 00:00:19,909";
int time = -2500;
boolean bMatched = (line.indexOf("-->") > 0)
&& Pattern.matches(
"^\\d{2}:\\d{2}:\\d{2},\\d{3}.*", line);
if (bMatched) {
System.
out.println(shiftTime(time, line.split("-->")[0].trim()));
System.
out.println(shiftTime(time, line.split("-->")[1].trim()));
}
}
/**
* @param timeMS
* milliseconds that will be shifted, could be negative
* @param timeToken
* the accept pattern example is: 00:00:17,213
* @return the String which is already shifted, the pattern keeps stable
* @throws ParseException
*/
public static String shiftTime(int timeMS, String timeToken)
throws ParseException {
SimpleDateFormat formatter =
new SimpleDateFormat("HH:mm:ss,SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatter.parse(timeToken));
calendar.add(Calendar.
MILLISECOND, timeMS);
return formatter.format(calendar.getTime());
}
/**
* @param timeMS
* milliseconds that will be shifted, could be negative
* @param timeToken
* the accept pattern is: 00:00:17,213
* @return the String which is already shifted
* @throws Exception
* Hours could not be negative
*/
public static String shiftTimeByString(int timeMS, String timeToken)
throws Exception {
String[] elements = timeToken.split(
":|,");
int nTimeHour = Integer.parseInt(elements[0]);
int nTimeMin = Integer.parseInt(elements[1]);
int nTimeSec = Integer.parseInt(elements[2]);
int nTimeMS = Integer.parseInt(elements[3]);
nTimeMS = nTimeMS + timeMS % 1000;
nTimeSec = nTimeSec + timeMS / 1000;
if (nTimeMS < 0) {
nTimeSec -= 1;
nTimeMS += 1000;
}
if (nTimeSec < 0) {
nTimeMin -= 1;
nTimeSec += 60;
}
if (nTimeMin < 0) {
nTimeHour -= 1;
nTimeMin += 60;
}
if (nTimeHour < 0) {
throw new Exception(
"Hour can not be less than zero. Unexpected results: "
+ timeToken);
}
StringBuffer sbResult =
new StringBuffer();
NumberFormat formatter2d =
new DecimalFormat("00");
NumberFormat formatter3d =
new DecimalFormat("000");
sbResult.append(formatter2d.format(nTimeHour)).append(
":").append(
formatter2d.format(nTimeMin)).append(
":").append(
formatter2d.format(nTimeSec)).append(
",").append(
formatter3d.format(nTimeMS));
System.
out.println(sbResult.toString());
return sbResult.toString();
}
private static String exec(String command) throws Exception {
Process process;
String inputLine =
null, errorLine;
int result = 0;
try {
process = Runtime.getRuntime().exec(command);
inputLine =
new BufferedReader(new InputStreamReader(process
.getInputStream())).readLine();
errorLine =
new BufferedReader(new InputStreamReader(process
.getErrorStream())).readLine();
result = process.waitFor();
System.
out.println("inputLine=" + inputLine);
}
catch (Exception e) {
errorLine = e.getMessage();
}
if (0 == result) {
return inputLine;
}
else {
throw new Exception("CNRPassWord failure (" + result + "): "
+ errorLine);
}
}
}
No comments:
Post a Comment