Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

find difference in days between two dates java

String dateStart = "01/14/2015 08:29:58";
String dateStop = "01/15/2015 11:31:48";

//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;
Date d2 = null;

d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

//in milliseconds
long diff = d2.getTime() - d1.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.");
Comment

java days between two dates

LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);
Comment

java 8 datediff in days

LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);
Comment

PREVIOUS NEXT
Code Example
Java :: how to read file from console in java 
Java :: string to byte array java utf-8 
Java :: from string to double java 
Java :: Java Queue Array Implementation 
Java :: remove last <br/ from string java 
Java :: java insert char in string at index 
Java :: how to convert milliseconds into second in android studio 
Java :: java empty array 
Java :: all possible valid IP addresses leetcode 
Java :: java check if number is in range 
Java :: create new empty list java 
Java :: graph contains a cycle 
Java :: How to efficiently find the next greater permutation of a list of values, in Java? 
Java :: initialize scanner java 
Java :: java how to convert string to int 
Java :: how to modify string in java 
Java :: navigate to another activity in android 
Java :: Array list deep copy 
Java :: java switch case 
Java :: java convert array to list 
Java :: findviewbyid in kotlin Just using id name . 
Java :: android:windowLightStatusBar programmatically 
Java :: how to fill a 2d array in java 
Java :: test a javafx project 
Java :: string format java 
Java :: Java push() Method 
Java :: how to print in a new line in java using print 
Java :: check folder for files java 
Java :: processing angle between two points 
Java :: sc.nextline skips 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =