Find difference between two time in java
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to convert Milliseconds to"X mins, x seconds" in Java?
Calculating the Difference Between Two Java Date Instances
我有这样的功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public String setDateAndTime(int h, int m, int s,int amPm) { TimeZone nst = TimeZone.getTimeZone("GMT+5:45"); Calendar calendar1 = new GregorianCalendar(nst); Date currentTime1 = new Date(); calendar1.setTime(currentTime1); calendar1.set(Calendar.HOUR, h); calendar1.set(Calendar.MINUTE, m); calendar1.set(Calendar.SECOND, s); calendar1.set(Calendar.YEAR, calendar1.get(Calendar.YEAR)); calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH)); calendar1.set(Calendar.DAY_OF_MONTH, calendar1.get(Calendar.DAY_OF_MONTH)); calendar1.set(Calendar.AM_PM, amPm); DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); String date = formatter.format(calendar1.getTime()); return date; } |
通过这个函数我创建了两个日期(String)
1 2 |
现在如何找到找到s1和s2之间的差异,我还需要小时,分钟和第二左,这样我才能做进一步的处理。
您可以通过调用
你也可以使用像JodaTime这样的第三方库,但是如果你需要这个一次性的Joda太多了。