File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to get the oldest timestamp record in an array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to get the oldest timestamp record in an array list" Watch "how to get the oldest timestamp record in an array list" New topic
Author

how to get the oldest timestamp record in an array list

guestin loy
Greenhorn

Joined: Oct 09, 2008
Posts: 5
Hi,
I have an arraylist. now i want to get the record with the oldest timestamp from all the records present in the arraylist.
example:
arraylist[]
record1 : Thu Oct 09 20:51:03 GMT+05:30 2008
record2: Fri Oct 09 20:51:03 GMT+05:30 2009
record3: Thu Oct 10 20:51:03 GMT+05:30 2008
record4: Thu Dec 09 20:51:03 GMT+05:30 2006

The required output should be record4:Thu Dec 09 20:51:03 GMT+05:30 2006.
could you please suggest any solution for this?

Regards
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Welcome to the JavaRanch.
We will not do your work for you but we are glad to help. What have you tried so far?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

to figure out how to do it in java, first think about how you would do it yourself. try and write out the steps you would take.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Do you know how you can calculate the minimum of a list of numbers? This is basically the same problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Originally posted by fred rosenberger:
to figure out how to do it in java, first think about how you would do it yourself. try and write out the steps you would take.


This is good advice. Write out the steps, then replace each step with functional code. When you get to hicups on actual syntax and questions on whether java has a function 'ready-made' to do a certain step people here will be more than glad to help. Showing any attempt...even if it's just the psuedo'd non-sense makes us much more likely to help with code.
guestin loy
Greenhorn

Joined: Oct 09, 2008
Posts: 5
Thank you all.
few things are there in my arraylit object.
Array list is defined in some object say (objDepartmentDetails).
and that Arraylist (say alDept) contains Department objects.The Department object has the fields DeptNo and DeptDate.The getter method for DeptNo is getDeptNum and for the DeptDate is getDeptDate.

and i calling this object(objDepartmentDetails) from other class.
I tried few things.
1. I converted date to Milisecond and then used this
min_time = Array[0].getMillisecond ;max_time =Array[0].getMillisecond;
for (int i = 0; i < Array.length; i++)
{
if (Array[i].getMillisecond < min_score)
{
min_time = Array[i].getMillisecond;
}
else {
if (Array[i].getMillisecond > max_score)
{
max_time = Array[i].getMillisecond; }


but i want to use if any already defined method is available 'cos i have to use the similar logic for 4-5 methods so,
2. I thought of using Collections.sort(); but its not accepting my Arraylist.

2. Then i tried with
getSortedList(ArrayList alObject, String getMethod,String dataType,char order)
but its not recognising the getDeptDate().

i am now clueless.Could you correct me and help in giving me right direction?

Regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Drop the else in else if; it is quite possible that the same value can be smaller than the minimum and greater than the maximum; in fact if you set your initial minimum and maximum correctly that should happen with the first value.
What are you setting your initial minimum and maximum to?
What you actually need to do is record the index where you found the minumum and maximum; then you can dispense with sorting.
guestin loy
Greenhorn

Joined: Oct 09, 2008
Posts: 5
i mistakenly give some other variable name. i correctd the same in bold.
i'm initializing it with the very firts record.
min_time = Array[0].getMillisecond ;max_time =Array[0].getMillisecond;
for (int i = 0; i < Array.length; i++)
{
if (Array[i].getMillisecond < min_time)
{
min_time = Array[i].getMillisecond;
}
else {
if (Array[i].getMillisecond > max_time)
{
max_time = Array[i].getMillisecond; }

as per your suggestion, i can take the Index where the min time and max time is present and then can retrive that element from the array list by avoidign sorting.

Could you please suggest any other inbuilt function to get the sort for the list?
Please note that the list here is JAXB generated list.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

One small improvement (apart from losing the else) that will prevent errors if your array is empty:

The idea here is that the minimum and maximum over an empty list or array are positive infinity and negative inifinity respectively. Since those are not valid for long Long.MAX_VALUE and Long.MIN_VALUE are the closest matches.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

In my humble opinion, you are not thinking in an object oriented way...

If you go this strings to sort according to the date:


Then I would attack the problem like this.

- I got a string that is has a description, a semicolon as delimiter and a date string.
- Lets split the string into two, the description and the date string.
- Hmm.. how can I convert the date string into something comparable?
- Oh, I can use the SimpleDateFormat and use a format description that looks something like this (but not exact) ""EEE MMM d HH:mm:ss Z yyyy" and parse the string into a Date-object.
- Then I put the date-object into a List of some sort.
- When I'm done parsing all the string, let's use Collection.sort(List<E> aList) on that list.

Well.... yes, you have to do something to keep track of the date-record relation...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

Originally posted by Ove Lindstr�m:
Collection.sort(List<E> aList)[/QB]


Just a small correction of a typo here.
It should be Collections.sort(List<E> aList)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get the oldest timestamp record in an array list