実際DAYOFのチェックをスキップしますここにはJoda-Timeのソリューション(Java 8の2セントもあります)があります。
私が正しく理解している場合、前の日曜日 n番目を取得するための一般的なアルゴリズムである、:
- 現在の日付がすでに日曜日であれば
n=1
のために、それを返すので、(n-1
週間戻って同じ日付)他
- 、私が受け取るメソッドを作成したその日
から番目の前の日曜日のnを見つけますn
(週数)およびDateTime
(開始日)。コードは
// get the n'th previous Sunday, from the given DateTime
public DateTime nPreviousSunday(int n, DateTime dateTime) {
// avoid zero or negative numbers (optional, see if it fits your use cases)
if (n <= 0) {
return dateTime; // return the same date
}
DateTime d = dateTime;
// get first previous (or same) Sunday
int dow = d.getDayOfWeek();
if (dow != DateTimeConstants.SUNDAY) { // not a Sunday, adjust the day to the previous one
int diff = DateTimeConstants.SUNDAY - dow;
// DateTimeConstants.SUNDAY is 7, so diff is always positive
// d is (7 - diff) days ahead of Sunday, adjusting
d = d.minusDays(7 - diff);
}
// find the n'th previous (considering that the first was already found above)
d = d.minusWeeks(n - 1);
return d;
}
です。以下は04/06/2017
(日曜日)のテストです。n=1
のために、それは04/06/2017
を返し、n >= 2
のためには、(04/06/2017
自体は最初のものであることを考慮して)その日から、前の日曜日 n番目を見つける:
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 4, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(2, new DateTime(2017, 6, 4, 10, 0))); // 2017-05-28
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 4, 10, 0))); // 2017-05-21
05/06/2017
のテスト(ない日曜日)、それ(最初の前の日曜日は04/06/2017
あると)同じ結果を得る:土曜日までに一週間のための
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 5, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(2, new DateTime(2017, 6, 5, 10, 0))); // 2017-05-28
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 5, 10, 0))); // 2017-05-21
テスト(10/06/2017
):
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 6, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 7, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 8, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 9, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(1, new DateTime(2017, 6, 10, 10, 0))); // 2017-06-04
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 6, 10, 0))); // 2017-05-21
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 7, 10, 0))); // 2017-05-21
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 8, 10, 0))); // 2017-05-21
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 9, 10, 0))); // 2017-05-21
System.out.println(nPreviousSunday(3, new DateTime(2017, 6, 10, 10, 0))); // 2017-05-21
PS:私はDateTime
を使用していますが、あなたはまた(ちょうどメソッドで変数の型を変更するアルゴリズムは同じである)org.joda.time.LocalDate
またはorg.joda.time.LocalDateTime
のためにこのコードを使用することができます。
のJava 8のアプローチ(私の2セント)
のJava 8では、あなたはalready answeredとしてTemporalAdjuster
を使用することができます。しかし、ちょうど私の2セントを入れて、あなたはTemporalAdjuster
を返すメソッドを作成することができ、その後、あなたは、java-time型のいずれかでそれを使用することができます。
:だから、あなたはこのようにそれを使用することができ
// get the n'th previous dayOfWeek, from the given temporal
public TemporalAdjuster previous(int n, DayOfWeek dayOfWeek) {
return (temporal) -> {
// avoid zero or negative numbers (optional, see if it fits your use cases)
if (n <= 0) {
return temporal; // return the same temporal
}
// get first previous (or same) dayOfWeek
Temporal t = temporal.with(TemporalAdjusters.previousOrSame(dayOfWeek));
// find the n'th previous (considering that the first was already found above)
t = t.minus(n - 1, ChronoUnit.WEEKS);
return t;
};
}
System.out.println(LocalDate.of(2017, 6, 4).with(previous(1, DayOfWeek.SUNDAY))); // 2017-06-04
System.out.println(LocalDate.of(2017, 6, 4).with(previous(2, DayOfWeek.SUNDAY))); // 2017-05-28
System.out.println(LocalDate.of(2017, 6, 4).with(previous(3, DayOfWeek.SUNDAY))); // 2017-05-21
System.out.println(LocalDate.of(2017, 6, 5).with(previous(1, DayOfWeek.SUNDAY))); // 2017-06-04
System.out.println(LocalDate.of(2017, 6, 5).with(previous(2, DayOfWeek.SUNDAY))); // 2017-05-28
System.out.println(LocalDate.of(2017, 6, 5).with(previous(3, DayOfWeek.SUNDAY))); // 2017-05-21
良いことは、それはまた別の種類で動作するということです。
LocalDateTime dt = LocalDateTime.of(2017, 6, 4, 10, 0);
System.out.println(dt.with(previous(1, DayOfWeek.SUNDAY))); // 2017-06-04T10:00
System.out.println(dt.with(previous(2, DayOfWeek.SUNDAY))); // 2017-05-28T10:00
ZonedDateTime zdt = ZonedDateTime.of(dt, ZoneId.of("America/Sao_Paulo"));
System.out.println(zdt.with(previous(1, DayOfWeek.SUNDAY))); // 2017-06-04T10:00-03:00[America/Sao_Paulo]
System.out.println(zdt.with(previous(2, DayOfWeek.SUNDAY))); // 2017-05-28T10:00-03:00[America/Sao_Paulo]
OffsetDateTime odt = OffsetDateTime.of(dt, ZoneOffset.ofHours(2));
System.out.println(odt.with(previous(1, DayOfWeek.SUNDAY))); // 2017-06-04T10:00+02:00
System.out.println(odt.with(previous(2, DayOfWeek.SUNDAY))); // 2017-05-28T10:00+02:00
previous()
方法はTemporalAdjuster
を返すと、あなたは毎回それを呼び出す必要はありません、チュSTストア変数で調整して再利用:
TemporalAdjuster thirdPreviousSunday = previous(3, DayOfWeek.SUNDAY);
System.out.println(LocalDate.of(2017, 6, 4).with(thirdPreviousSunday)); // 2017-05-21
System.out.println(LocalDate.of(2017, 6, 5).with(thirdPreviousSunday)); // 2017-05-21
このアプローチのもう一つの利点は、以下のとおりです。コードは(IMO)は、より読みやすく、きれいになり、それが週の任意の日で動作します。
PS:タイプ(のみ時/分/秒/ナノ秒を有しLocalTime
等)DayOfWeek
フィールドを持っていない場合以下のコードは例外をスロー:
// throws UnsupportedTemporalTypeException (because LocalTime doesn't have the DayOfWeek field)
LocalTime.now().with(previous(1, DayOfWeek.SUNDAY));
として、
// also throws exception (Unsupported field: DayOfWeek)
LocalTime.now().with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));
それは理にかなって:
はちょうどそれは、既存の調整で発生することを思い出させますには日付フィールドがなく、平日については知ることができません。
https://docs.oracle.com/javase/tutorial/datetime/でチュートリアルを読んでから、試してみることをお勧めします。 –
Joda-TimeよりもJava 8を好むべきだと思われます。 [The Joda-Time homepage](http://www.joda.org/joda-time/)では、「ユーザーは現在、java.time(JSR-310)に移行するように求められています。」 –
検索とリサーチ持ってくる?あなたは何を試しましたか(どのように失敗しましたか)? –