无论使用哪种日期格式,都需要获得一个月中星期数的正确输出 [英] Need to get correct output for number of weeks in a month irrespective of what date format being used

查看:96
本文介绍了无论使用哪种日期格式,都需要获得一个月中星期数的正确输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以正确返回星期数.

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);
    }    
}

输出:

每月的天数:31

每月的周数:6

我的日期格式为:

现在,当我将日期格式更改为:

Now when I change my Date format to :

输出是不同的.

输出:

每月的天数:31

每月的周数:4

需要诸如之类的输入,无论我们使用哪种日期格式,我如何都能获得正确的输出??

回答我如何更新格式的问题: a)转到控制面板->时钟和区域

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

b)选择更改日期,时间或数字格式链接. 您将获得以下弹出窗口,只需更改格式.

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

推荐答案

您的结果符合预期.既然您还期望其他东西,那么您的期望肯定是错误的.

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

  • 在美国,一周从星期日开始.该月的第一周是包含该月第一天的一周.因此,5月1日星期五和5月2日星期六构成第1周.第2周从5月3日星期日开始.5月10日第3周,5月17日第4周,5月24日第5周,第6周仅由5月31日组成./li>
  • 英国遵循国际标准,该周从星期一开始,该月的第1周是包含该月至少4天的第一周.因此,第1周是从5月4日星期一到5月10日星期日.第2周从5月11日开始,在5月18日是第3周.第4周是从5月25日到31日,因此是该月的最后一周.
  • 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.

在您的代码中,您没有查询每月的星期数.您查询了当月最高的星期几.在美国是6,在英国是4.即使两个地方的月份长度相同.

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日期和时间API java.time,建议您在日期工作中也这样做.与过时的Calendar类相比,现代类的使用要好得多,坦白地说,该类总是设计得很差.

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);

输出:

美国周数是1-6

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);

英国周数是0-4

UK week numbers are 0 - 4

我本该喜欢向您展示如何使用java.time获得所需的结果,但是即使我在评论中提出要求,您也没有告诉我们您认为一个月中正确的星期数是多少,所以我不能.

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.

这篇关于无论使用哪种日期格式,都需要获得一个月中星期数的正确输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆