calculate days between two dates in java

Solutions on MaxInterview for calculate days between two dates in java by the best coders in the world

showing results for - "calculate days between two dates in java"
Jannik
03 Feb 2016
1String dateStart = "01/14/2015 08:29:58";
2String dateStop = "01/15/2015 11:31:48";
3
4//HH converts hour in 24 hours format (0-23), day calculation
5SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
6
7Date d1 = null;
8Date d2 = null;
9
10d1 = format.parse(dateStart);
11d2 = format.parse(dateStop);
12
13//in milliseconds
14long diff = d2.getTime() - d1.getTime();
15
16long diffSeconds = diff / 1000 % 60;
17long diffMinutes = diff / (60 * 1000) % 60;
18long diffHours = diff / (60 * 60 * 1000) % 24;
19long diffDays = diff / (24 * 60 * 60 * 1000);
20
21System.out.print(diffDays + " days, ");
22System.out.print(diffHours + " hours, ");
23System.out.print(diffMinutes + " minutes, ");
24System.out.print(diffSeconds + " seconds.");
25
Luca
18 May 2016
1LocalDate start = LocalDate.parse("2020-11-03");
2LocalDate end = LocalDate.parse("2020-12-15");
3
4long diff = DAYS.between(start, end);
Ethan
18 Jul 2019
1SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
2		
3Date birth = sdf.parse("2000-01-01");
4Date now = new Date(System.currentTimeMillis());
5		
6Calendar c = Calendar.getInstance();
7c.setTimeInMillis(now.getTime() - birth.getTime());
8int y = c.get(Calendar.YEAR)-1970;
9int m = c.get(Calendar.MONTH);
10int d = c.get(Calendar.DAY_OF_MONTH)-1;
Henri
05 May 2016
1import java.util.Date;
2import java.text.SimpleDateFormat;
3class Example{
4   public static void main(String args[]){
5	 SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
6	 String dateBeforeString = "31 01 2014";
7	 String dateAfterString = "02 02 2014";
8
9	 try {
10	       Date dateBefore = myFormat.parse(dateBeforeString);
11	       Date dateAfter = myFormat.parse(dateAfterString);
12	       long difference = dateAfter.getTime() - dateBefore.getTime();
13	       float daysBetween = (difference / (1000*60*60*24));
14               /* You can also convert the milliseconds to days using this method
15                * float daysBetween = 
16                *         TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS)
17                */
18	       System.out.println("Number of Days between dates: "+daysBetween);
19	 } catch (Exception e) {
20	       e.printStackTrace();
21	 }
22   }
23}
queries leading to this page
check if difference of date between two dates javahow to get day difference between two days javacalculate date from a number of days from 31 2f12 2f1600 in javajava get difference between two dates in yearshow to find number of days between two dates in javaget months between two dates java 8how to calculate difference between dates in javaget day difference between two dates javacaculate year and month between two dates in javanumbers of days in months javajava calendar days between 2 datesdifference between two dates javahow to get the difference between two dates in days in javajava get years between 2 datesget number of days between two dates javaget different between two dates javano of days between two dates javadays difference dates java days between java untilduration get number of days in javadiff between two dates javafind by days between dates javahow to get difference days between two dates in javajava date difference in monthsjava get difference between two dates in dayshow to find difference between two util dates in java 8amount of days between two dates javajava difference between two timestamps in daysget amount of days between two dates javaduration between two dates javajava between two dateshow to find the n of days in between two dates javajava program to find difference between two datescalculate years between dates in javajava util date difference in daysspring boot date calculate days oldjava get years days difference between two datesget duration between two dates in javajava time between dates as daysjava day difference between datedifference between two dates in years 2c months and days in javahow to present date between two date in javaconvert years months day to number days in javahow to get number of months between two dates in javahow to find day difference in from date javafind numebrs between two dates javajava get number of months between two datesnumber of days between two dates in spring bootdays between javaget difference in days in javafind the number of days between datetime in javahow to get day difference between two dates in javahow to get the difference of dates in years 2c months and days in javajava days in number of years 2c months and daysfind the difference between two java datejava number of days between two dateshow to convert a number of days into months 2c weeks and days javadays in between two dates of same year javadate java check diff day from two datejava date days between two datesget days between two dates java 8calculate time java years between two datescalculate days between two dates javacalculate number of days between given date and current date in javajava difference between 2 datesjava program to find no of days between two datesjava date difference in yearshow to calculate no of days between two dates in javadifference in day between two dates javachronounit days between 28invoice date 2c localdate now 28 29 29 3c 3d 15 3bjava compare date with another given date and count days between two datesdays to years and months in javadifference between two dates java in daysjava calendar day between datesfind number of dates in a stringjava 8 date difference dysjava difference between datejava years between two dateshow to convert number of days to date in javano of days in dates in javahow to count the days betweenj to dates in jksdiff of days between two dates javaget date two days ago javajava number days between two datesdifferenece between two dates javahow to find difference between two dates util in java 8calculate between two dates in javadays between dates java 8java get days between two datesdays between two dates java 8 codedays between 2 dates in javajava difference days between two datesfind the number of days between two dates javamethod to count down number of days between dates in javajava how many dayshow to calculate days between two timestamp in javadays difference between two string dates in javajava days between dateget days from a specific date in javahow to find diffrnce between two days in javafind days between two date in spring bootjava months between two datesjava manually calculate how many days between two datesfind days between two dates in spring bootjava calculate how many days between two datesjava date object get difference in daysfind days between two dates formula in javajava get number days between two datesjava get difference between two datesjava 11 difference date daysfind months between two dates javajava 8 get days between 2 dateshow to find the number of days using dates javajava 8 number of days between two datesjava date between daysgetting date between two dates javafunction to calculate the number of days between two dates in javano of days between 2 dates in javajava calculate years between two datescount days between dates in java no of days between 2 util dates in javadifference day between date in javafind minimum dates between 2 days javajava days until datefind months diff between to dates javajava difference between two dates in dayscount number of days between two java sqldays between in javadifference between dates in days javacheck a date between two dates in javalist of months between two dates javahow to calculate the no of days between two dates in javafind days between two dates javafind days between two dates in java formulajava how to calculate difference between two datesjava chronounit days betwee time shiftjava time between two datesjava difference between two date and time in dayshow to get days between two java util 2c datehow to get two days before date in javadate between two dates javagetting all dates between two dates in javacalculate difference between two date javaget number of days between two dates in javatwo date difference in javacalculate difference between two dates by days javatwo dates difference in javadifference between in dates in javadays between two dates javacalculate days between 2 dates javajava get number of month between 2 dateshow to get days of date from using two dates using javaget the difference between two dates in jaavdifference between two date javacalculate difference between two dates in javathe difference between two dates in javajava 8 days between two datesjava date difference monthshow to get the number of days between two days javajava dates between two datesdate into days in javanumber of days between dates javahow to calculate number of days from a date in javadifference between java datesjava get days from two datesduration between two date javajava day difference between two datesdifference of days javacode to calculate days 2c month and year difference in javajava diff in daysget number of months between 2 java util datespring date calculate daysdays difference between two dates javano of days between two dates in javajava find date difference in daysjava difference between two dates in minutescalculate number of days javafind the number of days between two dates in javacalculate number of years and months between these 2 dates in javainbuilt java function to calculate the no of days between two datesjava 8 days between 2 dates 3 daysjava localdate get all days between two datesfor a given date number of day javajava calculate days between two dates without date classjava get number of days between 2 datescalculate number of years months and days between two dates in javaget days javahow to calculate 6 months from a date in javahow to difference between two dates in javajava daysyear difference between two dates javajava difference between two dates in monthshow can we find difference in days between two dates in javacheck date between two dates javacalculate days between two timestamps java 22calculate year and month between two dates in java 22find day count between two dates in java compare 2 dates in java and fin out howmany days are beetween themfind the difference in hours between two dates javaget dates between two dates javaget all months between two dates javahow to find the difference of two dates in ajavajava find time between two dateshow to find the days between two dates in javahow to calculate difference between the year of two dates in javadifference between two dates in javadifference between 2 days accurate by javajava date calculate days betweencalculate number of days between dates javajava 8 get how many months lies between two datescalculate days between dates javacalculate differece between two dates javafind the number of days between 2 dates javajava difference between dates in yearscount days in locadate timr javajava get all days between two dateschronounit days between javafind difference in days between two dates javajava count days between two datesjava difference between two datesjava program to calculate number of days between two datesget difference between two dates javacalculating number of days between two dates in javajava how to calculate how many full months between two datesjava find number of days between two datescompare difference between two dates in java java difference between date in daysget the number of calendar months between 2 dates in javafinding days gap between two datetime in javadifference of time into months in javaconvert date to number of days javalocaldate java how many days betweencheck if a month and day is between two dates javajava calculate months between two datescalculate days between two dates in javajava difference minutes between two datesnumber of dates between two dates javafind difference between dates in javajava calculate days between two datescheck if date in minutes is between two dates javajava program to find the number of days in a month according to the yearjava date day differencejava compare days between dayshow to find difference between two util dates in java 8how many days since javahow to find the number of days between two dates in javacalculate the difference between two dates javacalculate duration between two dates in javajava time difference between two datesget the difference between 2 dates in javahow to compare between two dates in java how greator than anotherjava calendar get number of days in monthget days difference java how to get no of months and days between two dates in javacreate a method that returns a number of days between two datesget difference between two dates java 11get difference between two dates in javacalculate days to something javahow many days between dates java timejava get days difference between two datesdate get difference in years javahow to calculate the no of days from the given date in javafind difference in two dates in javafind difference between two dates in javataking difference from two dates in javajava code to find date difference in daysdifference between two dates in days in javadifference bw tow dates javajava date get difference in daysyears between to dates on javadiference between two dates javafind no of days from the today in javajava calculate the difference between two datesfinding the difference in number of days between two dates in javacalculate the no of days between two dates in javajava diffrence between two dates in minuteshow to get difference between 2 dates in javanumber of days between two dates spring boot java days between two util datescount how many days since date javahow to do a difference between two dates in javadate differenc in days 2c javanumber of days between two local dates javahow to find difference between two dates in javaget the date of 2 days from now javacalculate number of days between two dates in javahow many day between two dates in javaget days between two dates javacalculate the days 2c years and months between two local dates in javagetting difference between two dates in javajava 8 days between 2 dates after 3 daysjava get age days between two datesjava difference between two dates over multiple yearscount number of months between two dates java get dif days between dates javajava get date difference in dayscalculate day diff in javadifference between two dates and calculate time java check date is between two dates in javajava number of days between two local datesnumber of days between two dates date javajava difference between two dates in years 2c months 2c daysdays between two dates stringget number of days between two dates java 8days left between two dates javajava difference between datesdifference between in dates in years in javadays between java time all months between two dates javajava find difference in days between datescount number of days in javahow to get the difference between two dates in javajava 7 date difference in daysjava days difference between two datesjava calendar get days between two dateshow to get difference between two dates in javajava calculating difference in days between two datescalculate time difference between two dates in javaget diff days between dates javajava get difference of two timestamps in daysnumber of days between two dates javanumber of days between two dates in javajava number of days between datesjava days betweenjava difference between two local dayscalculate the difference in days between two dates javafind days between two date in javahow to find no of days between two util dates in java 8java calculate amount of days in month manuallycalculate number of years and months between 2 dates in javahow to find difference between two dates in java 8calculate number of days between two dates javalocaldate date countjava date difference in daysdifference in days between two dates javacalculate days using dates in javafind years between two dates javajava days between two datesjava difference in days between two datesjava number as daysdifference between 2 date javajava 8 date difference in dayshow to convert days into years months and days in javabetween dates in java 8java number of days between two dates manuallyhow to calculate differencee between two days in javanumber of days in a month javafind days between two datetime javathe number of days java calculateget the difference between dates in no of days in javahow to find days between two dates in javaget days between dates javacalculate the difference in two dates in terms of month and day in javaload just days between two dates javajava two date difference in daysdays between localdates javajava method to find end date using number of days from from datehow to calculate days between two dates in javacount no of days between fromdate and todate javadiff between 2 dates in javadifference in months between two dates javajava get number of days between two datesjava localdate between two datesjava find difference between two dates in secondsget date based on days on javaget numbber of days between dates javajava get years between two datesdays between dates javahow to calculate number of days between dates in javafind days between dates javacalculate no of days between two dates in javacalculate days months and years duration between two dates in javajava get years days difference between two dates 24find difference between two dates javacompare dates java count days date javadays between 2 calendars javahow to get number of days between two dates javadifference between dates in daysjavamake find days between dates in javafind the number of common days between two dates in javanumber of months between two dates javajava check if two dates have 1 day differencefind days between two dates formula in java programget difference between two dates java in years int3 months gap between from date javaget difference between 2 dates javahow to find the difference between two dates in javacalculate days between two dates using javafind the difference between dates in javajava get days between datesjava date days between utilget days between two dates in javajava calendar days betweenjava 11 get time difference between two dates days between java untillocaldatetime java get daysjava count days from datejava different between two daystimestampdiff between two dates sqljava duration between two datesdatediff java in daysjava date number of days betweenprogram to calculate number of days between two dates in javajava date diff dayscalculate difference between week days in javafind no of days between two dates in javacalculate the difference between two dates in java 2fdifference between 2 dates in javahow to calculate difference between two dates in javajava days betweenduration between 2 dates javacalculating the number of days between dates javatotal days between two dates javacount days between two java util date datescalendar days between dates javajava how to find the date of 2 days prior to a particular dateget number of months betweentow dates java 8java date number of days between two datedifference between two date in javafind the number of days between two dates using period in javahow to get the number of days between two dates javacalculate days between two dates javdate difference between two dates javadays difference in javacount days between two dates in javafind weeks between two dates javacalculate year and month between two dates in javacalculate month and day between two date javahow to find number of days between two localdatetimejava calculate amount of days in monthmonths between two dates javacalculate days between two dates in java