如何將當前時間的差值轉換爲給定時間以創建時間格式的字符串:HH:mm?恩。 18:36將長時差轉換爲格式HH:mm [Java]
我做了以下,但它不是24小時格式,它將添加AM/PM到最後,並且它是3個小時關閉。
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(time);
java.sql.Timestamp ts2 = new java.sql.Timestamp(parsedDate.getTime());
long nowTime = ts1.getTime();
long givenTime = ts2.getTime();
long timeDiff = givenTime - nowTime;
//convert to string
java.util.Date d = new java.util.Date(timeDiff);
result = DateFormat.getTimeInstance(DateFormat.SHORT).format(d);
//Outputs: 6:56 PM for example
可能的重複[如何計算Java中的時間跨度和格式化輸出?](http://stackoverflow.com/questions/635935/how-can-i-calculate-a-time-span-in -java-and-format-the-output) – Thilo 2011-06-15 01:53:53
另外,一個可能的重複:http://stackoverflow.com/questions/2949699/converting-time-from-hhmmss-to-hhmm-in-java – 2011-06-15 01:55:22