简体   繁体   English

Java 日历使用 Calendar.DAY_OF_WEEK 获取特定日期的第一个和最后一个日期

[英]Java Calendar using Calendar.DAY_OF_WEEK to get the first and the last dates for a particular date

In my application there lies a code which works abruptly sometimes, its about getting a week interval using the java calendar object through Calendar.DAY_OF_WEEK.在我的应用程序中,有一个代码有时会突然工作,它是关于使用 java 日历 object 通过 Calendar.DAY_OF_WEEK 获得一周的间隔。 The code checked for monday as start of week and sunday as end of week like: fromCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);代码检查星期一作为一周的开始和星期日作为一周的结束,例如: fromCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); toCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); toCal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); the toCal is set as the last sunday instead of coming sunday. toCal 设置为最后一个星期日,而不是星期天。 Is there any alternate way to do this other then this kind of hard coding.除了这种硬编码之外,还有其他方法可以做到这一点。

Appriciate the help in advance.提前感谢帮助。

Thanks,谢谢,

Vaibhav瓦布哈夫

I guess you have to set the start of week to monday, otherwise last sunday IS the sunday of the week.我想您必须将一周的开始时间设置为星期一,否则上个星期日是一周中的星期日。

setFirstDayOfWeek setFirstDayOfWeek

public void setFirstDayOfWeek (int value)公共无效setFirstDayOfWeek (整数值)

 Sets what the first day of the week is; eg, Sunday in US, Monday in France. **Parameters:** value - the given first day of the week.

Java Calender Doc Java 日历文件

The issue is in locale.问题出在语言环境中。 In English(US), Sunday is the first day of the week.在英语(美国)中,星期日是一周的第一天。 Check this code:检查此代码:

Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
    System.out.println(cal.getTime().toString());
    cal = Calendar.getInstance(Locale.FRANCE);
    System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println(cal.getTime().toString());

Be very clear on your desired behaviour here.在这里非常清楚你想要的行为。 You start with a calendar object whose "now" is some day of the week, perhaps "today".您从日历 object 开始,其“现在”是一周中的某一天,也许是“今天”。 You the call set(DAY_OF_WEEK, ...).你的电话集(DAY_OF_WEEK,...)。 What effect do you desire if the Calendar's today is Tuesday?如果日历的今天是星期二,您希望得到什么效果? Sunday?星期日? Monday?周一?

As observed in other answers, what happens depends upon the Calendar's opinion about what the First day of week is.正如在其他答案中所观察到的,会发生什么取决于日历对一周的第一天的看法。 So first set that to your chosen value.因此,首先将其设置为您选择的值。 You will then (according to this answer get a Sunday and a Monday in the current week, which may not be what you want - what exactly do you need if today is Sunday? - some systems might actually be "thinking" about the next week.然后,您将(根据此答案在本周获得周日和周一,这可能不是您想要的-如果今天是周日,您到底需要什么?-某些系统实际上可能正在“考虑”下周.

Personally I might get my Monday according to some business rules and the get the Sunday after by adding 6 days.就个人而言,我可能会根据一些业务规则得到我的星期一,然后通过增加 6 天得到星期天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM