How can I find the number of days between two dates?

I have two Dates. How to know the difference between the two dates in days?

I heard about SimpleDateFormat, but I do not know how to use it.

I tried this:

String fromdate = "Apr 10 2011";

SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MMM DD YYYY"); 

sdf.parse(fromdate);

Calendar cal = Calendar.getInstance();
cal.setTime(sdf);

I also tried this:

String date1 = "APR 11 2011";
String date2 = "JUN 02 2011";
String format = "MMM DD YYYY";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(date1);
Date dateObj2 = sdf.parse(date2);
long diff = dateObj2.getTime() - dateObj1.getTime();
int diffDays = (int) (diff / (24* 1000 * 60 * 60));
System.out.println(diffDays);

but I got the exception "Invalid wildcard character" Y ".

+3
source share
5 answers

Other answers are correct but outdated.

java.time

The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes, such as java.util.Date, .Calendarand java.text.SimpleDateFormat. The Joda-Time team also advises switching to java.time.

, . Oracle. Qaru .

java.time Java 6 7 ThreeTen-Backport Android ThreeTenABP.

ThreeTen-Extra java.time . java.time.

LocalDate

LocalDate .

ISO 8601 , . .

String input = "Apr 10 2011";

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "MMM d uuuu" );
LocalDate earlier = LocalDate.parse ( input , f );

DateTimeFormatter formatterOut = DateTimeFormatter.ofLocalizedDate ( FormatStyle.MEDIUM ).withLocale ( Locale.US );
String output = earlier.format ( formatterOut );

LocalDate later = earlier.plusDays ( 13 );

long days = ChronoUnit.DAYS.between ( earlier , later );

.

System.out.println ( "from earlier: " + earlier + " to later: " + later + " = days: " + days + " ending: " + output );

: 2011-04-10 : 2011-04-23 = : 13 : 10 2011 .

+2

Date?

.

public double computeDaysBetweenDates(Date d1, Date d2) {
    long diff;

    diff = d2.getTime() - d1.getTime();
    return ((double) diff) / (86400.0 * 1000.0);
}

Date - , UTC POSIX EPOCH, , , 1 1970 UTC. , . , .

Note that this answer assumes that the day is 86400 seconds and ... this is mostly true.

Note that this solution still works, even if you formatted date strings. If you can first parse the date / time in Date objects, you can still call this function with them.

+1
source

This code should show you how to do what you need ...

public static void main(String[] args) {
    Calendar firstOfTheMonthCalendar = Calendar.getInstance();
    firstOfTheMonthCalendar.set(Calendar.DAY_OF_MONTH, 1);

    Date firstOfTheMonthDate = firstOfTheMonthCalendar.getTime();
    Date nowDate = new Date();

    int diffInDays = determineDifferenceInDays(firstOfTheMonthDate, nowDate);
    System.out.println("Number of days betweens dates: " + diffInDays);
}

private static int determineDifferenceInDays(Date date1, Date date2) {
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(date1);
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(date2);
    long diffInMillis = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();
    return (int) (diffInMillis / (24* 1000 * 60 * 60));
}
0
source

Difference between two date strings in java

this will help you get an idea

0
source

All Articles