-2

i want to get difference between two dates whose input parameter will if pass any input date then it should Convert in to yyyy-mm-dd hh:mm:ss

Date1 :2016-05-30 20:16:00


Date2: 2016-05-29 20:17:00

public long totalHour(Date startDate, Date endDate) {

          int diffInDays = (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));

        return diffInDays;
    }

I am using this code but unable to get Output in integer form please help me in this.

Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
user971035
  • 35
  • 1
  • 1
  • 6

1 Answers1

1

The classical way to calculate the difference between two dates in Java is to:

  1. parse them to Date objects
  2. convert both of them to "milliseconds since the UNIX epoch" by calling Date.getTime()
  3. subtract one from the other.
  4. divide by a constant to convert from milliseconds to the time unit that you require.

Your code seems to be doing that but there are a couple of problems:

  1. the constant is wrong
  2. you are truncating to an int which is liable to give you errors if the two dates are far enough apart.

This would be better:

public long hoursDifference(Date start, Date end) {
    return ((end.getTime() - start.getTime()) / (1000 * 60 * 60));
}

Also, you might want to consider rounding rather than truncation.


Parsing dates in an arbitrary format is impossible ... unless you know a priori what the expected format or formats are. In that case, you just try the formats, one at a time, until your parse succeeds. For example, something like this.

  SimpleDateFormat[] sdf = new SimpleDateFormat[] {
     new SimpleDateFormat(/* format 1 */),
     new SimpleDateFormat(/* format 2 */),
     ...
  };

  ...

  Date date = null;
  for (int i = 0; i < sdf.length && date == null; i++) {
      try { 
          date = sdf[i].parse(dateString);
      } catch (ParseException ex) { /* */ }
  }
  if (date == null) {
      /* report parse error ... */
  }

But beware, you won't be able to handle every possible date format correctly. For example, you won't be able to distinguish 7/6/2000 (English 7th of June 2000) from 7/6/2000 (American 6th of July 2000).

IMO, you are better off giving the user a Date chooser of some kind.


... but i want Dataformat is like this yyyy-mm-dd hh:mm:ss

The date format strings are documented in the javadocs for SimpleDateFormat. Read the documentation and you will be able to work it out. And you will be able to work out how to parse other formats too!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • please tell me Solution coz i really new in Android and Java – user971035 Jul 18 '16 at 11:23
  • Date startdate = null; Date endatedate = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { startdate = sdf.parse(Sdate); endatedate = sdf.parse(Edate); } catch (ParseException e) { throw new RuntimeException("Failed to parse date: ", e); } i have parse but date is coming like this Sun May 29 20:16:00 IST 2016 @Stephen C – user971035 Jul 18 '16 at 11:28
  • that is ok Dear but i want Dataformat is like this yyyy-mm-dd hh:mm:ss please help me in this – user971035 Jul 18 '16 at 11:37
  • 1
    Solution is to read the documentation ... and learn :-) – Stephen C Jul 18 '16 at 11:48