英文:
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<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;
}
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<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);
// Only consider days in the current month
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论