How do you get the first day of week given a Locale using Joda-Time?
如何使用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?!
要点:大多数国家把星期一作为一周的第一天(!)还有一些人用星期天(尤其是美国)。其他人显然星期六。一些显然周三吗? !
Wikipedia "Seven-day week"#Week_number
*# Week_number“一周七天”
6 个解决方案
#1
44
Joda-Time uses the ISO standard Monday to Sunday week.
Joda-Time在周一到周日使用ISO标准。
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.
它没有能力获得一周的第一天,也没有能力返回基于标准周一以外的任何一天的周日指数。最后,周总是被计算为wrt ISO规则。
#2
14
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.
您没有理由不能使用JDK,至少可以找到给定语言环境的“每周开始”。唯一棘手的部分是转换工作日的常量,这两个常量都是1到7,但是java.util。日历与日历一起被移动了一个。周一= 2 vs。星期一= 1。
Anyway, use this function:
不管怎样,使用这个函数:
/**
* Gets the first day of the week, in the default locale.
*
* @return a value in the range of {@link DateTimeConstants#MONDAY} to
* {@link DateTimeConstants#SUNDAY}.
*/
private static final int getFirstDayOfWeek() {
return ((Calendar.getInstance().getFirstDayOfWeek() + 5) % 7) + 1;
}
Add a Locale to Calendar.getInstance() to get a result for some Locale other than the default.
向Calendar.getInstance()添加一个语言环境,以获得除默认语言环境之外的其他语言环境的结果。
#3
4
Here is how one might work around Joda time to get the U.S. first day of the week:
以下是一个人如何在约达时间周围工作来获得美国的第一天:
DateTime getFirstDayOfWeek(DateTime other) {
if(other.dayOfWeek.get == 7)
return other;
else
return other.minusWeeks(1).withDayOfWeek(7);
}
Or in Scala
或在Scala中
def getFirstDayOfWeek(other: DateTime) = other.dayOfWeek.get match {
case 7 => other
case _ => other.minusWeeks(1).withDayOfWeek(7)
}
#4
2
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.
看来你运气不佳,似乎所有提供的年表都继承了baseChronology的实现,它只支持ISO定义,即Monday=1…星期天= 7。
You would have to define your own LocaleChronology, possibly modeled on StrictChronology or LenientChronology, add a factory method:
你需要定义你自己的地点时间,可能是按照严格的年代学或者是从轻的年代学,添加一个工厂方法:
public static LocaleChronology getInstance(Chronology base, Locale locale)
and override the implementation of
并覆盖实现。
public final DateTimeField dayOfWeek()
with a re-implementation of java.util.Calendar.setWeekCountData(Locale desiredLocale) which relies on sun.util.resources.LocaleData..getCalendarData(desiredLocale).
通过重新实现java.util.Calendar。setWeekCountData(Locale desiredLocale),它依赖于sun.util.resources.LocaleData. getcalendar . getcalendar ardata (desiredLocale)。
#5
-1
So your question is, how to get the DayOfWeek from a Joda DateTime object? What about this:
所以你的问题是,如何从Joda DateTime对象中获得每周的一天?这个:
DateTime dt = new DateTime().withYear(2009).plusDays(111);
dt.toGregorianCalendar().getFirstDayOfWeek();
#6
-1
I used the following stub in Scala
to obtain first and last days of the week from Joda
DateTime
我在Scala中使用了下面的存根来从Joda DateTime获得第一个和最后一个星期。
val today: DateTime = new DateTime()
val dayOfWeek: DateTime.Property = today.dayOfWeek()
val firstDayOfWeek: DateTime = dayOfWeek.withMinimumValue().minusDays(1)
val lastDayOfWeek: DateTime = dayOfWeek.withMaximumValue().minusDays(1)
Note: The minusDays(1)
is only meant to make the week span from Sunday to Saturday (instead of the default Monday to Sunday known to Joda
). For US (and other similar) locales, you can ignore this part
注意:minusDays(1)仅用于从周日到周六(而不是Joda所知道的默认周一到周日)。对于我们(和其他类似的)地区,您可以忽略这一部分
#1
44
Joda-Time uses the ISO standard Monday to Sunday week.
Joda-Time在周一到周日使用ISO标准。
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.
它没有能力获得一周的第一天,也没有能力返回基于标准周一以外的任何一天的周日指数。最后,周总是被计算为wrt ISO规则。
#2
14
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.
您没有理由不能使用JDK,至少可以找到给定语言环境的“每周开始”。唯一棘手的部分是转换工作日的常量,这两个常量都是1到7,但是java.util。日历与日历一起被移动了一个。周一= 2 vs。星期一= 1。
Anyway, use this function:
不管怎样,使用这个函数:
/**
* Gets the first day of the week, in the default locale.
*
* @return a value in the range of {@link DateTimeConstants#MONDAY} to
* {@link DateTimeConstants#SUNDAY}.
*/
private static final int getFirstDayOfWeek() {
return ((Calendar.getInstance().getFirstDayOfWeek() + 5) % 7) + 1;
}
Add a Locale to Calendar.getInstance() to get a result for some Locale other than the default.
向Calendar.getInstance()添加一个语言环境,以获得除默认语言环境之外的其他语言环境的结果。
#3
4
Here is how one might work around Joda time to get the U.S. first day of the week:
以下是一个人如何在约达时间周围工作来获得美国的第一天:
DateTime getFirstDayOfWeek(DateTime other) {
if(other.dayOfWeek.get == 7)
return other;
else
return other.minusWeeks(1).withDayOfWeek(7);
}
Or in Scala
或在Scala中
def getFirstDayOfWeek(other: DateTime) = other.dayOfWeek.get match {
case 7 => other
case _ => other.minusWeeks(1).withDayOfWeek(7)
}
#4
2
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.
看来你运气不佳,似乎所有提供的年表都继承了baseChronology的实现,它只支持ISO定义,即Monday=1…星期天= 7。
You would have to define your own LocaleChronology, possibly modeled on StrictChronology or LenientChronology, add a factory method:
你需要定义你自己的地点时间,可能是按照严格的年代学或者是从轻的年代学,添加一个工厂方法:
public static LocaleChronology getInstance(Chronology base, Locale locale)
and override the implementation of
并覆盖实现。
public final DateTimeField dayOfWeek()
with a re-implementation of java.util.Calendar.setWeekCountData(Locale desiredLocale) which relies on sun.util.resources.LocaleData..getCalendarData(desiredLocale).
通过重新实现java.util.Calendar。setWeekCountData(Locale desiredLocale),它依赖于sun.util.resources.LocaleData. getcalendar . getcalendar ardata (desiredLocale)。
#5
-1
So your question is, how to get the DayOfWeek from a Joda DateTime object? What about this:
所以你的问题是,如何从Joda DateTime对象中获得每周的一天?这个:
DateTime dt = new DateTime().withYear(2009).plusDays(111);
dt.toGregorianCalendar().getFirstDayOfWeek();
#6
-1
I used the following stub in Scala
to obtain first and last days of the week from Joda
DateTime
我在Scala中使用了下面的存根来从Joda DateTime获得第一个和最后一个星期。
val today: DateTime = new DateTime()
val dayOfWeek: DateTime.Property = today.dayOfWeek()
val firstDayOfWeek: DateTime = dayOfWeek.withMinimumValue().minusDays(1)
val lastDayOfWeek: DateTime = dayOfWeek.withMaximumValue().minusDays(1)
Note: The minusDays(1)
is only meant to make the week span from Sunday to Saturday (instead of the default Monday to Sunday known to Joda
). For US (and other similar) locales, you can ignore this part
注意:minusDays(1)仅用于从周日到周六(而不是Joda所知道的默认周一到周日)。对于我们(和其他类似的)地区,您可以忽略这一部分