Last active
August 29, 2015 14:24
-
-
Save jpt1122/ea3ff5b4fddaedc90a35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1000 milliseconds = 1 second | |
60 seconds = 1 minute | |
60 minutes = 1 hour | |
24 hours = 1 day | |
*/ | |
package com.jpt.dateExample; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class DateDiffExample { | |
public static void main(String[] args) { | |
String dateStart = "02/14/2015 09:29:58"; | |
String dateStop = "02/15/2015 10:31:48"; | |
//HH converts hour in 24 hours format (0-23), day calculation | |
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); | |
Date jd1 = null; | |
Date jd2 = null; | |
try { | |
jd1 = format.parse(dateStart); | |
jd2 = format.parse(dateStop); | |
//in milliseconds | |
long diff = jd2.getTime() - jd1.getTime(); | |
long diffSeconds = diff / 1000 % 60; | |
long diffMinutes = diff / (60 * 1000) % 60; | |
long diffHours = diff / (60 * 60 * 1000) % 24; | |
long diffDays = diff / (24 * 60 * 60 * 1000); | |
System.out.print(diffDays + " days, "); | |
System.out.print(diffHours + " hours, "); | |
System.out.print(diffMinutes + " minutes, "); | |
System.out.print(diffSeconds + " seconds."); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment