tags:

views:

55

answers:

3

I have a ArrayList returned from a service which contains date-timestamp as String values (with values: 2010-05-06T23:38:18,2010-05-06T23:32:52,2010-04-28T18:23:06,2010-04-27T20:34:02,2010-04-27T20:37:02)

to be more specific, This is part of a parent ArrayList ObjectHistory. This list contains the datestamp and serial number. I need to pick the correct serial number.

Objecthistory is the List object and I need to get the latest timestamp within this ObjectHistory.

I need to pick the latest timestamp from this Arraylist in Java 6.

How should I be doing this? Should I do convert these values into calendar-time? I am in panic mode as this has to be done directly in production.

A: 

Assuming you mean the arraylist contains Date Objects (i.e. it is of type ArrayList) you can simply use Collections.sort()

If the list contains Long values (i.e. the timestamp values) the same thing will work.

Edit: Added in a javadoc link to the Collections.sort() method


Additional Edit:

For additional clarification on comparators:

You would make a comparator for your ObjectHistory class:

public class ObjectHistoryComparator impelements Comparator<ObjectHistory> {
    public int compare(ObjectHistory o1, ObjectHistory o2) {
        return o1.getDateTime().compareTo(o2.getDateTime());
    }
}

What this does for you is allows you to use the default comparison of the dateTime format (whether it is a String or whatever) and then you can pass this into a sorter:

Collections.sort(array, new ObjectHistoryComparator());

Hope this helps out.

aperkins
Should work for strings as well, if they're in the format shown above.
highlycaffeinated
thanks, so I do a collections.sort() and get the first one?
rob
http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)There is the java doc - it will tell you if it is ascending or descending. A good way to test it would be to simply try it out and debug or print into the data that you find
aperkins
thanks aperkins. I am just in panic mode. I did look at it, modified my original question also, I have my time-stamp values in List<ObjectHistory> so I am unable to sort this. what would you suggest?
rob
I would recommend making a comparator: http://java.sun.com/javase/6/docs/api/java/util/Comparator.htmlYou would then call Collections.sort(<myArray>, new MyComparator())The comparator basically tells the sort method "this is how you compare two elements of this type to each other" so it can do the sorting for you.We have all been in panic mode - the best advice I can give is to just take it a step at a time. :) Good luck.
aperkins
A: 

I would use TreeMap. For example,

   TreeMap<String, String> map = new TreeMap<String, String>();

   map.put(timestamp1, serial1);
   map.put(timestmap2, serail2);
   ...

   String latestTimestamp = map.lastKey();
   String latestSerial = map.get(lastestTimestamp);
ZZ Coder
A: 

For converting your ISO 8601 dates to Java date time objects, you might take a look at the Joda Time library.

Marcus Adams