2011-12-28 11 views
7

メソッドに渡された週番号の開始日である&終了日を取得したいとします。私は2011として51として週番号と年を渡した場合、それは私が24 Dec 2011アンドロイドで週番号と年の開始日と終了日を取得します

として18 Dec 2011と終了日と開始日を返す必要があり、私はこれを達成するのに役立ちます任意の方法はありますか?

+0

私はMonthDisplayHelperとJodaTimeを使用してみましたが、何とか必要なものを達成できませんでした。 Thanx Sunil&Chase .. URソリューションを試してみましょう.2015年の – AndroidGuy

答えて

19

あなたがjava.util.Calendarクラスを使用する必要が

void getStartEndOFWeek(int enterWeek, int enterYear){ 
//enterWeek is week number 
//enterYear is year 
     Calendar calendar = Calendar.getInstance(); 
     calendar.clear(); 
     calendar.set(Calendar.WEEK_OF_YEAR, enterWeek); 
     calendar.set(Calendar.YEAR, enterYear); 

     SimpleDateFormat formatter = new SimpleDateFormat("ddMMM yyyy"); // PST` 
     Date startDate = calendar.getTime(); 
     String startDateInStr = formatter.format(startDate); 
     System.out.println("...date..."+startDateInStr); 

     calendar.add(Calendar.DATE, 6); 
     Date enddate = calendar.getTime(); 
     String endDaString = formatter.format(enddate); 
     System.out.println("...date..."+endDaString); 
    } 
+0

年、2016年はさらに1週間来ますが、2013,2014はまさに...これは閏年の問題です。 –

+0

こんにちはKamal、上記の解決方法上記の問題? – Deva

3

週の最初の日と終了日を取得するには、次の方法を使用することができます。 public void set(int field, int value)メソッドを使用してと年をCalendar.WEEK_OF_YEARに設定することができます。

ロケールが正しく設定されている場合は、setFirstDayOfWeekを使用して週の最初の曜日を変更することもできます。カレンダーインスタンスによって表される日付が開始日になります。終了日に6日を追加するだけです。

Calendar calendar = new GregorianCalendar(); 
// Clear the calendar since the default is the current time 
calendar.clear(); 
// Directly set year and week of year 
calendar.set(Calendar.YEAR, 2011); 
calendar.set(Calendar.WEEK_OF_YEAR, 51); 
// Start date for the week 
Date startDate = calendar.getTime(); 
// Add 6 days to reach the last day of the current week 
calendar.add(Calendar.DAY_OF_YEAR, 6); 
// End date for the week 
Date endDate = calendar.getTime(); 
関連する問題