I want to detect date of First Sunday/Monday of first/second week in every month in java?
How can I achieve it?
I have checked Calendar class as well as Date class in java but not able to get solution for that so please help me out for this.
解决方案
Java.time
Using java.time library built into Java 8:
import java.time.{DayOfWeek, LocalDate}
import java.time.temporal.TemporalAdjusters.firstInMonth
val now = LocalDate.now() # 2015-11-23
val firstMonday = now.`with`(firstInMonth(DayOfWeek.MONDAY)) # 2015-11-02 (Monday)
You may choose any day from java.time.DayOfWeek.{MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}.
If you need to add time information, you may use any available LocalDate to LocalDateTime conversion like
firstMonday.atStartOfDay() # 2015-11-02T00:00