당신은이 작업을 수행 할 수있는 라이브러리가 필요하지 않습니다.당신이 종이
HH MM
06 34
- 19 45
--------
10 49
에서와 (34)보다 작은 45이기 때문에 45에서 34를 뺄 수없는 것처럼 당신이 "빌려"시간에서 60분 그래서 그냥, 그것을 해결. 당신은 (60 + 34) -45 = 49로 끝나고 시간은 6에서 5로 줄었습니다. 5가 19보다 작기 때문에 우리는 다음날로 넘어 갔고 시간 차이는 5+ (24-19) = 10이다.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
final String filename = "myfile.txt";
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(filename));
String line;
//read the file, line-by-line
while ((line = in.readLine()) != null) {
//break the line into various pieces
Scanner scanner = new Scanner(line);
scanner.next(); //skip the first bit
int hour1 = scanner.nextInt();
int min1 = scanner.nextInt();
int hour2 = scanner.nextInt();
int min2 = scanner.nextInt();
int diffMin;
if (min2 < min1) {
// borrow 60mins from the hours
diffMin = 60 + min2 - min1;
if (hour2 == 0) {
hour2 = 23;
} else {
hour2--;
}
} else {
diffMin = min2 - min1;
}
int diffHour;
if (hour2 < hour1) {
diffHour = hour2 + 24 - hour1;
} else {
diffHour = hour2 - hour1;
}
System.out.printf("%sh %sm\n", diffHour, diffMin);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
}
이 코드는 압축 할 수 있지만, 당신은 무슨 일이 일어나고 있는지 이해할 수 있도록 내가 자세한 그것을 만든 : 여기
코드의 모습 것입니다.
@Downvoter : 관심있는 댓글? –
+1 downvote를 얻지 마십시오. 제 3 자 라이브러리이지만 Joda Time은 날짜/시간 조작을해야하는 경우 필수적이라고 생각합니다. – helpermethod