使用 Joda-Time 的区域设置的一周开始时间

发布于 2024-08-12 03:40:19 字数 429 浏览 5 评论 0原文

如何根据给定的 区域设置使用Joda-Time

要点:大多数国家/地区使用国际标准星期一作为一周的第一天(!)。其他一些人则使用周日(尤其是美国)。其他人显然是星期六。有些显然是星期三?!

维基百科“七天周”#Week_number

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?!

Wikipedia "Seven-day week"#Week_number

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

恋竹姑娘 2024-08-19 03:40:19

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.

盗心人 2024-08-19 03:40:19

您没有理由不能使用 JDK 至少找到给定区域设置的“习惯的一周开始时间”。唯一棘手的部分是翻译工作日常量,两者都是 1 到 7,但 java.util.Calendar 移动了 1,Calendar.MONDAY = 2 与 DateTimeConstants.MONDAY = 1。

无论如何,请使用此函数

/**
 * 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;
}

: 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:

/**
 * 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.

国粹 2024-08-19 03:40:19

以下是如何在 Joda 时间附近获取美国一周的第一天:

DateTime getFirstDayOfWeek(DateTime other) {
  if(other.dayOfWeek.get == 7)
    return other;
  else
    return other.minusWeeks(1).withDayOfWeek(7);
}

或者在 Scala 中

def getFirstDayOfWeek(other: DateTime) = other.dayOfWeek.get match {
    case 7 => other
    case _ => other.minusWeeks(1).withDayOfWeek(7)
}

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

def getFirstDayOfWeek(other: DateTime) = other.dayOfWeek.get match {
    case 7 => other
    case _ => other.minusWeeks(1).withDayOfWeek(7)
}
青巷忧颜 2024-08-19 03:40:19

看来你不走运,看起来所有提供的 Chronologies 都继承了 baseChronology 的实现,它只支持 ISO 定义,
即星期一=1 ...星期日=7。

您必须定义自己的 LocaleChronology(可能以 StrictChronology 或 LenientChronology 为模型),添加一个工厂方法:

public static LocaleChronology getInstance(Chronology base, Locale locale)

覆盖 的实现

public final DateTimeField dayOfWeek()

并使用依赖于 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:

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).

小瓶盖 2024-08-19 03:40:19

这是我想出来的。 startOfWeek 将始终是星期日的开始,endOfweek 将始终是星期六的结束(从星期一开始)。

DateTime startOfWeek;
DateTime endOfWeek;
// make sure Sunday is the first day of the week, not Monday
if (dateTime.getDayOfWeek() == 7) {
    startOfWeek = dateTime.plusDays(1).weekOfWeekyear().roundFloorCopy().minusDays(1);
    endOfWeek = dateTime.plusDays(1).weekOfWeekyear().roundCeilingCopy().minusDays(1);
} else {
    startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy().minusDays(1);
    endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy().minusDays(1);
}

This is what I came up with. The startOfWeek will always be the start of a Sunday and the endOfweek will always be an end of a Saturday(Start a of Monday).

DateTime startOfWeek;
DateTime endOfWeek;
// make sure Sunday is the first day of the week, not Monday
if (dateTime.getDayOfWeek() == 7) {
    startOfWeek = dateTime.plusDays(1).weekOfWeekyear().roundFloorCopy().minusDays(1);
    endOfWeek = dateTime.plusDays(1).weekOfWeekyear().roundCeilingCopy().minusDays(1);
} else {
    startOfWeek = dateTime.weekOfWeekyear().roundFloorCopy().minusDays(1);
    endOfWeek = dateTime.weekOfWeekyear().roundCeilingCopy().minusDays(1);
}
℉服软 2024-08-19 03:40:19

下面是动态获取一周开始和结束日期的 Scala 代码。

注意:- 确保开始和结束按顺序排列,例如,当开始日是星期日时,星期一是一周的结束日

def shiftWeeksBy(startDayOfWeek: Int, currentWeekDay: Int) : Int = (startDayOfWeek, currentWeekDay) match {
  case (s, c) if s <= c  | s == 1 => 0 //start day of week is Monday -OR- start day of week <= current week day
  case _ => 1
}

def getStartAndEndDays(initialTime: DateTime, startDayOfWeek: Int, endDayOfWeek: Int): (Option[DateTime], Option[DateTime]) = {
   val currentDateWeekDay = initialTime.dayOfWeek.get
   (Some(initialTime.minusWeeks(shiftWeeksBy(startDayOfWeek, currentDateWeekDay)).withDayOfWeek(startDayOfWeek).withTimeAtStartOfDay()),
       Some(initialTime.plusWeeks(shiftWeeksBy(currentDateWeekDay, endDayOfWeek)).withDayOfWeek(endDayOfWeek)))
}

输出:- 对于2021 年 1 月 5 日 一周的开始日期是 星期四,一周的结束日期是 星期三,那么一周的开始日期是 2020-12-31< /code> 并以 2021-01-06 结尾。

scala> getStartAndEndDays(new DateTime("2021-01-05T00:00:00.000"), 4, 3)
res5: (Option[org.joda.time.DateTime], Option[org.joda.time.DateTime]) = (Some(2020-12-31T00:00:00.000+05:30),Some(2021-01-06T00:00:00.000+05:30))

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 then Monday is end day of week

def shiftWeeksBy(startDayOfWeek: Int, currentWeekDay: Int) : Int = (startDayOfWeek, currentWeekDay) match {
  case (s, c) if s <= c  | s == 1 => 0 //start day of week is Monday -OR- start day of week <= current week day
  case _ => 1
}

def getStartAndEndDays(initialTime: DateTime, startDayOfWeek: Int, endDayOfWeek: Int): (Option[DateTime], Option[DateTime]) = {
   val currentDateWeekDay = initialTime.dayOfWeek.get
   (Some(initialTime.minusWeeks(shiftWeeksBy(startDayOfWeek, currentDateWeekDay)).withDayOfWeek(startDayOfWeek).withTimeAtStartOfDay()),
       Some(initialTime.plusWeeks(shiftWeeksBy(currentDateWeekDay, endDayOfWeek)).withDayOfWeek(endDayOfWeek)))
}

Output:- For 5th Jan 2021 start day of week is Thursday and end day of week is Wednesday then week begins with 2020-12-31 and end with 2021-01-06.

scala> getStartAndEndDays(new DateTime("2021-01-05T00:00:00.000"), 4, 3)
res5: (Option[org.joda.time.DateTime], Option[org.joda.time.DateTime]) = (Some(2020-12-31T00:00:00.000+05:30),Some(2021-01-06T00:00:00.000+05:30))
憧憬巴黎街头的黎明 2024-08-19 03:40:19

所以你的问题是,如何从 Joda DateTime 对象获取 DayOfWeek ?这个怎么样:

DateTime dt = new DateTime().withYear(2009).plusDays(111);
dt.toGregorianCalendar().getFirstDayOfWeek();

So your question is, how to get the DayOfWeek from a Joda DateTime object? What about this:

DateTime dt = new DateTime().withYear(2009).plusDays(111);
dt.toGregorianCalendar().getFirstDayOfWeek();
-小熊_ 2024-08-19 03:40:19

我在 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)

获取一周的第一天和最后一天注意minusDays(1) 仅用于使周跨度从周日到周六(而不是 已知的默认周一到周日乔达)。对于美国(和其他类似的)语言环境,您可以忽略这部分

I used the following stub in Scala to obtain first and last days of the week from 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文