java - Get first Monday and last Sunday of Month, crossing Month boundary -


i want first monday , last sunday in weeks overlapping current month, if monday or sunday aren't current month.

for example, month 2017-04 (april), first monday march 27 , last sunday april 30.

the temporaladjuster interface provides classes manipulate date-time values. temporaladjusters class provides several handy implementations.

the strategy first determine current date. requires time zone. given moment, date varies around globe zone.

from today’s date, first , last days of current month. each of ask previous or next occurrence of our desired day-of-week. accept either first or last of month may desired day-of-week “orsame” means in temporaladjusters calls seen below.

zoneid z = zoneid.of( "america/montreal" ); localdate today = localdate.now( z );  localdate firstofmonth = today.with( temporaladjusters.firstdayofmonth() ); localdate monday = firstofmonth.with( temporaladjusters.previousorsame( dayofweek.monday ) );  localdate endofmonth = today.with( temporaladjusters.lastdayofmonth() ); localdate sunday = endofmonth.with( temporaladjusters.nextorsame( dayofweek.sunday ) ); 

by way, if need represent entire month, @ yearmonth class.


Comments