import java.util.*;
import javax.swing.*;
public class ArrayListofDates {
public static void main(String[] args) {
String input;
int size;
int userAnswer;
ArrayList myDatesList = new ArrayList();
input = JOptionPane.showInputDialog( "Enter number of random dates" );
size = Integer.parseInt( input );
//**array list of random Dates
for (int i=1; i<=size; i++) {
myDatesList.add(generateRandomDate()); //**pseudo data...
}
//iterate over them
Iterator it=myDatesList.iterator();
System.out.println();
System.out.println("Here are your dates: ");
Date thisDate;
while (it.hasNext()) {
//reference to the Date in the list.
//***next() returns Object, must cast to Date:
thisDate = (Date)(it.next());
//thisDate = (it.next()); //**failure to cast would be compile error
System.out.print(thisDate);
//***access a method of the Date object:
System.out.println(" Month is: " + thisDate.getMonth());
}
System.out.println();
//Used to Identify and Display duplicate Dates
//Used to prompt user if they want to delete the duplicate Dates
//This is where I am having trouble inserting the methods.
//generate a random Date. Note this is not a method of the Date class.
//it creates and returns a Date object.
public static Date generateRandomDate() {
int year, month, day=0;
year = (int)(Math.random()*20+1995); // +/- 10 years from 2005
month = (int)(Math.random()*12+1);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = (int)(Math.random()*31+1);
break;
case 4:
case 6:
case 9:
case 11:
day = (int)(Math.random()*30+1);
break;
case 2:
if (year%4==0 && year%100!=0 || year%400==0) //leap year test
day = (int)(Math.random()*29+1);
else
day = (int)(Math.random()*28+1);
break;
}
//instantiate a Date object using the 3-arg constructor,
//then return the object to caller.
return new Date(month,day,year);
}
}
I am currently living in a tent in SW Asia and nobody around here can help me.
Thank You!

New Topic/Question
This topic is locked



MultiQuote






|