简体   繁体   中英

unable to get difference of days between two dates in java

I want to calculate the difference of days between two dates. My code works fine when the year of the date does not change, but when I calculate the difference between two dates like so: (13/01/2012 to 13/12/2011), it gives a negative value. It also gives wrong values of difference when I calculate the difference between today's date and a future date. Please help me. Thank you in advance. Here is my code:

//getting values from text box
String fromtext = from.getText().toString();
String totext = to.getText().toString();
//sdf if a simple date formatter
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date fromdate = (Date) sdf.parse(fromtext);
Date todate = (Date) sdf.parse(totext);

Calendar fromcal = Calendar.getInstance();
Calendar tocal = Calendar.getInstance();


    fromcal.setTime(fromdate);
    tocal.setTime(todate);// setting to date


    int reportDays=(int)(todate.getTime()-fromdate.getTime())/(3600*24*1000);

please tell me what is the best way to calculate the difference in days.

Dates input : 13/01/2012 , 13/12/2011

format seems dd/MM/yyyy and you are using wrong one (ie MM/dd/yyyy )

tl;dr

ChronoUnit.DAYS.between(
    LocalDate.parse( "13/01/2012" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ) ,
    LocalDate.parse( "13/12/2011" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) )
)

Using java.time

Much easier with the modern java.time classes that supplant the troublesome old legacy date-time classes such as Date & Calendar .

(13/01/2012 to 13/12/2011),

The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate start = LocalDate.parse( "13/01/2012" , f );
LocalDate stop = LocalDate.parse( "13/12/2011" , f );

Use ChronoUnit to calculate elapsed days.

long days = ChronoUnit.DAYS.between( start , stop );

Of course the number of days is negative when going back in time. Notice how your stop date is earlier than your start date.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec .

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

You can use ChronoUnit again to count days into the future.

long days = ChronoUnit.DAYS.between( today , today.plusMonths( 7 ) );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

In addition to the format issue already mentionned, you are likely to have an overflow. Try this:

int reportDays=(int)((todate.getTime()-fromdate.getTime())/(3600*24*1000));

I would do it like this!

package javaapplication2;
//@author Ibrahim Yesilay
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class JavaApplication2 {  


    public static void main(String[] args) throws ParseException {



    Scanner scan = new Scanner(System.in);        
        System.out.println("First dates Day :");
        int d = scan.nextInt();
        System.out.println("First dates Mounth :");
        int m = scan.nextInt();
        System.out.println("First dates Year :");
        int y = scan.nextInt();
        String date;
        date = Integer.toString(d) + "/" + Integer.toString(m) + "/" + Integer.toString(y);  
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date firstdate = null;
        firstdate = dateFormat.parse(date);
        System.out.println(dateFormat.format(firstdate)); 

        System.out.println("Second dates Day :");
        d = scan.nextInt();
        System.out.println("Second dates Month :");
        m = scan.nextInt();
        System.out.println("Second dates Year :");
        y = scan.nextInt();
        date = Integer.toString(d) + "/" + Integer.toString(m) + "/" + Integer.toString(y);  
        Date seconddate = null;
        seconddate = dateFormat.parse(date);
        System.out.println(dateFormat.format(seconddate)); 

        if (seconddate.getTime() > firstdate.getTime()) {
            long sonuc = (long)(seconddate.getTime()- firstdate.getTime())/(3600*24*1000);
            System.out.println("" + sonuc);
        } else if (firstdate.getTime() > seconddate.getTime()) {
            long sonuc = (long)(firstdate.getTime()- seconddate.getTime())/(3600*24*1000);
            System.out.println("" + sonuc);
        } else {
            System.out.println("The dates are equal!");
        }

    }   
}

使用joda时间将是最简单的方法。

check this code:

import java.util.Calendar;

public class DateDifferent{  
  public static void main(String[] args){
  Calendar calendar1 = Calendar.getInstance();
  Calendar calendar2 = Calendar.getInstance();
  calendar1.set(2007, 01, 10);
  calendar2.set(2007, 07, 01);
  long milliseconds1 = calendar1.getTimeInMillis();
  long milliseconds2 = calendar2.getTimeInMillis();
  long diff = milliseconds2 - milliseconds1;
  long diffSeconds = diff / 1000;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  long diffDays = diff / (24 * 60 * 60 * 1000);
  System.out.println("\nThe Date Different Example");
  System.out.println("Time in milliseconds: " + diff + " milliseconds.");
  System.out.println("Time in seconds: " + diffSeconds + " seconds.");
  System.out.println("Time in minutes: " + diffMinutes + " minutes.");
  System.out.println("Time in hours: " + diffHours + " hours.");
  System.out.println("Time in days: " + diffDays + " days.");
  }
}

Here's a simple little class I wrote for this purpose:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DifferenceInDays
{
    public int dateOffset(String incomingDate) throws ParseException
    {
        // parse dates
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = (Date) formatter.parse(incomingDate);

        // convert to milliseconds
        long millisecs = date.getTime();

        // convert to days
        int offsetInDays = (int) Math.abs(millisecs / (1000 * 60 * 60 * 24));
        return offsetInDays;
    }
}

It takes care of negative offsets using the absolute value method.

If you try this with a locale that has daylight saving, and the from and to dates are before and after a daylight saving change the result may be different by 1 day. This is because Date and Calendar use timezones.

If you are only going to be dealing with dates between the years 1900 and 2100, there is a simple calculation which will give you the number of days since 1900:

public static int daysSince1900(Date date) {
    Calendar c = new GregorianCalendar();
    c.setTime(date);

    int year = c.get(Calendar.YEAR);
    if (year < 1900 || year > 2099) {
        throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");
    }
    year -= 1900;
    int month = c.get(Calendar.MONTH) + 1;
    int days = c.get(Calendar.DAY_OF_MONTH);

    if (month < 3) {
        month += 12;
        year--;
    }
    int yearDays = (int) (year * 365.25);
    int monthDays = (int) ((month + 1) * 30.61);

    return (yearDays + monthDays + days - 63);
}

Thus, to get the difference in days between two dates, you calculate their days since 1900 and calc the difference. Our daysBetween method looks like this:

public static Integer getDaysBetween(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
        return null;
    }

    int days1 = daysSince1900(date1);
    int days2 = daysSince1900(date2);

    if (days1 < days2) {
        return days2 - days1;
    } else {
        return days1 - days2;
    }
}

And don't ask me where this calculation came from because we've used it since the early '90s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM