Calendar オブジェクトの不要なフィールドを「切り取る」ためにApache commons-lang3 メソッドを使用しています。DateUtils.truncate(Calendar calendar, int field)
パラメータfield
が の値を取得するとCalendar.WEEK_OF_MONTH
、
java.lang.IllegalArgumentException: The field 4 is not supported
truncate()
メソッドのドキュメントには次のように書かれています。
/**
* <p>Truncates a date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the date-time of 28 Mar 2002
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
* 2002 13:00:00.000. If this was passed with MONTH, it would
* return 1 Mar 2002 0:00:00.000.</p>
*
* @param date the date to work with, not null
* @param field the field from {@code Calendar} or <code>SEMI_MONTH</code>
* @return the different truncated date, not null
* @throws IllegalArgumentException if the date is <code>null</code>
* @throws ArithmeticException if the year is over 280 million
*/
したがって、これは機能するはずですが、明らかに機能しません。DateUtils を使用して日付を週の最初の日に切り詰める方法はありますか?
更新:
ソースコードを調べたところ、modify()
メソッド ( tuncate()
this を内部で使用) が一連の定義済みフィールドを反復処理して、指定されたパラメーターを見つけることがわかりました。これらのフィールドは次のとおりです。
private static final int[][] fields = {
{Calendar.MILLISECOND},
{Calendar.SECOND},
{Calendar.MINUTE},
{Calendar.HOUR_OF_DAY, Calendar.HOUR},
{Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM
/* Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH */
},
{Calendar.MONTH, DateUtils.SEMI_MONTH},
{Calendar.YEAR},
{Calendar.ERA}};
ご覧のとおり、Calendar
の WEEK っぽいフィールドに関連するものは何もないので、これを手動で行う必要があると思います...その他の考え/推奨事項は大歓迎です!