-3

私は、今年の週数を与える多くの関数を持っています。その年の週は、年の年の数から今年の現在の週を差し引いた差を与え、それらの差のモジュロを2で与えます。

現在の年(「2013」)と現在の日付「26/08/2013」の2つの入力を取り、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();  
    }
}
4

2 に答える 2

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) ;

}

なんのロジックもないのになんで書けないんだろう。

于 2013-08-26T10:20:19.677 に答える