Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding the earliest Date

843785Sep 24 2008 — edited Sep 24 2008
Hi all,

First of all, this is not my entire program, but just a part of it. I don't want to put my entire program up b/c i don't want to give it up to others who have to do the same program.

So I will have a bunch of dates for my input and I have to output the earliest date.
There are no specifications on how the date should be inputted so I just used the format "Sep 23, 2008"

This code compiles well but gives an ArrayOutOfBoundException.
Why the error is cause is shown in the program.
import java.util.*;
import java.text.*;

public class HomeworkAssignment1{

	ArrayList<String> arrayDate = new ArrayList<String>();
	Date date = new Date();
	Date temp = new Date();

	String temp1 = null;
	int temp2 = -1; // Reason for ArrayOutOfBoundException

	public Date getDate(String date2){
		try{
			date = DateFormat.getDateInstance(DateFormat.LONG).parse(date2);
		}
		catch (ParseException e) {}
		return date;
	}

	int i = 0;
	public void add(String date1){
		try{
			arrayDate.add(i, date1);
			i++;
		}
		catch (Exception e){
			System.out.println(e);
		}

	}

	public String asgEarlyDueDate(){

		try{
			temp = DateFormat.getDateInstance(DateFormat.LONG).parse("Sep 23, 2008"); // is there any other way to make temp the current date
		}
		catch (ParseException e) {}
		for(int y = 0; y <arrayDate.size(); y++){
			if(getDate(arrayDate.get(y)).compareTo(temp) < 0){ // I think this is where the error is, but I'm not sure how to correct it
				temp2 = arrayDate.indexOf(y);              // this part is not working
				System.out.println(temp2);                 // how to compare two dates and check which one comes first?
			}
		}
		temp1 = arrayDate.get(temp2);
		return temp1;
	}

	public static void main (String [] args){

		HomeworkAssignment1 hmk = new HomeworkAssignment1();
		hmk.add("Dec 16, 2008");
		hmk.add("Nov 17, 2008");
		hmk.add("Oct 01, 2008");
		System.out.println(hmk.asgEarlyDueDate());
	}
}
Thank You 4 ur help!

Edited by: anu14 on Sep 23, 2008 9:17 PM

Edited by: anu14 on Sep 23, 2008 9:17 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 22 2008
Added on Sep 24 2008
30 comments
1,342 views