001 /* 002 * Copyright 2001-2005 Stephen Colebourne 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.joda.time.chrono; 017 018 import java.util.Locale; 019 import java.util.TimeZone; 020 021 import junit.framework.TestCase; 022 import junit.framework.TestSuite; 023 024 import org.joda.time.DateMidnight; 025 import org.joda.time.DateTime; 026 import org.joda.time.DateTimeConstants; 027 import org.joda.time.DateTimeUtils; 028 import org.joda.time.DateTimeZone; 029 import org.joda.time.DurationField; 030 import org.joda.time.DurationFieldType; 031 import org.joda.time.IllegalFieldValueException; 032 import org.joda.time.Instant; 033 import org.joda.time.Period; 034 import org.joda.time.TimeOfDay; 035 import org.joda.time.YearMonthDay; 036 037 /** 038 * This class is a Junit unit test for GJChronology. 039 * 040 * @author Stephen Colebourne 041 */ 042 public class TestGJChronology extends TestCase { 043 044 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 045 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 046 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 047 048 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 049 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 050 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 051 366 + 365; 052 // 2002-06-09 053 private long TEST_TIME_NOW = 054 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 055 056 private DateTimeZone originalDateTimeZone = null; 057 private TimeZone originalTimeZone = null; 058 private Locale originalLocale = null; 059 060 public static void main(String[] args) { 061 junit.textui.TestRunner.run(suite()); 062 } 063 064 public static TestSuite suite() { 065 return new TestSuite(TestGJChronology.class); 066 } 067 068 public TestGJChronology(String name) { 069 super(name); 070 } 071 072 protected void setUp() throws Exception { 073 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 074 originalDateTimeZone = DateTimeZone.getDefault(); 075 originalTimeZone = TimeZone.getDefault(); 076 originalLocale = Locale.getDefault(); 077 DateTimeZone.setDefault(LONDON); 078 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 079 Locale.setDefault(Locale.UK); 080 } 081 082 protected void tearDown() throws Exception { 083 DateTimeUtils.setCurrentMillisSystem(); 084 DateTimeZone.setDefault(originalDateTimeZone); 085 TimeZone.setDefault(originalTimeZone); 086 Locale.setDefault(originalLocale); 087 originalDateTimeZone = null; 088 originalTimeZone = null; 089 originalLocale = null; 090 } 091 092 //----------------------------------------------------------------------- 093 public void testFactoryUTC() { 094 assertEquals(DateTimeZone.UTC, GJChronology.getInstanceUTC().getZone()); 095 assertSame(GJChronology.class, GJChronology.getInstanceUTC().getClass()); 096 } 097 098 public void testFactory() { 099 assertEquals(LONDON, GJChronology.getInstance().getZone()); 100 assertSame(GJChronology.class, GJChronology.getInstance().getClass()); 101 } 102 103 public void testFactory_Zone() { 104 assertEquals(TOKYO, GJChronology.getInstance(TOKYO).getZone()); 105 assertEquals(PARIS, GJChronology.getInstance(PARIS).getZone()); 106 assertEquals(LONDON, GJChronology.getInstance(null).getZone()); 107 assertSame(GJChronology.class, GJChronology.getInstance(TOKYO).getClass()); 108 } 109 110 public void testFactory_Zone_long_int() { 111 GJChronology chrono = GJChronology.getInstance(TOKYO, 0L, 2); 112 assertEquals(TOKYO, chrono.getZone()); 113 assertEquals(new Instant(0L), chrono.getGregorianCutover()); 114 assertEquals(2, chrono.getMinimumDaysInFirstWeek()); 115 assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, 0L, 2).getClass()); 116 117 try { 118 GJChronology.getInstance(TOKYO, 0L, 0); 119 fail(); 120 } catch (IllegalArgumentException ex) {} 121 try { 122 GJChronology.getInstance(TOKYO, 0L, 8); 123 fail(); 124 } catch (IllegalArgumentException ex) {} 125 } 126 127 public void testFactory_Zone_RI() { 128 GJChronology chrono = GJChronology.getInstance(TOKYO, new Instant(0L)); 129 assertEquals(TOKYO, chrono.getZone()); 130 assertEquals(new Instant(0L), chrono.getGregorianCutover()); 131 assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, new Instant(0L)).getClass()); 132 133 DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0, DateTimeZone.UTC); 134 chrono = GJChronology.getInstance(TOKYO, null); 135 assertEquals(TOKYO, chrono.getZone()); 136 assertEquals(cutover.toInstant(), chrono.getGregorianCutover()); 137 } 138 139 public void testFactory_Zone_RI_int() { 140 GJChronology chrono = GJChronology.getInstance(TOKYO, new Instant(0L), 2); 141 assertEquals(TOKYO, chrono.getZone()); 142 assertEquals(new Instant(0L), chrono.getGregorianCutover()); 143 assertEquals(2, chrono.getMinimumDaysInFirstWeek()); 144 assertSame(GJChronology.class, GJChronology.getInstance(TOKYO, new Instant(0L), 2).getClass()); 145 146 DateTime cutover = new DateTime(1582, 10, 15, 0, 0, 0, 0, DateTimeZone.UTC); 147 chrono = GJChronology.getInstance(TOKYO, null, 2); 148 assertEquals(TOKYO, chrono.getZone()); 149 assertEquals(cutover.toInstant(), chrono.getGregorianCutover()); 150 assertEquals(2, chrono.getMinimumDaysInFirstWeek()); 151 152 try { 153 GJChronology.getInstance(TOKYO, new Instant(0L), 0); 154 fail(); 155 } catch (IllegalArgumentException ex) {} 156 try { 157 GJChronology.getInstance(TOKYO, new Instant(0L), 8); 158 fail(); 159 } catch (IllegalArgumentException ex) {} 160 } 161 162 //----------------------------------------------------------------------- 163 public void testEquality() { 164 assertSame(GJChronology.getInstance(TOKYO), GJChronology.getInstance(TOKYO)); 165 assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(LONDON)); 166 assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance(PARIS)); 167 assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstanceUTC()); 168 assertSame(GJChronology.getInstance(), GJChronology.getInstance(LONDON)); 169 } 170 171 public void testWithUTC() { 172 assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance(LONDON).withUTC()); 173 assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance(TOKYO).withUTC()); 174 assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstanceUTC().withUTC()); 175 assertSame(GJChronology.getInstanceUTC(), GJChronology.getInstance().withUTC()); 176 } 177 178 public void testWithZone() { 179 assertSame(GJChronology.getInstance(TOKYO), GJChronology.getInstance(TOKYO).withZone(TOKYO)); 180 assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(TOKYO).withZone(LONDON)); 181 assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance(TOKYO).withZone(PARIS)); 182 assertSame(GJChronology.getInstance(LONDON), GJChronology.getInstance(TOKYO).withZone(null)); 183 assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstance().withZone(PARIS)); 184 assertSame(GJChronology.getInstance(PARIS), GJChronology.getInstanceUTC().withZone(PARIS)); 185 } 186 187 public void testToString() { 188 assertEquals("GJChronology[Europe/London]", GJChronology.getInstance(LONDON).toString()); 189 assertEquals("GJChronology[Asia/Tokyo]", GJChronology.getInstance(TOKYO).toString()); 190 assertEquals("GJChronology[Europe/London]", GJChronology.getInstance().toString()); 191 assertEquals("GJChronology[UTC]", GJChronology.getInstanceUTC().toString()); 192 assertEquals("GJChronology[UTC,cutover=1970-01-01]", GJChronology.getInstance(DateTimeZone.UTC, 0L, 4).toString()); 193 assertEquals("GJChronology[UTC,cutover=1970-01-01T00:00:00.001Z,mdfw=2]", GJChronology.getInstance(DateTimeZone.UTC, 1L, 2).toString()); 194 } 195 196 //----------------------------------------------------------------------- 197 public void testDurationFields() { 198 assertEquals("eras", GJChronology.getInstance().eras().getName()); 199 assertEquals("centuries", GJChronology.getInstance().centuries().getName()); 200 assertEquals("years", GJChronology.getInstance().years().getName()); 201 assertEquals("weekyears", GJChronology.getInstance().weekyears().getName()); 202 assertEquals("months", GJChronology.getInstance().months().getName()); 203 assertEquals("weeks", GJChronology.getInstance().weeks().getName()); 204 assertEquals("halfdays", GJChronology.getInstance().halfdays().getName()); 205 assertEquals("days", GJChronology.getInstance().days().getName()); 206 assertEquals("hours", GJChronology.getInstance().hours().getName()); 207 assertEquals("minutes", GJChronology.getInstance().minutes().getName()); 208 assertEquals("seconds", GJChronology.getInstance().seconds().getName()); 209 assertEquals("millis", GJChronology.getInstance().millis().getName()); 210 211 assertEquals(false, GJChronology.getInstance().eras().isSupported()); 212 assertEquals(true, GJChronology.getInstance().centuries().isSupported()); 213 assertEquals(true, GJChronology.getInstance().years().isSupported()); 214 assertEquals(true, GJChronology.getInstance().weekyears().isSupported()); 215 assertEquals(true, GJChronology.getInstance().months().isSupported()); 216 assertEquals(true, GJChronology.getInstance().weeks().isSupported()); 217 assertEquals(true, GJChronology.getInstance().days().isSupported()); 218 assertEquals(true, GJChronology.getInstance().halfdays().isSupported()); 219 assertEquals(true, GJChronology.getInstance().hours().isSupported()); 220 assertEquals(true, GJChronology.getInstance().minutes().isSupported()); 221 assertEquals(true, GJChronology.getInstance().seconds().isSupported()); 222 assertEquals(true, GJChronology.getInstance().millis().isSupported()); 223 224 assertEquals(false, GJChronology.getInstance().centuries().isPrecise()); 225 assertEquals(false, GJChronology.getInstance().years().isPrecise()); 226 assertEquals(false, GJChronology.getInstance().weekyears().isPrecise()); 227 assertEquals(false, GJChronology.getInstance().months().isPrecise()); 228 assertEquals(false, GJChronology.getInstance().weeks().isPrecise()); 229 assertEquals(false, GJChronology.getInstance().days().isPrecise()); 230 assertEquals(false, GJChronology.getInstance().halfdays().isPrecise()); 231 assertEquals(true, GJChronology.getInstance().hours().isPrecise()); 232 assertEquals(true, GJChronology.getInstance().minutes().isPrecise()); 233 assertEquals(true, GJChronology.getInstance().seconds().isPrecise()); 234 assertEquals(true, GJChronology.getInstance().millis().isPrecise()); 235 236 assertEquals(false, GJChronology.getInstanceUTC().centuries().isPrecise()); 237 assertEquals(false, GJChronology.getInstanceUTC().years().isPrecise()); 238 assertEquals(false, GJChronology.getInstanceUTC().weekyears().isPrecise()); 239 assertEquals(false, GJChronology.getInstanceUTC().months().isPrecise()); 240 assertEquals(true, GJChronology.getInstanceUTC().weeks().isPrecise()); 241 assertEquals(true, GJChronology.getInstanceUTC().days().isPrecise()); 242 assertEquals(true, GJChronology.getInstanceUTC().halfdays().isPrecise()); 243 assertEquals(true, GJChronology.getInstanceUTC().hours().isPrecise()); 244 assertEquals(true, GJChronology.getInstanceUTC().minutes().isPrecise()); 245 assertEquals(true, GJChronology.getInstanceUTC().seconds().isPrecise()); 246 assertEquals(true, GJChronology.getInstanceUTC().millis().isPrecise()); 247 248 DateTimeZone gmt = DateTimeZone.forID("Etc/GMT"); 249 assertEquals(false, GJChronology.getInstance(gmt).centuries().isPrecise()); 250 assertEquals(false, GJChronology.getInstance(gmt).years().isPrecise()); 251 assertEquals(false, GJChronology.getInstance(gmt).weekyears().isPrecise()); 252 assertEquals(false, GJChronology.getInstance(gmt).months().isPrecise()); 253 assertEquals(true, GJChronology.getInstance(gmt).weeks().isPrecise()); 254 assertEquals(true, GJChronology.getInstance(gmt).days().isPrecise()); 255 assertEquals(true, GJChronology.getInstance(gmt).halfdays().isPrecise()); 256 assertEquals(true, GJChronology.getInstance(gmt).hours().isPrecise()); 257 assertEquals(true, GJChronology.getInstance(gmt).minutes().isPrecise()); 258 assertEquals(true, GJChronology.getInstance(gmt).seconds().isPrecise()); 259 assertEquals(true, GJChronology.getInstance(gmt).millis().isPrecise()); 260 } 261 262 public void testDateFields() { 263 assertEquals("era", GJChronology.getInstance().era().getName()); 264 assertEquals("centuryOfEra", GJChronology.getInstance().centuryOfEra().getName()); 265 assertEquals("yearOfCentury", GJChronology.getInstance().yearOfCentury().getName()); 266 assertEquals("yearOfEra", GJChronology.getInstance().yearOfEra().getName()); 267 assertEquals("year", GJChronology.getInstance().year().getName()); 268 assertEquals("monthOfYear", GJChronology.getInstance().monthOfYear().getName()); 269 assertEquals("weekyearOfCentury", GJChronology.getInstance().weekyearOfCentury().getName()); 270 assertEquals("weekyear", GJChronology.getInstance().weekyear().getName()); 271 assertEquals("weekOfWeekyear", GJChronology.getInstance().weekOfWeekyear().getName()); 272 assertEquals("dayOfYear", GJChronology.getInstance().dayOfYear().getName()); 273 assertEquals("dayOfMonth", GJChronology.getInstance().dayOfMonth().getName()); 274 assertEquals("dayOfWeek", GJChronology.getInstance().dayOfWeek().getName()); 275 276 assertEquals(true, GJChronology.getInstance().era().isSupported()); 277 assertEquals(true, GJChronology.getInstance().centuryOfEra().isSupported()); 278 assertEquals(true, GJChronology.getInstance().yearOfCentury().isSupported()); 279 assertEquals(true, GJChronology.getInstance().yearOfEra().isSupported()); 280 assertEquals(true, GJChronology.getInstance().year().isSupported()); 281 assertEquals(true, GJChronology.getInstance().monthOfYear().isSupported()); 282 assertEquals(true, GJChronology.getInstance().weekyearOfCentury().isSupported()); 283 assertEquals(true, GJChronology.getInstance().weekyear().isSupported()); 284 assertEquals(true, GJChronology.getInstance().weekOfWeekyear().isSupported()); 285 assertEquals(true, GJChronology.getInstance().dayOfYear().isSupported()); 286 assertEquals(true, GJChronology.getInstance().dayOfMonth().isSupported()); 287 assertEquals(true, GJChronology.getInstance().dayOfWeek().isSupported()); 288 } 289 290 public void testTimeFields() { 291 assertEquals("halfdayOfDay", GJChronology.getInstance().halfdayOfDay().getName()); 292 assertEquals("clockhourOfHalfday", GJChronology.getInstance().clockhourOfHalfday().getName()); 293 assertEquals("hourOfHalfday", GJChronology.getInstance().hourOfHalfday().getName()); 294 assertEquals("clockhourOfDay", GJChronology.getInstance().clockhourOfDay().getName()); 295 assertEquals("hourOfDay", GJChronology.getInstance().hourOfDay().getName()); 296 assertEquals("minuteOfDay", GJChronology.getInstance().minuteOfDay().getName()); 297 assertEquals("minuteOfHour", GJChronology.getInstance().minuteOfHour().getName()); 298 assertEquals("secondOfDay", GJChronology.getInstance().secondOfDay().getName()); 299 assertEquals("secondOfMinute", GJChronology.getInstance().secondOfMinute().getName()); 300 assertEquals("millisOfDay", GJChronology.getInstance().millisOfDay().getName()); 301 assertEquals("millisOfSecond", GJChronology.getInstance().millisOfSecond().getName()); 302 303 assertEquals(true, GJChronology.getInstance().halfdayOfDay().isSupported()); 304 assertEquals(true, GJChronology.getInstance().clockhourOfHalfday().isSupported()); 305 assertEquals(true, GJChronology.getInstance().hourOfHalfday().isSupported()); 306 assertEquals(true, GJChronology.getInstance().clockhourOfDay().isSupported()); 307 assertEquals(true, GJChronology.getInstance().hourOfDay().isSupported()); 308 assertEquals(true, GJChronology.getInstance().minuteOfDay().isSupported()); 309 assertEquals(true, GJChronology.getInstance().minuteOfHour().isSupported()); 310 assertEquals(true, GJChronology.getInstance().secondOfDay().isSupported()); 311 assertEquals(true, GJChronology.getInstance().secondOfMinute().isSupported()); 312 assertEquals(true, GJChronology.getInstance().millisOfDay().isSupported()); 313 assertEquals(true, GJChronology.getInstance().millisOfSecond().isSupported()); 314 } 315 316 public void testIllegalDates() { 317 try { 318 new DateTime(1582, 10, 5, 0, 0, 0, 0, GJChronology.getInstance(DateTimeZone.UTC)); 319 fail("Constructed illegal date"); 320 } catch (IllegalArgumentException e) { /* good */ } 321 322 try { 323 new DateTime(1582, 10, 14, 0, 0, 0, 0, GJChronology.getInstance(DateTimeZone.UTC)); 324 fail("Constructed illegal date"); 325 } catch (IllegalArgumentException e) { /* good */ } 326 } 327 328 public void testParseEquivalence() { 329 testParse("1581-01-01T01:23:45.678", 1581, 1, 1, 1, 23, 45, 678); 330 testParse("1581-06-30", 1581, 6, 30, 0, 0, 0, 0); 331 testParse("1582-01-01T01:23:45.678", 1582, 1, 1, 1, 23, 45, 678); 332 testParse("1582-06-30T01:23:45.678", 1582, 6, 30, 1, 23, 45, 678); 333 testParse("1582-10-04", 1582, 10, 4, 0, 0, 0, 0); 334 testParse("1582-10-15", 1582, 10, 15, 0, 0, 0, 0); 335 testParse("1582-12-31", 1582, 12, 31, 0, 0, 0, 0); 336 testParse("1583-12-31", 1583, 12, 31, 0, 0, 0, 0); 337 } 338 339 private void testParse(String str, 340 int year, int month, int day, 341 int hour, int minute, int second, int millis) { 342 assertEquals(new DateTime(str, GJChronology.getInstance(DateTimeZone.UTC)), 343 new DateTime(year, month, day, hour, minute, second, millis, 344 GJChronology.getInstance(DateTimeZone.UTC))); 345 } 346 347 public void testCutoverAddYears() { 348 testAdd("1582-01-01", DurationFieldType.years(), 1, "1583-01-01"); 349 testAdd("1582-02-15", DurationFieldType.years(), 1, "1583-02-15"); 350 testAdd("1582-02-28", DurationFieldType.years(), 1, "1583-02-28"); 351 testAdd("1582-03-01", DurationFieldType.years(), 1, "1583-03-01"); 352 testAdd("1582-09-30", DurationFieldType.years(), 1, "1583-09-30"); 353 testAdd("1582-10-01", DurationFieldType.years(), 1, "1583-10-01"); 354 testAdd("1582-10-04", DurationFieldType.years(), 1, "1583-10-04"); 355 testAdd("1582-10-15", DurationFieldType.years(), 1, "1583-10-15"); 356 testAdd("1582-10-16", DurationFieldType.years(), 1, "1583-10-16"); 357 358 // Leap years... 359 testAdd("1580-01-01", DurationFieldType.years(), 4, "1584-01-01"); 360 testAdd("1580-02-29", DurationFieldType.years(), 4, "1584-02-29"); 361 testAdd("1580-10-01", DurationFieldType.years(), 4, "1584-10-01"); 362 testAdd("1580-10-10", DurationFieldType.years(), 4, "1584-10-10"); 363 testAdd("1580-10-15", DurationFieldType.years(), 4, "1584-10-15"); 364 testAdd("1580-12-31", DurationFieldType.years(), 4, "1584-12-31"); 365 } 366 367 public void testCutoverAddWeekyears() { 368 testAdd("1582-W01-1", DurationFieldType.weekyears(), 1, "1583-W01-1"); 369 testAdd("1582-W39-1", DurationFieldType.weekyears(), 1, "1583-W39-1"); 370 testAdd("1583-W45-1", DurationFieldType.weekyears(), 1, "1584-W45-1"); 371 372 // This test fails, but I'm not sure if its worth fixing. The date 373 // falls after the cutover, but in the cutover year. The add operation 374 // is performed completely within the gregorian calendar, with no 375 // crossing of the cutover. As a result, no special correction is 376 // applied. Since the full gregorian year of 1582 has a different week 377 // numbers than the full julian year of 1582, the week number is off by 378 // one after the addition. 379 // 380 //testAdd("1582-W42-1", DurationFieldType.weekyears(), 1, "1583-W42-1"); 381 382 // Leap years... 383 testAdd("1580-W01-1", DurationFieldType.weekyears(), 4, "1584-W01-1"); 384 testAdd("1580-W30-7", DurationFieldType.weekyears(), 4, "1584-W30-7"); 385 testAdd("1580-W50-7", DurationFieldType.weekyears(), 4, "1584-W50-7"); 386 } 387 388 public void testCutoverAddMonths() { 389 testAdd("1582-01-01", DurationFieldType.months(), 1, "1582-02-01"); 390 testAdd("1582-01-01", DurationFieldType.months(), 6, "1582-07-01"); 391 testAdd("1582-01-01", DurationFieldType.months(), 12, "1583-01-01"); 392 testAdd("1582-11-15", DurationFieldType.months(), 1, "1582-12-15"); 393 394 testAdd("1582-09-04", DurationFieldType.months(), 2, "1582-11-04"); 395 testAdd("1582-09-05", DurationFieldType.months(), 2, "1582-11-05"); 396 testAdd("1582-09-10", DurationFieldType.months(), 2, "1582-11-10"); 397 testAdd("1582-09-15", DurationFieldType.months(), 2, "1582-11-15"); 398 399 400 // Leap years... 401 testAdd("1580-01-01", DurationFieldType.months(), 48, "1584-01-01"); 402 testAdd("1580-02-29", DurationFieldType.months(), 48, "1584-02-29"); 403 testAdd("1580-10-01", DurationFieldType.months(), 48, "1584-10-01"); 404 testAdd("1580-10-10", DurationFieldType.months(), 48, "1584-10-10"); 405 testAdd("1580-10-15", DurationFieldType.months(), 48, "1584-10-15"); 406 testAdd("1580-12-31", DurationFieldType.months(), 48, "1584-12-31"); 407 } 408 409 public void testCutoverAddWeeks() { 410 testAdd("1582-01-01", DurationFieldType.weeks(), 1, "1582-01-08"); 411 testAdd("1583-01-01", DurationFieldType.weeks(), 1, "1583-01-08"); 412 413 // Weeks are precise, and so cutover is not ignored. 414 testAdd("1582-10-01", DurationFieldType.weeks(), 2, "1582-10-25"); 415 testAdd("1582-W01-1", DurationFieldType.weeks(), 51, "1583-W01-1"); 416 } 417 418 public void testCutoverAddDays() { 419 testAdd("1582-10-03", DurationFieldType.days(), 1, "1582-10-04"); 420 testAdd("1582-10-04", DurationFieldType.days(), 1, "1582-10-15"); 421 testAdd("1582-10-15", DurationFieldType.days(), 1, "1582-10-16"); 422 423 testAdd("1582-09-30", DurationFieldType.days(), 10, "1582-10-20"); 424 testAdd("1582-10-04", DurationFieldType.days(), 10, "1582-10-24"); 425 testAdd("1582-10-15", DurationFieldType.days(), 10, "1582-10-25"); 426 } 427 428 public void testYearEndAddDays() { 429 testAdd("1582-11-05", DurationFieldType.days(), 28, "1582-12-03"); 430 testAdd("1582-12-05", DurationFieldType.days(), 28, "1583-01-02"); 431 432 testAdd("2005-11-05", DurationFieldType.days(), 28, "2005-12-03"); 433 testAdd("2005-12-05", DurationFieldType.days(), 28, "2006-01-02"); 434 } 435 436 public void testSubtractDays() { 437 // This is a test for a bug in version 1.0. The dayOfMonth range 438 // duration field did not match the monthOfYear duration field. This 439 // caused an exception to be thrown when subtracting days. 440 DateTime dt = new DateTime 441 (1112306400000L, GJChronology.getInstance(DateTimeZone.forID("Europe/Berlin"))); 442 YearMonthDay ymd = dt.toYearMonthDay(); 443 while (ymd.toDateTimeAtMidnight().getDayOfWeek() != DateTimeConstants.MONDAY) { 444 ymd = ymd.minus(Period.days(1)); 445 } 446 } 447 448 private void testAdd(String start, DurationFieldType type, int amt, String end) { 449 DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC)); 450 DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC)); 451 assertEquals(dtEnd, dtStart.withFieldAdded(type, amt)); 452 assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt)); 453 454 DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC)); 455 int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis()); 456 assertEquals(amt, diff); 457 458 if (type == DurationFieldType.years() || 459 type == DurationFieldType.months() || 460 type == DurationFieldType.days()) { 461 YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC)); 462 YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC)); 463 assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt)); 464 assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt)); 465 } 466 } 467 468 public void testTimeOfDayAdd() { 469 TimeOfDay start = new TimeOfDay(12, 30, GJChronology.getInstance()); 470 TimeOfDay end = new TimeOfDay(10, 30, GJChronology.getInstance()); 471 assertEquals(end, start.plusHours(22)); 472 assertEquals(start, end.minusHours(22)); 473 assertEquals(end, start.plusMinutes(22 * 60)); 474 assertEquals(start, end.minusMinutes(22 * 60)); 475 } 476 477 public void testMaximumValue() { 478 DateMidnight dt = new DateMidnight(1570, 1, 1, GJChronology.getInstance()); 479 while (dt.getYear() < 1590) { 480 dt = dt.plusDays(1); 481 YearMonthDay ymd = dt.toYearMonthDay(); 482 assertEquals(dt.year().getMaximumValue(), ymd.year().getMaximumValue()); 483 assertEquals(dt.monthOfYear().getMaximumValue(), ymd.monthOfYear().getMaximumValue()); 484 assertEquals(dt.dayOfMonth().getMaximumValue(), ymd.dayOfMonth().getMaximumValue()); 485 } 486 } 487 488 public void testPartialGetAsText() { 489 GJChronology chrono = GJChronology.getInstance(TOKYO); 490 assertEquals("January", new YearMonthDay("2005-01-01", chrono).monthOfYear().getAsText()); 491 assertEquals("Jan", new YearMonthDay("2005-01-01", chrono).monthOfYear().getAsShortText()); 492 } 493 494 public void testLeapYearRulesConstruction() { 495 // 1500 not leap in Gregorian, but is leap in Julian 496 DateMidnight dt = new DateMidnight(1500, 2, 29, GJChronology.getInstanceUTC()); 497 assertEquals(dt.getYear(), 1500); 498 assertEquals(dt.getMonthOfYear(), 2); 499 assertEquals(dt.getDayOfMonth(), 29); 500 } 501 502 public void testLeapYearRulesConstructionInvalid() { 503 // 1500 not leap in Gregorian, but is leap in Julian 504 try { 505 new DateMidnight(1500, 2, 30, GJChronology.getInstanceUTC()); 506 fail(); 507 } catch (IllegalFieldValueException ex) { 508 // good 509 } 510 } 511 512 }