今月の第三日曜日は?
Date and Time APIの場合。
import static java.time.DayOfWeek.SUNDAY; import java.time.LocalDate; import static java.time.temporal.TemporalAdjusters.dayOfWeekInMonth; public class ThirdSunday { public static void main(String[] args) { System.out.println( LocalDate.now().with(dayOfWeekInMonth(3, SUNDAY)) ); } }
java.time.temporal.TemporalAdjusters
クラスにdayOfWeekInMonth
という便利TemporalAdjuster
を提供してくれるメソッドがある。
$ java ThirdSunday 2016-03-20 $ cal 3月 2016 日 月 火 水 木 金 土 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21(22)23 24 25 26 27 28 29 30 31