I have a task to read in a file which contains content in the format of
June 17, 1997
July 23, 1997
September 28, 1980
September 31, 1980
Mar. 2, 1980
Apr. 2, 1980
May 3, 1980
Nov 25, 1989
Dec 25, 1989
Jan 3, 1973
I have to store each date into a Date object. If the next date is later than the previous date, a DateRange object should be made from the two dates. The final output should look like
Date: June 17, 1997
Date: July 23, 1997
DateRange: Date: June 17, 1997 - Date: July 23, 1997
Date: September 28, 1980
Invalid Date
Date: March 2, 1980
Date: April 2, 1980
DateRange: Date: March 2, 1980 - Date: April 2, 1980
Date: May 3, 1980
DateRange: Date: April 2, 1980 - Date: May 3, 1980
Date: November 25, 1989
DateRange: Date: May 3, 1980 - Date: November 25, 1989
Date: December 25, 1989
DateRange: Date: November 25, 1989 - Date: December 25, 1989
Date: January 3, 1973
To start, I'm sorry for such a long post and so much to read, but I've been working on this problem for a while and I'm not sure how to continue. I'm stuck because I'm not entirely certain how I can compare the two dates and put them into the DateRange class. A snippet of my main program consists of:
Scanner in = null;
try {
in = new Scanner(new File("dates.txt"));
} catch (FileNotFoundException exception) {
System.err.println("failed to open dates.txt");
System.exit(1);
}
while (in.hasNextLine()) {
String line = in.nextLine();
Date date = new Date(line);
testDates(date);
if (testDateRange(date) < 0){
System.out.println("There's a range");
}
}
}
public static int testDateRange(Date dates){
return dates.compareTo(dates);
}
public static void testDates(Date dates){
System.out.printf("%s\n", dates.getDate());
//System.out.printf("Date: %s\n", dates.getDate());
}
}
My Date class consists of:
public class Date implements Comparable<Date> {
private String dates;
private int year;
private int day;
private int month;
public Date(String line) {
dates = line; // initialize date
} // end one-argument Grades constructor
public String getDate() {
dates = dates.replace(".", "");
dates = dates.replace(",", "");
StringTokenizer st = new StringTokenizer(dates);
String initMonth = st.nextToken();
String initDay = st.nextToken();
String initYear = st.nextToken();
Integer day = Integer.parseInt(initDay);
Integer year = Integer.parseInt(initYear);
String month = initMonth.substring(0,3);
int numMonth = 0;
int maxDays = 0;
String Month = null;
switch (month){
case "Jan":
numMonth = 1;
maxDays = 31;
Month = "January";
break;
case "Feb":
numMonth = 2;
maxDays = 29;
Month = "February";
break;
case "Mar":
numMonth = 3;
maxDays = 31;
Month = "March";
break;
case "Apr":
numMonth = 4;
maxDays = 30;
Month = "April";
break;
case "May":
numMonth = 5;
maxDays = 31;
Month = "May";
break;
case "Jun":
numMonth = 6;
maxDays = 30;
Month = "June";
break;
case "Jul":
numMonth = 7;
maxDays = 31;
Month = "July";
break;
case "Aug":
numMonth = 8;
maxDays = 31;
Month = "August";
break;
case "Sep":
numMonth = 9;
maxDays = 30;
Month = "September";
break;
case "Oct":
numMonth = 10;
maxDays = 31;
Month = "October";
break;
case "Nov":
numMonth = 11;
maxDays = 30;
Month = "November";
break;
case "Dec":
numMonth = 12;
maxDays = 31;
Month = "Decemeber";
break;
default:
break;
}
if(maxDays < day){
dates = ("Invalid Date");
}else{
dates = ("Date: " + Month + " " + day + ", " + year);
}
return dates;
}
public int compareTo (Date other) {
int result = year - other.year;
if (result == 0)
{
result = month - other.month;
if (result == 0)
{
result = day - other.day;
}
}
if (result > 0) return 1;
else if (result < 0) return -1;
else return 0;
}
}
My concern is in the Date class where I need to compare two dates. In my compareTo() method I know that it's always going to return 0 because I'm not implementing it correctly. I'm just confused on how to implement this compareTo() method and actually compare the first date with the second, the second with the third, and so on. And I currently have no DateRange class because I have not been able to test two dates to see if they have a range.
SimpleDateFormatwith a (possible) format ofMMMM d, yyyyto parse theStrings toDates. Then use eitherDate#beforeorDate#afterto determine the relationship between the dates...