0

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.

4
  • What about converting dates to miliseconds then subtracting them. Eg: date1.getTime()-date2.getTime() Commented Sep 24, 2013 at 2:35
  • 1
    Start by using SimpleDateFormat with a (possible) format of MMMM d, yyyy to parse the Strings to Dates. Then use either Date#before or Date#after to determine the relationship between the dates... Commented Sep 24, 2013 at 2:37
  • Well my main concern is how do I actually access date1 and date2 at the same time? I can compare them, I just don't know how to access them at the same time Commented Sep 24, 2013 at 2:42
  • Should I store the dates in an array? Commented Sep 24, 2013 at 2:42

2 Answers 2

5

You can try using SimpleDateFormat class. It will simplify your issue.

For example you can convert the date in String format to Date format as below first.

String str = "June 17, 1997";
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
Date date = sdf.parse(str);

then for your comparison between dates you can use before(date) or after(date) methods.

date.before(date2);
date.after(date2);
Sign up to request clarification or add additional context in comments.

2 Comments

It seems easy enough and makes perfect sense, but what my main concern is... How do I access 'date2'? Does the compiler already know to scan the next line and to read in another date? That's what I'm confused about.
If that is your problem, Inside your while loop store the previous date in a separate variable and use that variable to compare with the current variable.
0

something like:

Date former = null;
Date latter = null;
DateRange range = null;

while(in.hasNextLine()) {
  latter = readNextDate(in);
  if(isLatterLarger(former, latter)) {
    range = getDateRange(former, latter);
    doSomethingWithRange(range);
  }

  former = latter;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.