Start of week for locale using Joda-Time

后端 未结 7 2076
旧时难觅i
旧时难觅i 2020-11-28 10:20

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

相关标签:
7条回答
  • 2020-11-28 10:43

    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.

    0 讨论(0)
  • 2020-11-28 10:45

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

    0 讨论(0)
  • 2020-11-28 10:48

    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)
    }
    
    0 讨论(0)
  • 2020-11-28 10:49

    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.

    0 讨论(0)
  • 2020-11-28 10:49

    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);
    }
    
    0 讨论(0)
  • 2020-11-28 11:01

    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();
    
    0 讨论(0)
提交回复
热议问题