使用 Joda-Time 的区域设置的一周开始时间
要点:大多数国家/地区使用国际标准星期一作为一周的第一天(!)。其他一些人则使用周日(尤其是美国)。其他人显然是星期六。有些显然是星期三?!
How do you determine which day of the week is considered the “start” according to a given Locale using Joda-Time?
Point: Most countries use the international standard Monday as first day of week (!). A bunch others use Sunday (notably USA). Others apparently Saturday. Some apparently Wednesday?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Joda-Time 使用 ISO 标准周一至周日周。
它无法获取一周的第一天,也无法根据标准星期一以外的任何一天返回星期几索引。最后,周数始终根据 ISO 规则计算。
Joda-Time uses the ISO standard Monday to Sunday week.
It does not have the ability to obtain the first day of week, nor to return the day of week index based on any day other than the standard Monday. Finally, weeks are always calculated wrt ISO rules.
您没有理由不能使用 JDK 至少找到给定区域设置的“习惯的一周开始时间”。唯一棘手的部分是翻译工作日常量,两者都是 1 到 7,但 java.util.Calendar 移动了 1,Calendar.MONDAY = 2 与 DateTimeConstants.MONDAY = 1。
无论如何,请使用此函数
: Locale 到 Calendar.getInstance() 以获取除默认值之外的某些区域设置的结果。
There's no reason you can't make use of the JDK at least to find the "customary start of the week" for the given Locale. The only tricky part is translating constants for weekdays, where both are 1 through 7, but java.util.Calendar is shifted by one, with Calendar.MONDAY = 2 vs. DateTimeConstants.MONDAY = 1.
Anyway, use this function:
Add a Locale to Calendar.getInstance() to get a result for some Locale other than the default.
以下是如何在 Joda 时间附近获取美国一周的第一天:
或者在 Scala 中
Here is how one might work around Joda time to get the U.S. first day of the week:
Or in Scala
看来你不走运,看起来所有提供的 Chronologies 都继承了 baseChronology 的实现,它只支持 ISO 定义,
即星期一=1 ...星期日=7。
您必须定义自己的 LocaleChronology(可能以 StrictChronology 或 LenientChronology 为模型),添加一个工厂方法:
覆盖 的实现
并使用依赖于 sun.util 的 java.util.Calendar.setWeekCountData(LocaledesiredLocale) 的重新实现来 。 resources.LocaleData..getCalendarData(desiredLocale)。
Seems like you're out of luck, it looks like all of the provided Chronologies inherit the implementation from baseChronology, which supports only ISO definitions,
i.e. Monday=1 ... Sunday=7.
You would have to define your own LocaleChronology, possibly modeled on StrictChronology or LenientChronology, add a factory method:
and override the implementation of
with a re-implementation of java.util.Calendar.setWeekCountData(Locale desiredLocale) which relies on sun.util.resources.LocaleData..getCalendarData(desiredLocale).
这是我想出来的。
startOfWeek
将始终是星期日的开始,endOfweek
将始终是星期六的结束(从星期一开始)。This is what I came up with. The
startOfWeek
will always be the start of a Sunday and theendOfweek
will always be an end of a Saturday(Start a of Monday).下面是动态获取一周开始和结束日期的 Scala 代码。
注意:- 确保开始和结束按顺序排列,例如,当开始日是
星期日
时,星期一
是一周的结束日输出:- 对于
2021 年 1 月 5 日
一周的开始日期是星期四
,一周的结束日期是星期三
,那么一周的开始日期是2020-12-31< /code> 并以
2021-01-06
结尾。Here is the Scala code to get start and end day of week dynamically.
Note:- Make sure start and end are in order for example when start day is
Sunday
thenMonday
is end day of weekOutput:- For
5th Jan 2021
start day of week isThursday
and end day of week isWednesday
then week begins with2020-12-31
and end with2021-01-06
.所以你的问题是,如何从 Joda DateTime 对象获取 DayOfWeek ?这个怎么样:
So your question is, how to get the DayOfWeek from a Joda DateTime object? What about this:
我在 Scala 中使用以下存根从
Joda
DateTime
获取一周的第一天和最后一天注意:
minusDays(1)
仅用于使周跨度从周日到周六(而不是已知的默认周一到周日乔达
)。对于美国(和其他类似的)语言环境,您可以忽略这部分I used the following stub in
Scala
to obtain first and last days of the week fromJoda
DateTime
Note: The
minusDays(1)
is only meant to make the week span from Sunday to Saturday (instead of the default Monday to Sunday known toJoda
). For US (and other similar) locales, you can ignore this part