我有很多給出當年,本週的週數的函數 給出當年的年數減去當前周的年數並給出它們的區別的模數兩個。將各種日期計算合併成一個函數
我想創造出採用兩個輸入,本年度(「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();
}
}
我想更換這麼多的函數內部它需要兩個參數currrent年1個主要功能(「2013」)和CURENT日期(26/8/2013),並返回1或零 – user2706686
能否請您解釋一下「迴歸有2的差異模式是0或1「 –
請參閱此行int diffrence = totalWeeks -currentWeeks; int remainder = diffrence%2; – user2706686