获取Android中当前月份的星期范围

huangapple go评论64阅读模式
英文:

Get week range of current month in Android

问题

在MPAndroidChart图表中显示周范围是您的需求。例如,在二月份,您想要在x轴标签上显示值,如1-4,5-11,12-18,19-25,26-28。这里的1-4来自于二月的第一周,之前的月份日期也是可用的。但我只需要当前月的日期。然而,我得到了整个周的日期。

public List<String> getWeeksInCurrentMonth() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int month = cal.get(Calendar.MONTH);

    List<String> weekRanges = new ArrayList<>();

    while (cal.get(Calendar.MONTH) == month) {
        int week = cal.get(Calendar.WEEK_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        if (dayOfWeek == Calendar.SUNDAY || cal.getActualMaximum(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) {
            int startDay = cal.get(Calendar.DAY_OF_MONTH) - (dayOfWeek - 1);
            int endDay = cal.get(Calendar.DAY_OF_MONTH) + (7 - dayOfWeek);

            if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }

            if (startDay <= endDay && startDay <= cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                weekRanges.add(String.format("%d-%d", startDay, endDay));
            }
        }

        cal.add(Calendar.DAY_OF_MONTH, 1);
    }

    System.out.println(weekRanges);
    return weekRanges;
}

在美国地区运行时的观察输出:

[5-11, 12-18, 19-25, 26-28, 26-28]

看起来第一周缺失了。

请有人在这里提供一些解决方案,以实现只包括当前月的日期的周范围。

英文:

I would like to show the week range in the MPAndroidChart Graph. For example in February month I would like to show the xAxis label value like 1-4, 5-11, 12- 18, 19-25, 26-28. Here 1-4 comes from the 1st week of February where previous month dates also availble. But I need only current month days. However I am getting all the dates in the week.

 public List&lt;String&gt; getWeeksInCurrentMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int month = cal.get(Calendar.MONTH);
List&lt;String&gt; weekRanges = new ArrayList&lt;&gt;();
while (cal.get(Calendar.MONTH) == month) {
int week = cal.get(Calendar.WEEK_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || cal.getActualMaximum(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) {
int startDay = cal.get(Calendar.DAY_OF_MONTH) - (dayOfWeek - 1);
int endDay = cal.get(Calendar.DAY_OF_MONTH) + (7 - dayOfWeek);
if (endDay &gt; cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if (startDay &lt;= endDay &amp;&amp; startDay &lt;= cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
weekRanges.add(String.format(&quot;%d-%d&quot;, startDay, endDay));
}
}
cal.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(weekRanges);
return weekRanges;
}

Observed output when run in US locale:

> [5-11, 12-18, 19-25, 26-28, 26-28]

It seems that the first week is missing.

Someone please shed some light here, to acheive the week range with only current month date.

答案1

得分: 2

为了获得仅包含当前月份日期的星期范围,您可以修改您的getWeeksInCurrentMonth()方法中的逻辑,以考虑当前月份并排除来自上个月或下个月的日期。以下是一个更新后的实现,应该能够实现所需的行为:

public List<String> getWeeksInCurrentMonth() {
    Calendar cal = Calendar.getInstance();
    int currentMonth = cal.get(Calendar.MONTH);
    int currentYear = cal.get(Calendar.YEAR);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    List<String> weekRanges = new ArrayList<>();

    while (cal.get(Calendar.MONTH) == currentMonth) {
        int week = cal.get(Calendar.WEEK_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        // 仅考虑当前月份的日期
        if (cal.get(Calendar.MONTH) == currentMonth && cal.get(Calendar.YEAR) == currentYear) {
            int startDay = cal.get(Calendar.DAY_OF_MONTH);
            int endDay = startDay + (7 - dayOfWeek);

            if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }

            weekRanges.add(String.format("%d-%d", startDay, endDay));
        }

        cal.add(Calendar.DAY_OF_MONTH, 7 - dayOfWeek + 1);
    }

    System.out.println(weekRanges);
    return weekRanges;
}

请注意,这是一个Java代码示例,用于获取当前月份的星期范围,并排除了上个月或下个月的日期。

英文:

To get the week ranges with only current month dates, you can modify the logic in your getWeeksInCurrentMonth() method to take the current month into account and exclude any dates from previous or next months. Here's an updated implementation that should achieve the desired behavior:

public List&lt;String&gt; getWeeksInCurrentMonth() {
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH);
int currentYear = cal.get(Calendar.YEAR);
cal.set(Calendar.DAY_OF_MONTH, 1);
List&lt;String&gt; weekRanges = new ArrayList&lt;&gt;();
while (cal.get(Calendar.MONTH) == currentMonth) {
int week = cal.get(Calendar.WEEK_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
// Only consider days in the current month
if (cal.get(Calendar.MONTH) == currentMonth &amp;&amp; cal.get(Calendar.YEAR) == currentYear) {
int startDay = cal.get(Calendar.DAY_OF_MONTH);
int endDay = startDay + (7 - dayOfWeek);
if (endDay &gt; cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
weekRanges.add(String.format(&quot;%d-%d&quot;, startDay, endDay));
}
cal.add(Calendar.DAY_OF_MONTH, 7 - dayOfWeek + 1);
}
System.out.println(weekRanges);
return weekRanges;
}

huangapple
  • 本文由 发表于 2023年2月19日 22:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定