-1

Hi I have to find out days difference b/w two dates .First one is current date and second Date i am taking from database.Actually i want to send Automatic email at difference of 90 days b/w these two dates . Java 1.6 is using in my Application.

I am using this code

for(BidderHeaderBean checkBean:tenderheadersbean){
    Date currentdate=new Date();
    String datePattern = "yyyy-MM-dd";
    SimpleDateFormat df = new SimpleDateFormat(datePattern);
    String bidopeningdate=  MisUtility.convertDateToString(checkBean.getBidOpeningDate());
    Date givenDate = df.parse(bidopeningdate);
    Long last = givenDate.getTime();
    Long current=currentdate.getTime();
    Long timediff=last-current;
    Long diffdays=timediff/(1000 * 60 * 60 * 24);
}

I am not getting difference of days.Please help me . Thanks & Regards Bhagwan singh

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user3595144
  • 45
  • 2
  • 9
  • So, if not the difference in days, what are you getting? – Robby Cornelissen Aug 15 '14 at 07:52
  • Take the first date, transform it to a Calendar, add 90 days, see if the result is before or after the second date. – JB Nizet Aug 15 '14 at 07:58
  • i am getting value of diffdays is -730163. – user3595144 Aug 15 '14 at 07:59
  • You know how the bid opening data has been established, and with which hour/minute/second value. - We all know that new Date() returns the current point in time. - We also know that a difference in milliseconds can be integer-divided by ms/day, and we can expect one day more or less, depending on the answers to previous questions. - So...? – laune Aug 15 '14 at 08:02
  • Time increases: "now" has more milliseconds than any previous date. – laune Aug 15 '14 at 08:03
  • Actually bid opening date is in "yyyy/MM/dd" format it's doesn't contain hour/minute/second value. – user3595144 Aug 15 '14 at 08:08
  • possible duplicate of [Difference in days between two dates in Java?](http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java) – DavidPostill Aug 15 '14 at 08:19

1 Answers1

2

You are probably doing this the wrong way anyway, do a database query to give you every result where the difference is > 90 days.

Assuming you really need to do it this way (for example you've already loaded these records into Java anyway) then the correct way is to use a Calendar or the new java Time API to do all the maths for you. Take the first date, add 90 days to it, see if it is after or before the second date.

Tim B
  • 40,716
  • 16
  • 83
  • 128