2013-08-26 59 views
-3

我有很多給出當年,本週的週數的函數 給出當年的年數減去當前周的年數並給出它們的區別的模數兩個。將各種日期計算合併成一個函數

我想創造出採用兩個輸入,本年度(「2013」​​),單一的方法和當前日期「26/08/2013」​​,並返回它們的差模2爲0或1。

int totalWeeks = getTotalWeeksInYear(2013); 
int currentWeeks = getcurrentweekofYear("26/08/2013"); 

private int getTotalWeeksInYear(int year) { 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.YEAR, year); 
    cal.set(Calendar.MONTH, Calendar.DECEMBER); 
    cal.set(Calendar.DAY_OF_MONTH, 31); 

    int ordinalDay = cal.get(Calendar.DAY_OF_YEAR); 
    int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; // Sunday = 0 
    int numberOfWeeks = (ordinalDay - weekDay + 10)/7; 
    System.out.println(numberOfWeeks); 
    return numberOfWeeks ; 

} 

private int getcurrentweekofYear(String week) { 
    // String dtStart = "26/10/2013"; // Input date from user 
    String dtStart = week; 
    SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy"); 
    try { 
     date = format.parse(dtStart); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Calendar calender = new GregorianCalendar(); 
    calender.setTime(date); 

    return calender.get(Calendar.WEEK_OF_YEAR) ; 
} 

int diffrence = totalWeeks - currentWeeks; 
int remainder = diffrence % 2; 
if (remainder == 0) 
{ 
    Toast.makeText(this, "current year weeks is 0", 
    Toast.LENGTH_SHORT).show();  
} 
else 
{ 
    if (remainder == 1) 
    { 
     Toast.makeText(this, "current year weeks is 1" , 
         Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

我想更換這麼多的函數內部它需要兩個參數currrent年1個主要功能(「2013」​​)和CURENT日期(26/8/2013),並返回1或零 – user2706686

+1

能否請您解釋一下「迴歸有2的差異模式是0或1「 –

+0

請參閱此行int diffrence = totalWeeks -currentWeeks; int remainder = diffrence%2; – user2706686

回答

0
int result(int year, String week) { 

SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy"); 

Calendar cal1 = Calendar.getInstance(); 
cal1.set(Calendar.YEAR, year); 
cal1.set(Calendar.MONTH, Calendar.DECEMBER); 
cal1.set(Calendar.DAY_OF_MONTH, 31); 

try { 
    Date date = format.parse(week); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

Calendar cal2 = new GregorianCalendar(); 
calender.setTime(date); 

return (((cal.get(Calendar.DAY_OF_YEAR) - (cal.get(Calendar.DAY_OF_WEEK) - 1) + 10)/7) - cal2.get(Calendar.WEEK_OF_YEAR) %2) ; 

} 

雖然我想知道,沒有任何邏輯涉及,所以你不能寫它。

+0

不,我想先減去totalNumberofWeeks - 當前周結果的模式2,並檢查它是零或1 – user2706686

+0

我只想要單個函數在兩個patameter(年和當前日期)使用方法,我提出的結果= totalNumberofWeeks - currentweek取得結果%2; 返回結果; – user2706686

+0

'函數int mod(類型年,類型日期){ return getTotalWeeksInYear(year) - getCurrentWeekOfYear(date)%2;' } –

2

您可以在第一個函數中使用Calendar.WEEK_OF_YEAR獲取getTotalWeeksInYear();

int xyz(String date){ 
    // get week of year for the above date, you will get currentWeeks 
    // Split String at '/' and get the year 
    // Create new calendar with the for December 31 and year from above splitting 
    // again get week of year , you will get totalWeeks 
    int diffrence= totalWeeks -currentWeeks; 
    int remainder = diffrence % 2; 
    Toast.makeText(this, "current year weeks is "+remainder , 
    Toast.LENGTH_SHORT).show(); 
    return remainder ; 
} 
+0

ü得到我的觀點,我說,但我想要一個主要方法裏面的所有tewo方法,並採取兩個參數(intyear,mdate) 返回最終值 – user2706686

+1

@ user2706686爲什麼你不能通過你自己做到這一點? –

+0

你可以避免年份參數,因爲它已經存在,除了計算不同的年份和不同的日期沒有意義,所以我只使用了一個參數,即當前日期。然後使用String拆分它,split(「/」)這會給你一個所有令牌的數組,並且year絕對是最後一個令牌,所以你使用(array.length-1)來獲得年份並進行其他計算 –

相關問題