1. 程式人生 > >SDUT 1177 C語言實驗——時間間隔

SDUT 1177 C語言實驗——時間間隔

next btn main -i top input acm sub none

C語言實驗——時間間隔

Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss

Problem Description

從鍵盤輸入兩個時間點(24小時制),輸出兩個時間點之間的時間間隔,時間間隔用“小時:分鐘:秒”表示。
如:3點5分25秒應表示為--03:05:25.假設兩個時間在同一天內,時間先後順序與輸入無關。

Input

輸入包括兩行。
第一行為時間點1。
第二行為時間點2。

Output

以“小時:分鐘:秒”的格式輸出時間間隔。
格式參看輸入輸出。

Example Input

12:01:12
13:09:43

Example Output

01:08:31

Java實現

 1 import java.util.Scanner;
 2 import java.util.Date;
 3 import java.util.TimeZone;
 4 import java.text.SimpleDateFormat;
 5 import java.lang.Math;
 6 
 7 public class Main{
 8     public static void main(String[] args) throws Exception {
 9         SimpleDateFormat fd=new SimpleDateFormat("HH:mm:ss");
10 Scanner input=new Scanner(System.in); 11 Date t1=fd.parse(input.next()); 12 Date t2=fd.parse(input.next()); 13 long t=Math.abs(t2.getTime()-t1.getTime()); 14 fd.setTimeZone(TimeZone.getTimeZone("GMT+0")); 15 System.out.println(fd.format(t)); 16 }
17 } 18 19 /*************************************************** 20 User name: *** 21 Result: Accepted 22 Take time: 196ms 23 Take Memory: 11900KB 24 Submit time: 2017-05-18 15:48:06 25 ****************************************************/

SDUT 1177 C語言實驗——時間間隔