2013-09-24 75 views
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類,因爲我無法測試兩個日期以查看它們是否有範圍。

+0

有關轉換日期以毫秒什麼,然後減去他們。例如:date1.getTime() - date2.getTime() –

+1

首先使用帶有'MMMM d,yyyy'(可能)格式的'SimpleDateFormat'來解析'String'到'Date's。然後使用'日期#之前'或'日期#之後'來確定日期之間的關係... – MadProgrammer

+0

那麼我主要關心的是如何實際訪問date1和date2在同一時間?我可以比較它們,我只是不知道如何同時訪問它們 – DLR

回答

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); 
+0

這看起來很簡單,而且非常有意義,但我主要關心的是......如何訪問'date2'?編譯器是否已知道掃描下一行並在另一日期讀取?這就是我所困惑的。 – DLR

+1

如果這是您的問題,那麼在while循環中將先前日期存儲在單獨的變量中,並使用該變量與當前變量進行比較。 – Jayamohan

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