1
String sql = "SELECT  endtime FROM user_req";
ResultSet rs = stmt.executeQuery(sql);

ArrayList<Timestamp> list1= new ArrayList<Timestamp>();
while (rs.next()) {

    list1.add(rs.getTimestamp("endtime"));
}
System.out.println("\n the requested time is : ");
System.out.println(list1);

this gives me the result:-

the requested time is : 
[2013-12-12 07:00:00.0, 2013-12-12 05:00:00.0]

this is the code which I've used that retrieves the timestamp data from MySQL and stores it in an aaraylist. but how to compare those dates and get the difference in terms of days and hours. please help me and thanks in advance.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
sb15
  • 69
  • 10
  • Will your query always return 2 results? – shree.pat18 Mar 28 '14 at 03:07
  • Take a look http://stackoverflow.com/questions/3796841/getting-the-difference-between-date-in-days-in-java – newuser Mar 28 '14 at 03:13
  • Why not use `Timeslamp.comparteTo()`? – locoyou Mar 28 '14 at 03:13
  • @shree.pat18 no..at present I have these 2 dates. I've seen many examples but I didn find any like how to compare more than one timestamp when it is stored in a single arraylist – sb15 Mar 28 '14 at 03:22
  • @newuser : even in that example, they have a startdate and an enddate so that v can subtract..but my doubt is like how to perform samething when it is stored in a single arraylist... please help me with that concept – sb15 Mar 28 '14 at 03:24
  • 1
    You do know how to get objects out of the list, right? – vanza Mar 28 '14 at 03:34
  • @sb15 - Have you bothered to read the javadoc for `ArrayList`? Did you see the `get(int)` operation??? – Stephen C Mar 28 '14 at 03:40

1 Answers1

0

try this,

    Collections.sort(timeStampList);
    Timestamp tempStamp = timeStampList.get(0);
    for (Timestamp startstamp : timeStampList)
    {
        if (tempStamp != startstamp)
        {
                long diffTime = startstamp.getTime() - tempStamp.getTime();
                diffDays = diffTime / (1000 * 60 * 60);

                if(diffDays > 24)
                {
                    System.out.println(diffDays / 24 +"Day" + " "+diffDays % 24 + "hours");
                }
                tempStamp = startstamp;
        }
    }
newuser
  • 8,338
  • 2
  • 25
  • 33
  • thanks for the code.. I've tried with this nd its showing me the result as:- the requested time is : [2013-12-12 07:00:00.0, 2013-12-12 05:00:00.0] 7200000 83 – sb15 Mar 28 '14 at 04:30
  • thank you so much for the help..its working fine..just I had to make it in terms of hours, minutes nd seconds..which I could do :) – sb15 Mar 28 '14 at 05:38
  • is it possible to apply the same logic if there are more than 2 timestamps in a list.. If so I thought of arranging it or sorting it in a ascending order and then check the difference. sorting I'm able to do.. so can I apply the same logic?? – sb15 Mar 31 '14 at 04:22
  • S its possible to show the difference for more than 2. If show the diff of 1,2 and 2,3 and 3,4 and so on. – newuser Mar 31 '14 at 07:13