我有很多功能可以提供当年的周数,一年中的一周 给出一年中当年的年数减去当前一周的年份差异,并将它们的差值模数减去两个。
我想创建单个方法,它接受两个输入,当前年份(“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 :(得分:2)
您可以在getTotalWeeksInYear()的第一个函数中使用Calendar.WEEK_OF_YEAR;
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 ;
}
答案 1 :(得分: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) ;
}
虽然我不知道,但是没有任何逻辑,所以为什么你不能写它。