我有很多函数,它们给出当年的周数,一年中的周给出一年中的年数减去一年中的当前周的差值,并将它们的差模取为二。
我想创建一个方法,它接受两个输入,即当前年份(“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();
}
}