Erik Eriksson
Erik Eriksson

Reputation: 75

Get the oldest Timestamp in list Java

Is this correct?

ArrayList<Timestamp> timeStampList = new ArrayList<Timestamp>();
timeStampList.add(0, new Timestamp(System.currentTimeMillis())); 

Thread.sleep(600); 

timeStampList.add(1, new Timestamp(System.currentTimeMillis()));

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;
    }
}

I am NOT sure how I can find the oldest timestamp in the array list. Any advice or direct answer is appreciated. Thanks a lot. This current code I think only compares.

EDIT: So assume I do not use Collection.sort in this context, I know I did apply that when I posted, but lets assume I want to know how to compare and find the oldest timestamp of all in the arrayList via a for-loop.

Upvotes: 0

Views: 3769

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338654

tl;dr

new ArrayList<>( original ).sort().getLast()

Details

If you do not want to sort the original collection, make a new collection.

Collection< Timestamp > original = … ;
List< Timestamp > sorted = new ArrayList<>( original ) ;

Then sort. If your objects are Comparable, simply call List#sort.

sorted.sort() ;

Then grab the last one. In Java 21+, use the convenient getLast method.

Timestamp latest = sorted.getLast() ;

By the way, never use the java.sql.Timestamp class. As part of the terribly flawed legacy date-time classes including Calendar, Date, etc., you should avoid like the plague.

Instead, use only the java.time classes defined in JSR 310. In general, use java.time.Instant. When exchanging with a database via JDBC, use java.time.OffsetDateTime.

Upvotes: 0

furkle
furkle

Reputation: 5059

If, based on your post, you didn't want to sort your data, but instead wanted to just iterate through your entire collection of Timestamp objects and compare that way, you could use the Timestamp's after() method, which establishes whether a Timestamp occurs after the Timestamp provided as the argument of the function.

public Timestamp getOldestTimeStamp(ArrayList<Timestamp> timeStampList)
{
    if (timeStampList != null || timeStampList.length() == 0)
    {
        return null;
    }

    Timestamp oldestTimestamp = timeStampList.get(0);

    for (int i = 1; i < timeStampList.length(); i++)
    {
        if (oldestTimeStamp.after(timeStampList.get(i))
        {
            oldestTimeStamp = timeStampList.get(i);
        }
    }

   // oldestTimeStamp is now the oldest Timestamp in the ArrayList

    return oldestTimeStamp;
}

I could've sworn I included this in my first edit, but it looks like it didn't take. Everyone in the comments is correct in their wish for you to use the built-in features of the language instead of rolling your own. In this case, you would be far better suited learning to use simple language features like Collections.sort() and Collections.min(), which will invariably be as efficient and typically more so than the kind of code you or I will write, and significantly more succinct than a 12-line method like the one above.

Upvotes: 2

Related Questions