Receive weeks of the month depending on the start of the week

I use my own calendar in my application. I gave users the opportunity to choose the first day of the week, which can be: Saturday, Sunday, Monday

I want to get the number of weeks per month - depending on when the week starts, overriding the default value for the beginning of the week - Sunday .

The code:

 public int getWeeksOfMonth(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.YEAR, year); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); return numOfWeeksInMonth; } 
+2
java android date calendar
source share
3 answers

Try

 calendar.setFirstDayOfWeek(Calendar.MONDAY); //sets first day to monday, for example. 

In your case, you can do this:

 public int getWeeksOfMonth(int year, int month, int weekStart) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.YEAR, year); calendar.setFirstDayOfWeek(weekStart); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); return numOfWeeksInMonth; } 

And call the method using a string like

 int weeks = getWeeksOfMonth(2012, 5, Calendar.WEDNESDAY); 
+2
source share

First try setting the first day of the week:

Calendar.setFirstDayOfWeek ()

+3
source share

TL; DR

 ChronoUnit.WEEKS.between( calendarStart , calendarStop ) 

Avoid obsolete classes

Avoid the problematic old time classes that are now obsolete are being superseded by modern java.time classes.

java.time

Track the day of the week with DayOfWeek enum.

 DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY ; 

Get the month of interest presented by YearMonth . Perhaps in the current month. During the current month, you must indicate the desired / expected time zone, since for any given moment, the date can be changed worldwide for the zone.

 ZoneId z = ZoneId.of( "America/Montreal" ) ; YearMonth ym = YearMonth.now( z ) ; 

Get the first and last day of the month.

 LocalDate monthStart = ym.atDay( 1 ) ; LocalDate monthStop = ym.atEndOfMonth() ; 

Get the date for the day of the week. To do this, use the TemporalAdjuster , one of which is represented by TemporalAdjusters (note the plural s ).

Time spaces in java.time are reasonably defined using the Half-Open approach, where the beginning is included, while the ending is exclusive. So, pay attention to how we need to get the date of the desired day of the week after the end of the month. If the end of the month in itself has become a desirable day of the week, we need to go past it.

 LocalDate calendarStart = monthStart.with( TemporalAdjusters.previousOrSame( firstDayOfWeek ) ) ; LocalDate calendarStop = monthStop.with( TemporalAdjusters.next( firstDayOfWeek ) ) ; 

Calculate the number of weeks between.

 long weeks = ChronoUnit.WEEKS.between( calendarStart , calendarStop ) ; 

See this code running on IdeOne.com .

firstDayOfWeek: MONDAY

ym: 2017-09

month: 2017-09-01 / 2017-09-30

calendar: 2017-08-28 / 2017-10-02

weeks: 5

September 2017 calendar calendar showing a calendar starting from Monday August 28 to Monday, October 2.


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

  • Java SE 8 , Java SE 9 , and then
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
    • See How to use ThreeTenABP ....

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source share

All Articles