Need to get correct output for number of weeks in a month irrespective of what date format being used

六月ゝ 毕业季﹏ 提交于 2020-06-01 06:24:05

问题


I have this code which returns Number of week correctly.

package org.test.Calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class GetDaysInMonth {
    public static void main(String[] args) {
        Calendar calendar = GregorianCalendar.getInstance();
        int year = 2020;
        int month = Calendar.MAY;
        int date = 1;
        calendar.set(year, month, date);
        int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);

        System.out.println("Number of Days In Month: " + numOfDaysInMonth);
        System.out.println("Number of Weeks In Month: " + numOfWeeksInMonth);
    }    
}

Output :

Number of Days In Month: 31

Number of Weeks In Month: 6

My Date format was :

Now when I change my Date format to :

Output is different.

Output :

Number of Days In Month: 31

Number of Weeks In Month: 4

Need inputs like how can I get correct output irrespective of what date format are we using?

Answering to the question how am I updating the format : a) Go to Control Panel -> Clock and Region

b) Select Change date, time or number format link. You will get below popup, just change the format.


回答1:


Your results are as they should be. Since you expected something else, your expectations must have been wrong.

  • In the USA the week begins on Sunday. Week 1 of the month is the week that contains the first day of the month. So Friday May 1 and Saturday May 2 make up week 1. Week 2 begins on Sunday, May 3. Week 3 on May 10, week 4 on May 17, week 5 on May 24 and week 6 consists of just 31 of May.
  • The UK follows the international standard where the week begins on Monday and week 1 of the month is the first week that contains at least 4 days of the month. So week 1 is from Monday, May 4 through Sunday, May 10. Week 2 begins on May 11, week 3 on May 18. Week 4 is May 25 through 31 and hence the last week of the month.

In your code you didn’t query the number of weeks in the month. You queried the highest week-of-month number in the month. This is 6 in the US and 4 in the UK. Even though the month has the same length in both places.

java.time

For my demonstration I am using java.time, the modern Java date and time API, with a recommendation that you do the same in your date work. The modern classes are so much nicer to work with than the outdated Calendar class, which frankly was always poorly designed.

    YearMonth mayThisYear = YearMonth.of(2020, Month.MAY);
    LocalDate firstOfMonth = mayThisYear.atDay(1);
    LocalDate lastOfMonth = mayThisYear.atEndOfMonth();

    // US weeks
    WeekFields us = WeekFields.of(Locale.US);
    int weekOfFirstDay = firstOfMonth.get(us.weekOfMonth());
    int weekOfLastDay = lastOfMonth.get(us.weekOfMonth());
    System.out.println("US week numbers are " + weekOfFirstDay + " - " + weekOfLastDay);

Output:

US week numbers are 1 - 6

Let’s instead try for the United Kingdom of Great Britain and Northern Ireland, UK for short:

    // UK weeks
    WeekFields uk = WeekFields.of(Locale.UK);
    int weekOfFirstDay = firstOfMonth.get(uk.weekOfMonth());
    int weekOfLastDay = lastOfMonth.get(uk.weekOfMonth());
    System.out.println("UK week numbers are " + weekOfFirstDay + " - " + weekOfLastDay);

UK week numbers are 0 - 4

I should have loved to show you how to get the result that you had wanted using java.time, but even though I asked in a comment, you have not told us what you consider the correct number of weeks in a month, so I can’t.



来源:https://stackoverflow.com/questions/61537895/need-to-get-correct-output-for-number-of-weeks-in-a-month-irrespective-of-what-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!