0

我有一个任务要读取一个包含以下格式内容的文件

  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

我必须将每个日期存储到一个 Date 对象中。如果下一个日期晚于前一个日期,则应从这两个日期创建一个 DateRange 对象。最终输出应如下所示

    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

首先,我很抱歉这么长的帖子和这么多的阅读,但我一直在研究这个问题一段时间,我不知道如何继续。我被卡住了,因为我不完全确定如何比较两个日期并将它们放入 DateRange 类。我的主程序的一个片段包括:

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());
}
}

我的日期课程包括:

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;
}
}

我关心的是 Date 类,我需要比较两个日期。在我的 compareTo() 方法中,我知道它总是会返回 0,因为我没有正确实现它。我只是对如何实现这个 compareTo() 方法感到困惑,实际上将第一个日期与第二个日期进行比较,将第二个日期与第三个日期进行比较,依此类推。而且我目前没有 DateRange 课程,因为我无法测试两个日期以查看它们是否有范围。

4

2 回答 2

5

您可以尝试使用SimpleDateFormat类。它将简化您的问题。

例如,您可以先将字符串格式的日期转换为日期格式,如下所示。

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

那么对于日期之间的比较,您可以使用before(date)after(date)方法。

date.before(date2);
date.after(date2);
于 2013-09-24T02:50:36.607 回答
0

就像是:

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;
}
于 2013-09-24T03:25:47.187 回答