Java Joda 时间:一周的第一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1801907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:47:20  来源:igfitidea点击:

Joda Time: First day of week?

javacalendarjodatimeweekday

提问by stolsvik

How do you get the first day of week given a Localeusing Joda-Time?

您如何使用Joda-Time 在给定Locale 的情况下获得一周的第一天?

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

采纳答案by JodaStephen

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.

它无法获取一周的第一天,也无法根据标准星期一以外的任何一天返回星期几索引。最后,周数总是按照 ISO 规则计算的。

回答by Tim Büthe

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

所以你的问题是,如何从 Joda DateTime 对象中获取 DayOfWeek?那这个呢:

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

回答by crowne

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.

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

You would have to define your own LocaleChronology, possibly modeled on StrictChronology or LenientChronology, add a factory method:

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

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

重新实现依赖于 sun.util.resources.LocaleData..getCalendarData(desiredLocale) 的 java.util.Calendar.setWeekCountData(Locale desiredLocale)。

回答by Mark Renouf

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 来为给定的 Locale 找到“一周的习惯开始”。唯一棘手的部分是转换工作日的常量,两者都是 1 到 7,但 java.util.Calendar 移位了 1,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.

将 Locale 添加到 Calendar.getInstance() 以获取除默认值之外的某些 Locale 的结果。

回答by Cory Klein

Here is how one might work around Joda time to get the U.S. first day of the week:

以下是人们如何绕过 Joda 时间来获得美国一周的第一天:

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

回答by y2k-shubham

I used the following stubin Scalato obtain first and last days of the week from JodaDateTime

我用下面的存根Scala获得第一天和最后一天的一周,从JodaDateTime

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)。对于美国(和其他类似的)语言环境,您可以忽略此部分

回答by Fan Jin

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

这就是我想出的。在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);
}