-6

I have two resultset each having only one column. The column field of both resultset contain a time entry in a format like this (2012-12-31 13:49:21.999). Now can anyone help me to find difference of time between two columns of the resultset? e.g. if first field of column of first resultset has entry (2013-02-13 17:04:09.672) and first field of column of second resultset has entry (2012-12-31 13:49:21.999) then the program should be able to difference in the time of these two enries.

need help?

newuser
  • 8,338
  • 2
  • 25
  • 33
Suhas Dalvi
  • 70
  • 1
  • 4
  • 3
    What have you tried and what are you having difficulty with? Have you tried getting the milli-seconds of each time stamp and taking the difference. – Peter Lawrey Sep 13 '13 at 08:28
  • parsing dates from strings and comparing them has been asked too many times here... – A4L Sep 13 '13 at 08:29
  • What have you tried so far, post your code and where you're having an issue. A basic search of the site will show many options for this. Have you attempted anything or do you want your homework done for you? Duplicate http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – RossC Sep 13 '13 at 08:29
  • yes im looking for a code which gives difference with milliseconds as well – Suhas Dalvi Sep 13 '13 at 08:29
  • You're looking for code? Google it. Then come back when you have a specific issue not a thinly veiled 'do my homework for me' type question. – RossC Sep 13 '13 at 08:30
  • the thing is i knoiw abt java.util.date class, but the time format i have specified here is making me think if its the same format of java.util.date class? – Suhas Dalvi Sep 13 '13 at 08:32

1 Answers1

2

Something like:

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column1);
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column2);
getDateDiff(date1,date2,TimeUnit.MINUTES);

/**
 * Get a diff between two dates
 * @param date1 the oldest date
 * @param date2 the newest date
 * @param timeUnit the unit in which you want the diff
 * @return the diff value, in the provided unit
 */
public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}
stan
  • 984
  • 10
  • 15