1

カレンダーと日付を使用してJavaで週の最初の日付を取得し、入力を::1.月の週番号2.月3.年

    Calendar cal = Calendar.getInstance();
    int weekOfMonth = Calendar.WEEK_OF_MONTH;
    System.out.println("weekOfMonth : " + weekOfMonth);

この後、私は先に進むことを知りません。

4

3 に答える 3

2

Calendar クラスには getFirstDayOfWeek() メソッドがあり、日曜日の場合は 0、月曜日の場合は 1 などの日を返します。次のコード スニペットはこれに基づいており、週の最初の日付を示します。

private static Date getFirstDateOfWeek(){
        Calendar cal = Calendar.getInstance(); 
        Date d = Calendar.getInstance().getTime();
        int currDay  =  d.getDay(); // getting the current day
        int startDay =  (currDay - cal.getFirstDayOfWeek()) + 1; // calculate the difference of number days to the current date from the first day of week
        d.setDate(d.getDate() - startDay); // setting the date accordingly.
        return d;
    }

これがあなたを助けることを願っています。

于 2012-04-12T10:04:21.957 に答える
0
int year = 2012;
int month = 3 //for April(they start from 0 = January)
int week = 2;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEEK_OF_MONTH, week);

Date date = calendar.getTime();
于 2012-04-12T08:22:55.530 に答える