-3

Assuming we have data "September2021W5", in which W5 is the week number of the month September2021. Ofcourse I can write custom method to split and get year, month and week-number of the month seperately. How can we get daterange i.e start date and end date of that particular week. I had checked other similar questions but they all had it solved in case of weeknumber specified for the year and not month. Thanks in advance for any guidance or help provided!!!

kmj
  • 41
  • 2
  • 7
  • The reason why people are downvoting your request: "here my requirements" isn't considered a good/valid question by many. Remember: you learn programming by *first* trying yourself. So, please: always first do so some research, then try things, and then bring your code with your question. Instead of just giving us "here is what I need, now somebody else please do all the work for me" – GhostCat Jan 12 '22 at 12:35
  • Define week. Define week-of-month. – Basil Bourque Jan 13 '22 at 05:32
  • 1
    @GhostCat apologies that people felt that way, this is my first time asking question here,i have tried rephrasing the question and body for now, its just that had checked similar question earlier and it had answers limited to having weeknumber of the year and not month. Anyways will improvise myself going forward and will post only after thorough research! thanks for the advice – kmj Jan 13 '22 at 05:41
  • @BasilBourque taking example of September2021, there are in total 5 weeks: 2021-09-01 to 2021-09-06 = Week 1, 2021-09-07 to 2021-09-13 = Week 2, and so on... here in String "September2021W5" , W5 is week 5 of the month September 2021 i.e. 2021-09-27 to 2021-09-31 – kmj Jan 13 '22 at 05:50
  • Hint: as said, the point is: it is always better to first write code on your own, and bring that with you. and B) NEVER put more information into comments, always edit/update your question instead. All necessary information needs to be in the question. Dont expect the people here to dive through all the comments in order to be able to answer your question. – GhostCat Jan 13 '22 at 07:27

2 Answers2

2

Build a DateTimeFormatter that uses a default day of week as needed (start of week, e.g. Monday).
Example:

var format = new DateTimeFormatterBuilder()
             .appendPattern("MMMMuuuu'W'W")   // or use corresponding appends
             .parseDefaulting(ChronoField.DAY_OF_WEEK, DayOfWeek.MONDAY.getValue())
             .toFormatter()
             .withLocale(Locale.US);
var date = LocalDate.parse("September2021W5", format);

same can be done for the end of the week, or just adding 6 days to previous result (date.plusDays(6))


MMMM is for month of year, full form
uuuu year
'W' for literal W
W for week of month

Using Locale.UK so the month gets parsed for that locale, use the one needed for the given input - to use the default system Locale, use Locale.getDefault().

user16320675
  • 135
  • 1
  • 3
  • 9
0
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;

public class HelloWorld
{
     public static void main(String []args)
     {
        // your input date
        LocalDate localDate = LocalDate.of(2022, 9, 30);

        // ensure week 5
        LocalDate week = localDate.with(ChronoField.ALIGNED_WEEK_OF_MONTH, 5);

        // get localDate for first and last day
        LocalDate start = week.with(DayOfWeek.MONDAY);
        LocalDate end = start.plusDays(6);
        System.out.println(start +" - "+ end);
    }
}
ChrLipp
  • 15,526
  • 10
  • 75
  • 107