diff options
author | Narayan Kamath <narayan@google.com> | 2013-11-20 17:50:40 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-11-20 17:50:41 +0000 |
commit | 270ee7a8ac9a4289070cb4c48dcd440637b79f28 (patch) | |
tree | 2f2fa16bd3e795318ffa282adb2ab719c57454f2 | |
parent | bd03ef0737fcf43d654840987ea2f3f1b5dadffa (diff) | |
parent | 4eb6dd5362871b00084b2fe0cfb4f4b87f052d4c (diff) | |
download | libcore-270ee7a8ac9a4289070cb4c48dcd440637b79f28.zip libcore-270ee7a8ac9a4289070cb4c48dcd440637b79f28.tar.gz libcore-270ee7a8ac9a4289070cb4c48dcd440637b79f28.tar.bz2 |
Merge "Document some surprising GregorianCalendar behavior."
-rw-r--r-- | luni/src/main/java/java/util/GregorianCalendar.java | 12 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/util/GregorianCalendarTest.java | 56 |
2 files changed, 66 insertions, 2 deletions
diff --git a/luni/src/main/java/java/util/GregorianCalendar.java b/luni/src/main/java/java/util/GregorianCalendar.java index 9ff9ccc..df6d772 100644 --- a/luni/src/main/java/java/util/GregorianCalendar.java +++ b/luni/src/main/java/java/util/GregorianCalendar.java @@ -670,8 +670,16 @@ public class GregorianCalendar extends Calendar { if (useMonth && (lastDateFieldSet == DAY_OF_WEEK || lastDateFieldSet == WEEK_OF_YEAR)) { if (isSet[WEEK_OF_YEAR] && isSet[DAY_OF_WEEK]) { - useMonth = lastDateFieldSet != WEEK_OF_YEAR && weekMonthSet - && isSet[DAY_OF_WEEK]; + if (lastDateFieldSet == WEEK_OF_YEAR) { + useMonth = false; + } else if (lastDateFieldSet == DAY_OF_WEEK) { + // DAY_OF_WEEK belongs to both the Month + Week + Day and the + // WeekOfYear + Day combinations. We're supposed to use the most + // recent combination, as specified by the single set field. We can't + // know for sure in this case, so we always prefer the week-month-day + // combination if week-month is already set. + useMonth = weekMonthSet; + } } else if (isSet[DAY_OF_YEAR]) { useMonth = isSet[DATE] && isSet[MONTH]; } diff --git a/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java b/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java new file mode 100644 index 0000000..b2c50b2 --- /dev/null +++ b/luni/src/test/java/libcore/java/util/GregorianCalendarTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package libcore.java.util; + + +import junit.framework.TestCase; +import java.util.Calendar; +import java.util.GregorianCalendar; + +public class GregorianCalendarTest extends TestCase { + + // https://code.google.com/p/android/issues/detail?id=61993 + public void test_computeFields_dayOfWeekAndWeekOfYearSet() { + Calendar greg = GregorianCalendar.getInstance(); + + // Setting WEEK_OF_YEAR and DAY_OF_WEEK with an intervening + // call to computeFields will work. + greg.set(Calendar.WEEK_OF_YEAR, 1); + assertEquals(1, greg.get(Calendar.WEEK_OF_YEAR)); + greg.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + assertEquals(1, greg.get(Calendar.WEEK_OF_YEAR)); + + // Setting WEEK_OF_YEAR after DAY_OF_WEEK with no intervening + // call to computeFields will work. + greg = GregorianCalendar.getInstance(); + greg.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + greg.set(Calendar.WEEK_OF_YEAR, 1); + assertEquals(1, greg.get(Calendar.WEEK_OF_YEAR)); + assertEquals(Calendar.MONDAY, greg.get(Calendar.DAY_OF_WEEK)); + + // Setting DAY_OF_WEEK *after* WEEK_OF_YEAR with no intervening computeFields + // will make WEEK_OF_YEAR have no effect. This is a limitation of the API. + // Combinations are chosen based *only* on the value of the last field set, + // which in this case is DAY_OF_WEEK. + greg = GregorianCalendar.getInstance(); + int weekOfYear = greg.get(Calendar.WEEK_OF_YEAR); + greg.set(Calendar.WEEK_OF_YEAR, 1); + greg.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + // Unchanged WEEK_OF_YEAR. + assertEquals(weekOfYear, greg.get(Calendar.WEEK_OF_YEAR)); + } +} |