Calculate weeks for one month in jodatime

Is it possible to calculate weeks of one month in jodatime?

I need something like this:

Month: july

  • Week 27 1-7. July
  • Week 28; 9-14. July
  • Week 29 16-21. July
  • Week of the 30th year; 23-31. July

Month: august

  • Week 31 years; 1-4. August
  • Week of the year 32; 6-11. August
  • Week of the year 33; 13-18. August
  • Week 34 years; 20-25. August
  • Week 35; 27-31. August

I know that I can get the week of the Year at this time:

new LocalDate().weekOfWeekYear()

But I do not know how to get the relevant dates.

+7
source share
4 answers

To get the range of the week, just create an object pointing to the first and last day of the week, then just pull the month out of it.

 int weekOfYear = 32; LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1); LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(7); System.out.println("Week of Year "+weekOfYear+"; "+firstDay.toString("d MMM")+" - "+lastDay.toString("d MMM")); 

You can also highlight the following day:

 int weekStart = firstDay.getDayOfMonth(); int weekEnd = lastDay.getDayOfMonth(); 

Then you can use the same technique to get weeks per month.

 int firstWeekInMonth = new LocalDate().withMonthOfYear(month).withDayOfMonth(1).getWeekOfYear(); int lastWeekInMonth = new LocalDate().withMonthOfYear(month).dayOfMonth().withMaximalValue().getWeekOfYear(); 

You can probably limit the start and end time of your stay within the month range, otherwise you could get things like September 30 - 5.

+8
source

fixing some variables:

 int weekOfYear = 32; LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1); LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(6); int weekStart = firstDay.getDayOfMonth(); int weekEnd = lastDay.getDayOfMonth(); System.out.println("Week of Year "+weekOfYear+"; "+weekStart+"-"+weekEnd+" "+month); 
+1
source

My solution, given other answers

  public List<Pair<LocalDate, LocalDate>> getWeeksInMonth(LocalDate data) { int firstWeekInMonth = data.withDayOfMonth(1).getWeekOfWeekyear(); int lastWeekInMonth = data.dayOfMonth().withMaximumValue().getWeekOfWeekyear(); List<Pair<LocalDate, LocalDate>> weeks = new cicero.minhasfinancas.util.array.ArrayList<>(); while (firstWeekInMonth <= lastWeekInMonth) { LocalDate firstDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(1); LocalDate lastDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(7); weeks.add(new Pair<>(firstDay, lastDay)); firstWeekInMonth++; } return weeks; } public class Pair<V1, V2>{ V1 first; V2 second; public Pair(V1 first, V2 second) { this.first = first; this.second = second; } public V1 getFirst() { return first; } public void setFirst(V1 first) { this.first = first; } public V2 getSecond() { return second; } public void setSecond(V2 second) { this.second = second; } } 
+1
source

I think it will also help find every week.

 String[] weekDays = {today.withDayOfWeek(DateTimeConstants.MONDAY).getDayOfMonth() +"", today.withDayOfWeek(DateTimeConstants.TUESDAY).getDayOfMonth()+ "", today.withDayOfWeek(DateTimeConstants.WEDNESDAY).getDayOfMonth() + "", today.withDayOfWeek(DateTimeConstants.THURSDAY).getDayOfMonth() + "", today.withDayOfWeek(DateTimeConstants.FRIDAY).getDayOfMonth() +"", today.withDayOfWeek(DateTimeConstants.SATURDAY).getDayOfMonth() +"", today.withDayOfWeek(DateTimeConstants.SUNDAY).getDayOfMonth() +""}; 
0
source

All Articles