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 number of days between dates

LocalDate start = LocalDate.parse("2020-11-03");
LocalDate end = LocalDate.parse("2020-12-15");

long diff = DAYS.between(start, end);
Comment

java days between two dates

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

calculate number of years months and days between two dates in java

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
Date birth = sdf.parse("2000-01-01");
Date now = new Date(System.currentTimeMillis());
		
Calendar c = Calendar.getInstance();
c.setTimeInMillis(now.getTime() - birth.getTime());
int y = c.get(Calendar.YEAR)-1970;
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH)-1;
Comment

PREVIOUS NEXT
Code Example
Java :: heroku java 17 enable preview 
Java :: java arraylist get number of elements with same parameter 
Java :: java get specific element from arraylist 
Java :: java array kürzen 
Java :: program in java. Get a string from user and also validate it’s length should be at least 1. Pass this string to a method that returns a new string with the first char added at the front and end, so "cat" yields "ccatc". 
Java :: how to change a bukit chat format 
Java :: minecraft plugin send all players message 
Java :: text before dot java 
Java :: java int to hex fixed length 
Java :: java split array into two 
Java :: Integrity check failed: java.security.NoSuchAlgorithmException: Algorithm HmacPBESHA256 not available jks 
Java :: play ringtone android in infinite loop 
Java :: access each key and value in a hashmap java 
Java :: cannot set char field to null value java 
Java :: print arraylist java 
Java :: How to efficiently invert a binary tree, in Java? 
Java :: nums.add to add the number to the array in java sample 
Java :: decimals java 
Java :: Date from String java7 
Java :: separate string by comma java 
Java :: check if table exist sqlite java 
Java :: convert set to list java 
Java :: how to play audio files java 
Java :: convert string to short java 
Java :: get file extension java 
Java :: how to remove components from a JFRame java 
Java :: greatest common divisor java 
Java :: testException = com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 
Java :: java.lang.ExceptionInInitializerError: null 
Java :: java random 5 digit int 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =