2014-10-19 110 views
1

我希望能夠調用另一個輸入的三種方法的回報。在下面的例子中,我有其他方法從MM/DD/CCYY日期中提取月份,日期和年份。然後,dayOfWeekMethod將這些數據整理並計算出某一天的一週中的某一天。如何正確地格式化前兩行以正確地給我那個結果?謝謝!!如何從其他方法調用返回是其他方法的輸入?

這裏是我有(這是不正確的完成):

System.out.println("The date is "+MyDateTest.dayOfWeekMethod(MyDateTest.monthMethod(date), 
MyDateTest.dayMethod(date), MyDateTest.yearMethod(date)); 

public static int dayMethod(String dateInput){ 
    int day = MyDate.getDay(dateInput); 

return day; 
} 

public static int monthMethod(String dateInput){ 
    int month = MyDate.getMonth(dateInput); 

    return month; 
} 

public static int yearMethod(String dateInput){ 
    int year = MyDate.getYear(dateInput); 

    return year; 
} 

public static String dayOfWeekMethod(int month, int day, int year){ 
    String dayOfWeek = MyDate.dayofWeek(month, day, year); 

    return dayOfWeek; 
} 
+0

而錯誤信息e是? – 2014-10-19 14:36:43

+2

看來你只是寫了一些代碼出來的任何方法或塊('System.out.println'),這就是爲什麼它不編譯。另外,爲什麼你要用類的名稱給你的方法調用加上前綴,而你在這個類中是**? – Dici 2014-10-19 14:39:09

+0

那麼你可以給你的方法一個價值。例如int day = dayMethod(date); int month = monthMetod(date); int year = yearMethod(date),然後調用dayOfWeekMethod(day,month,year)。 例如:System.out.println(「日期爲」+ dayOfWeekMethod(day,moth,year)) – charen 2014-10-19 14:44:21

回答

0

無法測試,因爲我不上課指明MyDate但這應該工作,如果你的類指明MyDate作品。

public static void main(String[] args) { 
    String date = ""; 
    int day = dayMethod(date); 
    int month = monthMethod(date); 
    int year = yearMethod(date); 
    System.out.println("The date is "+dayOfWeekMethod(month,day,year)); 
} 

public static int dayMethod(String dateInput) { 
    int day = MyDate.getDay(dateInput); 

    return day; 
} 

public static int monthMethod(String dateInput) { 
    int month = MyDate.getMonth(dateInput); 

    return month; 
} 

public static int yearMethod(String dateInput) { 
    int year = MyDate.getYear(dateInput); 

    return year; 
} 

public static String dayOfWeekMethod(int month, int day, int year) { 
    String dayOfWeek = MyDate.dayofWeek(month, day, year); 

    return dayOfWeek; 
} 
0

如果我理解你的問題,你可以使用一個Calendar例如像

public static String dayOfWeekMethod(int month, int day, int year){ 
    Calendar cal = new GregorianCalendar(year, month - 1, day); 
    switch (cal.get(Calendar.DAY_OF_WEEK)) { 
    case Calendar.MONDAY: return "Monday"; 
    case Calendar.TUESDAY: return "Tuesday"; 
    case Calendar.WEDNESDAY: return "Wednesday"; 
    case Calendar.THURSDAY: return "Thursday"; 
    case Calendar.FRIDAY: return "Friday"; 
    case Calendar.SATURDAY: return "Saturday"; 
    case Calendar.SUNDAY: return "Sunday"; 
    } 
    return null; 
} 

然後你就可以用類似稱呼它,

// System.out.println("The day is "+ dayOfWeekMethod(
// MyDateTest.monthMethod(date), MyDateTest.dayMethod(date), 
// MyDateTest.yearMethod(date)); 
System.out.println(dayOfWeekMethod(10, 19, 2014)); 

輸出是(預期)

Sunday