datetime - Is there a way to get starting date of a given weekNo of a given year Java? -
this question has answer here:
is there way using library this.
datetime getstartingdate(string year, int weekno){ //should return starting day of given weekno of given year. }
ex: year = 2016 weekno=1
datetime returning = 3rd jan in (sun-sat format) = 4th jan in (mon-sun format)
it seems want, starting point, first full week starts on given day of week (sunday or monday in example).
this achieved this:
import static java.time.temporal.temporaladjusters.nextorsame; public static localdate getstartingdate(int year, int weekno, dayofweek weekstart) { //should check arguments valid etc. return year.of(year).atday(1).with(nextorsame(weekstart)).plusdays((weekno - 1) * 7); }
or alternative:
return year.of(year).atday(1).with(aligned_week_of_year, weekno).with(nextorsame(weekstart));
and call this:
import static java.time.dayofweek.monday; import static java.time.dayofweek.sunday; system.out.println(getstartingdate(2016, 1, sunday)); //2016-01-03 system.out.println(getstartingdate(2016, 1, monday)); //2016-01-04
Comments
Post a Comment