मेरे पास DateTime की दो वस्तुएँ हैं , जिन्हें उनके अंतर की अवधि ज्ञात करने की आवश्यकता है ,
मेरे पास निम्नलिखित कोड है, लेकिन यह सुनिश्चित करने के लिए सुनिश्चित नहीं है कि इसे निम्नलिखित परिणामों के लिए कैसे जारी रखा जाए:
उदाहरण
11/03/14 09:30:58
11/03/14 09:33:43
elapsed time is 02 minutes and 45 seconds
-----------------------------------------------------
11/03/14 09:30:58
11/03/15 09:30:58
elapsed time is a day
-----------------------------------------------------
11/03/14 09:30:58
11/03/16 09:30:58
elapsed time is two days
-----------------------------------------------------
11/03/14 09:30:58
11/03/16 09:35:58
elapsed time is two days and 05 mintues
कोड
String dateStart = "11/03/14 09:29:58";
String dateStop = "11/03/14 09:33:43";
Custom date format
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
} catch (ParseException e) {
e.printStackTrace();
}
// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");