Source Code Cross Referenced for DateUtilsTest.java in  » Library » Apache-common-lang » org » apache » commons » lang » time » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang.time 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:        package org.apache.commons.lang.time;
0018:
0019:        import java.lang.reflect.Constructor;
0020:        import java.lang.reflect.Modifier;
0021:        import java.text.DateFormat;
0022:        import java.text.ParseException;
0023:        import java.text.SimpleDateFormat;
0024:        import java.util.Calendar;
0025:        import java.util.Date;
0026:        import java.util.GregorianCalendar;
0027:        import java.util.Iterator;
0028:        import java.util.Locale;
0029:        import java.util.NoSuchElementException;
0030:        import java.util.TimeZone;
0031:
0032:        import junit.framework.AssertionFailedError;
0033:        import junit.framework.Test;
0034:        import junit.framework.TestCase;
0035:        import junit.framework.TestSuite;
0036:        import junit.textui.TestRunner;
0037:
0038:        import org.apache.commons.lang.SystemUtils;
0039:
0040:        /**
0041:         * Unit tests {@link org.apache.commons.lang.time.DateUtils}.
0042:         *
0043:         * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
0044:         * @author <a href="mailto:steve@mungoknotwise.com">Steven Caswell</a>
0045:         */
0046:        public class DateUtilsTest extends TestCase {
0047:
0048:            private static final long MILLIS_TEST;
0049:            static {
0050:                GregorianCalendar cal = new GregorianCalendar(2000, 6, 5, 4, 3,
0051:                        2);
0052:                cal.set(Calendar.MILLISECOND, 1);
0053:                MILLIS_TEST = cal.getTime().getTime();
0054:            }
0055:
0056:            DateFormat dateParser = null;
0057:            DateFormat dateTimeParser = null;
0058:            DateFormat timeZoneDateParser = null;
0059:            Date dateAmPm1 = null;
0060:            Date dateAmPm2 = null;
0061:            Date dateAmPm3 = null;
0062:            Date dateAmPm4 = null;
0063:            Date date0 = null;
0064:            Date date1 = null;
0065:            Date date2 = null;
0066:            Date date3 = null;
0067:            Date date4 = null;
0068:            Date date5 = null;
0069:            Date date6 = null;
0070:            Date date7 = null;
0071:            Date date8 = null;
0072:            Calendar calAmPm1 = null;
0073:            Calendar calAmPm2 = null;
0074:            Calendar calAmPm3 = null;
0075:            Calendar calAmPm4 = null;
0076:            Calendar cal1 = null;
0077:            Calendar cal2 = null;
0078:            Calendar cal3 = null;
0079:            Calendar cal4 = null;
0080:            Calendar cal5 = null;
0081:            Calendar cal6 = null;
0082:            Calendar cal7 = null;
0083:            Calendar cal8 = null;
0084:            TimeZone zone = null;
0085:            TimeZone defaultZone = null;
0086:
0087:            public DateUtilsTest(String name) {
0088:                super (name);
0089:            }
0090:
0091:            public static void main(String[] args) {
0092:                TestRunner.run(suite());
0093:            }
0094:
0095:            public static Test suite() {
0096:                TestSuite suite = new TestSuite(DateUtilsTest.class);
0097:                suite.setName("DateUtils Tests");
0098:                return suite;
0099:            }
0100:
0101:            protected void setUp() throws Exception {
0102:                super .setUp();
0103:
0104:                dateParser = new SimpleDateFormat("MMM dd, yyyy",
0105:                        Locale.ENGLISH);
0106:                dateTimeParser = new SimpleDateFormat(
0107:                        "MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH);
0108:
0109:                dateAmPm1 = dateTimeParser
0110:                        .parse("February 3, 2002 01:10:00.000");
0111:                dateAmPm2 = dateTimeParser
0112:                        .parse("February 3, 2002 11:10:00.000");
0113:                dateAmPm3 = dateTimeParser
0114:                        .parse("February 3, 2002 13:10:00.000");
0115:                dateAmPm4 = dateTimeParser
0116:                        .parse("February 3, 2002 19:10:00.000");
0117:                date0 = dateTimeParser.parse("February 3, 2002 12:34:56.789");
0118:                date1 = dateTimeParser.parse("February 12, 2002 12:34:56.789");
0119:                date2 = dateTimeParser.parse("November 18, 2001 1:23:11.321");
0120:                defaultZone = TimeZone.getDefault();
0121:                zone = TimeZone.getTimeZone("MET");
0122:                TimeZone.setDefault(zone);
0123:                dateTimeParser.setTimeZone(zone);
0124:                date3 = dateTimeParser.parse("March 30, 2003 05:30:45.000");
0125:                date4 = dateTimeParser.parse("March 30, 2003 01:10:00.000");
0126:                date5 = dateTimeParser.parse("March 30, 2003 01:40:00.000");
0127:                date6 = dateTimeParser.parse("March 30, 2003 02:10:00.000");
0128:                date7 = dateTimeParser.parse("March 30, 2003 02:40:00.000");
0129:                date8 = dateTimeParser.parse("October 26, 2003 05:30:45.000");
0130:                dateTimeParser.setTimeZone(defaultZone);
0131:                TimeZone.setDefault(defaultZone);
0132:                calAmPm1 = Calendar.getInstance();
0133:                calAmPm1.setTime(dateAmPm1);
0134:                calAmPm2 = Calendar.getInstance();
0135:                calAmPm2.setTime(dateAmPm2);
0136:                calAmPm3 = Calendar.getInstance();
0137:                calAmPm3.setTime(dateAmPm3);
0138:                calAmPm4 = Calendar.getInstance();
0139:                calAmPm4.setTime(dateAmPm4);
0140:                cal1 = Calendar.getInstance();
0141:                cal1.setTime(date1);
0142:                cal2 = Calendar.getInstance();
0143:                cal2.setTime(date2);
0144:                TimeZone.setDefault(zone);
0145:                cal3 = Calendar.getInstance();
0146:                cal3.setTime(date3);
0147:                cal4 = Calendar.getInstance();
0148:                cal4.setTime(date4);
0149:                cal5 = Calendar.getInstance();
0150:                cal5.setTime(date5);
0151:                cal6 = Calendar.getInstance();
0152:                cal6.setTime(date6);
0153:                cal7 = Calendar.getInstance();
0154:                cal7.setTime(date7);
0155:                cal8 = Calendar.getInstance();
0156:                cal8.setTime(date8);
0157:                TimeZone.setDefault(defaultZone);
0158:            }
0159:
0160:            protected void tearDown() throws Exception {
0161:                super .tearDown();
0162:            }
0163:
0164:            //-----------------------------------------------------------------------
0165:            public void testConstructor() {
0166:                assertNotNull(new DateUtils());
0167:                Constructor[] cons = DateUtils.class.getDeclaredConstructors();
0168:                assertEquals(1, cons.length);
0169:                assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
0170:                assertEquals(true, Modifier.isPublic(DateUtils.class
0171:                        .getModifiers()));
0172:                assertEquals(false, Modifier.isFinal(DateUtils.class
0173:                        .getModifiers()));
0174:            }
0175:
0176:            //-----------------------------------------------------------------------
0177:            public void testIsSameDay_Date() {
0178:                Date date1 = new GregorianCalendar(2004, 6, 9, 13, 45)
0179:                        .getTime();
0180:                Date date2 = new GregorianCalendar(2004, 6, 9, 13, 45)
0181:                        .getTime();
0182:                assertEquals(true, DateUtils.isSameDay(date1, date2));
0183:                date2 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
0184:                assertEquals(false, DateUtils.isSameDay(date1, date2));
0185:                date1 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
0186:                assertEquals(true, DateUtils.isSameDay(date1, date2));
0187:                date2 = new GregorianCalendar(2005, 6, 10, 13, 45).getTime();
0188:                assertEquals(false, DateUtils.isSameDay(date1, date2));
0189:                try {
0190:                    DateUtils.isSameDay((Date) null, (Date) null);
0191:                    fail();
0192:                } catch (IllegalArgumentException ex) {
0193:                }
0194:            }
0195:
0196:            //-----------------------------------------------------------------------
0197:            public void testIsSameDay_Cal() {
0198:                GregorianCalendar cal1 = new GregorianCalendar(2004, 6, 9, 13,
0199:                        45);
0200:                GregorianCalendar cal2 = new GregorianCalendar(2004, 6, 9, 13,
0201:                        45);
0202:                assertEquals(true, DateUtils.isSameDay(cal1, cal2));
0203:                cal2.add(Calendar.DAY_OF_YEAR, 1);
0204:                assertEquals(false, DateUtils.isSameDay(cal1, cal2));
0205:                cal1.add(Calendar.DAY_OF_YEAR, 1);
0206:                assertEquals(true, DateUtils.isSameDay(cal1, cal2));
0207:                cal2.add(Calendar.YEAR, 1);
0208:                assertEquals(false, DateUtils.isSameDay(cal1, cal2));
0209:                try {
0210:                    DateUtils.isSameDay((Calendar) null, (Calendar) null);
0211:                    fail();
0212:                } catch (IllegalArgumentException ex) {
0213:                }
0214:            }
0215:
0216:            //-----------------------------------------------------------------------
0217:            public void testIsSameInstant_Date() {
0218:                Date date1 = new GregorianCalendar(2004, 6, 9, 13, 45)
0219:                        .getTime();
0220:                Date date2 = new GregorianCalendar(2004, 6, 9, 13, 45)
0221:                        .getTime();
0222:                assertEquals(true, DateUtils.isSameInstant(date1, date2));
0223:                date2 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
0224:                assertEquals(false, DateUtils.isSameInstant(date1, date2));
0225:                date1 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
0226:                assertEquals(true, DateUtils.isSameInstant(date1, date2));
0227:                date2 = new GregorianCalendar(2005, 6, 10, 13, 45).getTime();
0228:                assertEquals(false, DateUtils.isSameInstant(date1, date2));
0229:                try {
0230:                    DateUtils.isSameInstant((Date) null, (Date) null);
0231:                    fail();
0232:                } catch (IllegalArgumentException ex) {
0233:                }
0234:            }
0235:
0236:            //-----------------------------------------------------------------------
0237:            public void testIsSameInstant_Cal() {
0238:                GregorianCalendar cal1 = new GregorianCalendar(TimeZone
0239:                        .getTimeZone("GMT+1"));
0240:                GregorianCalendar cal2 = new GregorianCalendar(TimeZone
0241:                        .getTimeZone("GMT-1"));
0242:                cal1.set(2004, 6, 9, 13, 45, 0);
0243:                cal1.set(Calendar.MILLISECOND, 0);
0244:                cal2.set(2004, 6, 9, 13, 45, 0);
0245:                cal2.set(Calendar.MILLISECOND, 0);
0246:                assertEquals(false, DateUtils.isSameInstant(cal1, cal2));
0247:
0248:                cal2.set(2004, 6, 9, 11, 45, 0);
0249:                assertEquals(true, DateUtils.isSameInstant(cal1, cal2));
0250:                try {
0251:                    DateUtils.isSameInstant((Calendar) null, (Calendar) null);
0252:                    fail();
0253:                } catch (IllegalArgumentException ex) {
0254:                }
0255:            }
0256:
0257:            //-----------------------------------------------------------------------
0258:            public void testIsSameLocalTime_Cal() {
0259:                GregorianCalendar cal1 = new GregorianCalendar(TimeZone
0260:                        .getTimeZone("GMT+1"));
0261:                GregorianCalendar cal2 = new GregorianCalendar(TimeZone
0262:                        .getTimeZone("GMT-1"));
0263:                cal1.set(2004, 6, 9, 13, 45, 0);
0264:                cal1.set(Calendar.MILLISECOND, 0);
0265:                cal2.set(2004, 6, 9, 13, 45, 0);
0266:                cal2.set(Calendar.MILLISECOND, 0);
0267:                assertEquals(true, DateUtils.isSameLocalTime(cal1, cal2));
0268:
0269:                cal2.set(2004, 6, 9, 11, 45, 0);
0270:                assertEquals(false, DateUtils.isSameLocalTime(cal1, cal2));
0271:                try {
0272:                    DateUtils.isSameLocalTime((Calendar) null, (Calendar) null);
0273:                    fail();
0274:                } catch (IllegalArgumentException ex) {
0275:                }
0276:            }
0277:
0278:            //-----------------------------------------------------------------------
0279:            public void testParseDate() throws Exception {
0280:                GregorianCalendar cal = new GregorianCalendar(1972, 11, 3);
0281:                String dateStr = "1972-12-03";
0282:                String[] parsers = new String[] { "yyyy'-'DDD",
0283:                        "yyyy'-'MM'-'dd", "yyyyMMdd" };
0284:                Date date = DateUtils.parseDate(dateStr, parsers);
0285:                assertEquals(cal.getTime(), date);
0286:
0287:                dateStr = "1972-338";
0288:                date = DateUtils.parseDate(dateStr, parsers);
0289:                assertEquals(cal.getTime(), date);
0290:
0291:                dateStr = "19721203";
0292:                date = DateUtils.parseDate(dateStr, parsers);
0293:                assertEquals(cal.getTime(), date);
0294:
0295:                try {
0296:                    DateUtils.parseDate("PURPLE", parsers);
0297:                    fail();
0298:                } catch (ParseException ex) {
0299:                }
0300:                try {
0301:                    DateUtils.parseDate("197212AB", parsers);
0302:                    fail();
0303:                } catch (ParseException ex) {
0304:                }
0305:                try {
0306:                    DateUtils.parseDate(null, parsers);
0307:                    fail();
0308:                } catch (IllegalArgumentException ex) {
0309:                }
0310:                try {
0311:                    DateUtils.parseDate(dateStr, null);
0312:                    fail();
0313:                } catch (IllegalArgumentException ex) {
0314:                }
0315:            }
0316:
0317:            //-----------------------------------------------------------------------
0318:            public void testAddYears() throws Exception {
0319:                Date base = new Date(MILLIS_TEST);
0320:                Date result = DateUtils.addYears(base, 0);
0321:                assertNotSame(base, result);
0322:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0323:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0324:
0325:                result = DateUtils.addYears(base, 1);
0326:                assertNotSame(base, result);
0327:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0328:                assertDate(result, 2001, 6, 5, 4, 3, 2, 1);
0329:
0330:                result = DateUtils.addYears(base, -1);
0331:                assertNotSame(base, result);
0332:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0333:                assertDate(result, 1999, 6, 5, 4, 3, 2, 1);
0334:            }
0335:
0336:            //-----------------------------------------------------------------------
0337:            public void testAddMonths() throws Exception {
0338:                Date base = new Date(MILLIS_TEST);
0339:                Date result = DateUtils.addMonths(base, 0);
0340:                assertNotSame(base, result);
0341:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0342:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0343:
0344:                result = DateUtils.addMonths(base, 1);
0345:                assertNotSame(base, result);
0346:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0347:                assertDate(result, 2000, 7, 5, 4, 3, 2, 1);
0348:
0349:                result = DateUtils.addMonths(base, -1);
0350:                assertNotSame(base, result);
0351:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0352:                assertDate(result, 2000, 5, 5, 4, 3, 2, 1);
0353:            }
0354:
0355:            //-----------------------------------------------------------------------
0356:            public void testAddWeeks() throws Exception {
0357:                Date base = new Date(MILLIS_TEST);
0358:                Date result = DateUtils.addWeeks(base, 0);
0359:                assertNotSame(base, result);
0360:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0361:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0362:
0363:                result = DateUtils.addWeeks(base, 1);
0364:                assertNotSame(base, result);
0365:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0366:                assertDate(result, 2000, 6, 12, 4, 3, 2, 1);
0367:
0368:                result = DateUtils.addWeeks(base, -1);
0369:                assertNotSame(base, result);
0370:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1); // july
0371:                assertDate(result, 2000, 5, 28, 4, 3, 2, 1); // june
0372:            }
0373:
0374:            //-----------------------------------------------------------------------
0375:            public void testAddDays() throws Exception {
0376:                Date base = new Date(MILLIS_TEST);
0377:                Date result = DateUtils.addDays(base, 0);
0378:                assertNotSame(base, result);
0379:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0380:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0381:
0382:                result = DateUtils.addDays(base, 1);
0383:                assertNotSame(base, result);
0384:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0385:                assertDate(result, 2000, 6, 6, 4, 3, 2, 1);
0386:
0387:                result = DateUtils.addDays(base, -1);
0388:                assertNotSame(base, result);
0389:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0390:                assertDate(result, 2000, 6, 4, 4, 3, 2, 1);
0391:            }
0392:
0393:            //-----------------------------------------------------------------------
0394:            public void testAddHours() throws Exception {
0395:                Date base = new Date(MILLIS_TEST);
0396:                Date result = DateUtils.addHours(base, 0);
0397:                assertNotSame(base, result);
0398:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0399:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0400:
0401:                result = DateUtils.addHours(base, 1);
0402:                assertNotSame(base, result);
0403:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0404:                assertDate(result, 2000, 6, 5, 5, 3, 2, 1);
0405:
0406:                result = DateUtils.addHours(base, -1);
0407:                assertNotSame(base, result);
0408:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0409:                assertDate(result, 2000, 6, 5, 3, 3, 2, 1);
0410:            }
0411:
0412:            //-----------------------------------------------------------------------
0413:            public void testAddMinutes() throws Exception {
0414:                Date base = new Date(MILLIS_TEST);
0415:                Date result = DateUtils.addMinutes(base, 0);
0416:                assertNotSame(base, result);
0417:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0418:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0419:
0420:                result = DateUtils.addMinutes(base, 1);
0421:                assertNotSame(base, result);
0422:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0423:                assertDate(result, 2000, 6, 5, 4, 4, 2, 1);
0424:
0425:                result = DateUtils.addMinutes(base, -1);
0426:                assertNotSame(base, result);
0427:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0428:                assertDate(result, 2000, 6, 5, 4, 2, 2, 1);
0429:            }
0430:
0431:            //-----------------------------------------------------------------------
0432:            public void testAddSeconds() throws Exception {
0433:                Date base = new Date(MILLIS_TEST);
0434:                Date result = DateUtils.addSeconds(base, 0);
0435:                assertNotSame(base, result);
0436:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0437:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0438:
0439:                result = DateUtils.addSeconds(base, 1);
0440:                assertNotSame(base, result);
0441:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0442:                assertDate(result, 2000, 6, 5, 4, 3, 3, 1);
0443:
0444:                result = DateUtils.addSeconds(base, -1);
0445:                assertNotSame(base, result);
0446:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0447:                assertDate(result, 2000, 6, 5, 4, 3, 1, 1);
0448:            }
0449:
0450:            //-----------------------------------------------------------------------
0451:            public void testAddMilliseconds() throws Exception {
0452:                Date base = new Date(MILLIS_TEST);
0453:                Date result = DateUtils.addMilliseconds(base, 0);
0454:                assertNotSame(base, result);
0455:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0456:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0457:
0458:                result = DateUtils.addMilliseconds(base, 1);
0459:                assertNotSame(base, result);
0460:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0461:                assertDate(result, 2000, 6, 5, 4, 3, 2, 2);
0462:
0463:                result = DateUtils.addMilliseconds(base, -1);
0464:                assertNotSame(base, result);
0465:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0466:                assertDate(result, 2000, 6, 5, 4, 3, 2, 0);
0467:            }
0468:
0469:            //-----------------------------------------------------------------------
0470:            public void testAddByField() throws Exception {
0471:                Date base = new Date(MILLIS_TEST);
0472:                Date result = DateUtils.add(base, Calendar.YEAR, 0);
0473:                assertNotSame(base, result);
0474:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0475:                assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
0476:
0477:                result = DateUtils.add(base, Calendar.YEAR, 1);
0478:                assertNotSame(base, result);
0479:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0480:                assertDate(result, 2001, 6, 5, 4, 3, 2, 1);
0481:
0482:                result = DateUtils.add(base, Calendar.YEAR, -1);
0483:                assertNotSame(base, result);
0484:                assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
0485:                assertDate(result, 1999, 6, 5, 4, 3, 2, 1);
0486:            }
0487:
0488:            //-----------------------------------------------------------------------
0489:            private void assertDate(Date date, int year, int month, int day,
0490:                    int hour, int min, int sec, int mil) throws Exception {
0491:                GregorianCalendar cal = new GregorianCalendar();
0492:                cal.setTime(date);
0493:                assertEquals(year, cal.get(Calendar.YEAR));
0494:                assertEquals(month, cal.get(Calendar.MONTH));
0495:                assertEquals(day, cal.get(Calendar.DAY_OF_MONTH));
0496:                assertEquals(hour, cal.get(Calendar.HOUR_OF_DAY));
0497:                assertEquals(min, cal.get(Calendar.MINUTE));
0498:                assertEquals(sec, cal.get(Calendar.SECOND));
0499:                assertEquals(mil, cal.get(Calendar.MILLISECOND));
0500:            }
0501:
0502:            //-----------------------------------------------------------------------
0503:            /**
0504:             * Tests various values with the round method
0505:             */
0506:            public void testRound() throws Exception {
0507:                // tests for public static Date round(Date date, int field)
0508:                assertEquals("round year-1 failed", dateParser
0509:                        .parse("January 1, 2002"), DateUtils.round(date1,
0510:                        Calendar.YEAR));
0511:                assertEquals("round year-2 failed", dateParser
0512:                        .parse("January 1, 2002"), DateUtils.round(date2,
0513:                        Calendar.YEAR));
0514:                assertEquals("round month-1 failed", dateParser
0515:                        .parse("February 1, 2002"), DateUtils.round(date1,
0516:                        Calendar.MONTH));
0517:                assertEquals("round month-2 failed", dateParser
0518:                        .parse("December 1, 2001"), DateUtils.round(date2,
0519:                        Calendar.MONTH));
0520:                assertEquals("round semimonth-0 failed", dateParser
0521:                        .parse("February 1, 2002"), DateUtils.round(date0,
0522:                        DateUtils.SEMI_MONTH));
0523:                assertEquals("round semimonth-1 failed", dateParser
0524:                        .parse("February 16, 2002"), DateUtils.round(date1,
0525:                        DateUtils.SEMI_MONTH));
0526:                assertEquals("round semimonth-2 failed", dateParser
0527:                        .parse("November 16, 2001"), DateUtils.round(date2,
0528:                        DateUtils.SEMI_MONTH));
0529:
0530:                assertEquals("round date-1 failed", dateParser
0531:                        .parse("February 13, 2002"), DateUtils.round(date1,
0532:                        Calendar.DATE));
0533:                assertEquals("round date-2 failed", dateParser
0534:                        .parse("November 18, 2001"), DateUtils.round(date2,
0535:                        Calendar.DATE));
0536:                assertEquals("round hour-1 failed", dateTimeParser
0537:                        .parse("February 12, 2002 13:00:00.000"), DateUtils
0538:                        .round(date1, Calendar.HOUR));
0539:                assertEquals("round hour-2 failed", dateTimeParser
0540:                        .parse("November 18, 2001 1:00:00.000"), DateUtils
0541:                        .round(date2, Calendar.HOUR));
0542:                assertEquals("round minute-1 failed", dateTimeParser
0543:                        .parse("February 12, 2002 12:35:00.000"), DateUtils
0544:                        .round(date1, Calendar.MINUTE));
0545:                assertEquals("round minute-2 failed", dateTimeParser
0546:                        .parse("November 18, 2001 1:23:00.000"), DateUtils
0547:                        .round(date2, Calendar.MINUTE));
0548:                assertEquals("round second-1 failed", dateTimeParser
0549:                        .parse("February 12, 2002 12:34:57.000"), DateUtils
0550:                        .round(date1, Calendar.SECOND));
0551:                assertEquals("round second-2 failed", dateTimeParser
0552:                        .parse("November 18, 2001 1:23:11.000"), DateUtils
0553:                        .round(date2, Calendar.SECOND));
0554:                assertEquals("truncate ampm-1 failed", dateTimeParser
0555:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0556:                        .round(dateAmPm1, Calendar.AM_PM));
0557:                assertEquals("truncate ampm-2 failed", dateTimeParser
0558:                        .parse("February 4, 2002 00:00:00.000"), DateUtils
0559:                        .round(dateAmPm2, Calendar.AM_PM));
0560:                assertEquals("truncate ampm-3 failed", dateTimeParser
0561:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0562:                        .round(dateAmPm3, Calendar.AM_PM));
0563:                assertEquals("truncate ampm-4 failed", dateTimeParser
0564:                        .parse("February 4, 2002 12:00:00.000"), DateUtils
0565:                        .round(dateAmPm4, Calendar.AM_PM));
0566:
0567:                // tests for public static Date round(Object date, int field)
0568:                assertEquals("round year-1 failed", dateParser
0569:                        .parse("January 1, 2002"), DateUtils.round(
0570:                        (Object) date1, Calendar.YEAR));
0571:                assertEquals("round year-2 failed", dateParser
0572:                        .parse("January 1, 2002"), DateUtils.round(
0573:                        (Object) date2, Calendar.YEAR));
0574:                assertEquals("round month-1 failed", dateParser
0575:                        .parse("February 1, 2002"), DateUtils.round(
0576:                        (Object) date1, Calendar.MONTH));
0577:                assertEquals("round month-2 failed", dateParser
0578:                        .parse("December 1, 2001"), DateUtils.round(
0579:                        (Object) date2, Calendar.MONTH));
0580:                assertEquals("round semimonth-1 failed", dateParser
0581:                        .parse("February 16, 2002"), DateUtils.round(
0582:                        (Object) date1, DateUtils.SEMI_MONTH));
0583:                assertEquals("round semimonth-2 failed", dateParser
0584:                        .parse("November 16, 2001"), DateUtils.round(
0585:                        (Object) date2, DateUtils.SEMI_MONTH));
0586:                assertEquals("round date-1 failed", dateParser
0587:                        .parse("February 13, 2002"), DateUtils.round(
0588:                        (Object) date1, Calendar.DATE));
0589:                assertEquals("round date-2 failed", dateParser
0590:                        .parse("November 18, 2001"), DateUtils.round(
0591:                        (Object) date2, Calendar.DATE));
0592:                assertEquals("round hour-1 failed", dateTimeParser
0593:                        .parse("February 12, 2002 13:00:00.000"), DateUtils
0594:                        .round((Object) date1, Calendar.HOUR));
0595:                assertEquals("round hour-2 failed", dateTimeParser
0596:                        .parse("November 18, 2001 1:00:00.000"), DateUtils
0597:                        .round((Object) date2, Calendar.HOUR));
0598:                assertEquals("round minute-1 failed", dateTimeParser
0599:                        .parse("February 12, 2002 12:35:00.000"), DateUtils
0600:                        .round((Object) date1, Calendar.MINUTE));
0601:                assertEquals("round minute-2 failed", dateTimeParser
0602:                        .parse("November 18, 2001 1:23:00.000"), DateUtils
0603:                        .round((Object) date2, Calendar.MINUTE));
0604:                assertEquals("round second-1 failed", dateTimeParser
0605:                        .parse("February 12, 2002 12:34:57.000"), DateUtils
0606:                        .round((Object) date1, Calendar.SECOND));
0607:                assertEquals("round second-2 failed", dateTimeParser
0608:                        .parse("November 18, 2001 1:23:11.000"), DateUtils
0609:                        .round((Object) date2, Calendar.SECOND));
0610:                assertEquals("round calendar second-1 failed", dateTimeParser
0611:                        .parse("February 12, 2002 12:34:57.000"), DateUtils
0612:                        .round((Object) cal1, Calendar.SECOND));
0613:                assertEquals("round calendar second-2 failed", dateTimeParser
0614:                        .parse("November 18, 2001 1:23:11.000"), DateUtils
0615:                        .round((Object) cal2, Calendar.SECOND));
0616:                assertEquals("truncate ampm-1 failed", dateTimeParser
0617:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0618:                        .round((Object) dateAmPm1, Calendar.AM_PM));
0619:                assertEquals("truncate ampm-2 failed", dateTimeParser
0620:                        .parse("February 4, 2002 00:00:00.000"), DateUtils
0621:                        .round((Object) dateAmPm2, Calendar.AM_PM));
0622:                assertEquals("truncate ampm-3 failed", dateTimeParser
0623:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0624:                        .round((Object) dateAmPm3, Calendar.AM_PM));
0625:                assertEquals("truncate ampm-4 failed", dateTimeParser
0626:                        .parse("February 4, 2002 12:00:00.000"), DateUtils
0627:                        .round((Object) dateAmPm4, Calendar.AM_PM));
0628:
0629:                try {
0630:                    DateUtils.round((Date) null, Calendar.SECOND);
0631:                    fail();
0632:                } catch (IllegalArgumentException ex) {
0633:                }
0634:                try {
0635:                    DateUtils.round((Calendar) null, Calendar.SECOND);
0636:                    fail();
0637:                } catch (IllegalArgumentException ex) {
0638:                }
0639:                try {
0640:                    DateUtils.round((Object) null, Calendar.SECOND);
0641:                    fail();
0642:                } catch (IllegalArgumentException ex) {
0643:                }
0644:                try {
0645:                    DateUtils.round("", Calendar.SECOND);
0646:                    fail();
0647:                } catch (ClassCastException ex) {
0648:                }
0649:                try {
0650:                    DateUtils.round(date1, -9999);
0651:                    fail();
0652:                } catch (IllegalArgumentException ex) {
0653:                }
0654:
0655:                assertEquals("truncate ampm-1 failed", dateTimeParser
0656:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0657:                        .round((Object) calAmPm1, Calendar.AM_PM));
0658:                assertEquals("truncate ampm-2 failed", dateTimeParser
0659:                        .parse("February 4, 2002 00:00:00.000"), DateUtils
0660:                        .round((Object) calAmPm2, Calendar.AM_PM));
0661:                assertEquals("truncate ampm-3 failed", dateTimeParser
0662:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0663:                        .round((Object) calAmPm3, Calendar.AM_PM));
0664:                assertEquals("truncate ampm-4 failed", dateTimeParser
0665:                        .parse("February 4, 2002 12:00:00.000"), DateUtils
0666:                        .round((Object) calAmPm4, Calendar.AM_PM));
0667:
0668:                // Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
0669:                // Test rounding across the beginning of daylight saving time
0670:                TimeZone.setDefault(zone);
0671:                dateTimeParser.setTimeZone(zone);
0672:                assertEquals("round MET date across DST change-over",
0673:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0674:                        DateUtils.round(date4, Calendar.DATE));
0675:                assertEquals("round MET date across DST change-over",
0676:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0677:                        DateUtils.round((Object) cal4, Calendar.DATE));
0678:                assertEquals("round MET date across DST change-over",
0679:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0680:                        DateUtils.round(date5, Calendar.DATE));
0681:                assertEquals("round MET date across DST change-over",
0682:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0683:                        DateUtils.round((Object) cal5, Calendar.DATE));
0684:                assertEquals("round MET date across DST change-over",
0685:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0686:                        DateUtils.round(date6, Calendar.DATE));
0687:                assertEquals("round MET date across DST change-over",
0688:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0689:                        DateUtils.round((Object) cal6, Calendar.DATE));
0690:                assertEquals("round MET date across DST change-over",
0691:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0692:                        DateUtils.round(date7, Calendar.DATE));
0693:                assertEquals("round MET date across DST change-over",
0694:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0695:                        DateUtils.round((Object) cal7, Calendar.DATE));
0696:
0697:                assertEquals("round MET date across DST change-over",
0698:                        dateTimeParser.parse("March 30, 2003 01:00:00.000"),
0699:                        DateUtils.round(date4, Calendar.HOUR_OF_DAY));
0700:                assertEquals("round MET date across DST change-over",
0701:                        dateTimeParser.parse("March 30, 2003 01:00:00.000"),
0702:                        DateUtils.round((Object) cal4, Calendar.HOUR_OF_DAY));
0703:                if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
0704:                    assertEquals(
0705:                            "round MET date across DST change-over",
0706:                            dateTimeParser.parse("March 30, 2003 03:00:00.000"),
0707:                            DateUtils.round(date5, Calendar.HOUR_OF_DAY));
0708:                    assertEquals(
0709:                            "round MET date across DST change-over",
0710:                            dateTimeParser.parse("March 30, 2003 03:00:00.000"),
0711:                            DateUtils
0712:                                    .round((Object) cal5, Calendar.HOUR_OF_DAY));
0713:                    assertEquals(
0714:                            "round MET date across DST change-over",
0715:                            dateTimeParser.parse("March 30, 2003 03:00:00.000"),
0716:                            DateUtils.round(date6, Calendar.HOUR_OF_DAY));
0717:                    assertEquals(
0718:                            "round MET date across DST change-over",
0719:                            dateTimeParser.parse("March 30, 2003 03:00:00.000"),
0720:                            DateUtils
0721:                                    .round((Object) cal6, Calendar.HOUR_OF_DAY));
0722:                    assertEquals(
0723:                            "round MET date across DST change-over",
0724:                            dateTimeParser.parse("March 30, 2003 04:00:00.000"),
0725:                            DateUtils.round(date7, Calendar.HOUR_OF_DAY));
0726:                    assertEquals(
0727:                            "round MET date across DST change-over",
0728:                            dateTimeParser.parse("March 30, 2003 04:00:00.000"),
0729:                            DateUtils
0730:                                    .round((Object) cal7, Calendar.HOUR_OF_DAY));
0731:                } else {
0732:                    this 
0733:                            .warn("WARNING: Some date rounding tests not run since the current version is "
0734:                                    + SystemUtils.JAVA_VERSION);
0735:                }
0736:                TimeZone.setDefault(defaultZone);
0737:                dateTimeParser.setTimeZone(defaultZone);
0738:            }
0739:
0740:            /**
0741:             * Tests various values with the trunc method
0742:             */
0743:            public void testTruncate() throws Exception {
0744:                // tests public static Date truncate(Date date, int field)
0745:                assertEquals("truncate year-1 failed", dateParser
0746:                        .parse("January 1, 2002"), DateUtils.truncate(date1,
0747:                        Calendar.YEAR));
0748:                assertEquals("truncate year-2 failed", dateParser
0749:                        .parse("January 1, 2001"), DateUtils.truncate(date2,
0750:                        Calendar.YEAR));
0751:                assertEquals("truncate month-1 failed", dateParser
0752:                        .parse("February 1, 2002"), DateUtils.truncate(date1,
0753:                        Calendar.MONTH));
0754:                assertEquals("truncate month-2 failed", dateParser
0755:                        .parse("November 1, 2001"), DateUtils.truncate(date2,
0756:                        Calendar.MONTH));
0757:                assertEquals("truncate semimonth-1 failed", dateParser
0758:                        .parse("February 1, 2002"), DateUtils.truncate(date1,
0759:                        DateUtils.SEMI_MONTH));
0760:                assertEquals("truncate semimonth-2 failed", dateParser
0761:                        .parse("November 16, 2001"), DateUtils.truncate(date2,
0762:                        DateUtils.SEMI_MONTH));
0763:                assertEquals("truncate date-1 failed", dateParser
0764:                        .parse("February 12, 2002"), DateUtils.truncate(date1,
0765:                        Calendar.DATE));
0766:                assertEquals("truncate date-2 failed", dateParser
0767:                        .parse("November 18, 2001"), DateUtils.truncate(date2,
0768:                        Calendar.DATE));
0769:                assertEquals("truncate hour-1 failed", dateTimeParser
0770:                        .parse("February 12, 2002 12:00:00.000"), DateUtils
0771:                        .truncate(date1, Calendar.HOUR));
0772:                assertEquals("truncate hour-2 failed", dateTimeParser
0773:                        .parse("November 18, 2001 1:00:00.000"), DateUtils
0774:                        .truncate(date2, Calendar.HOUR));
0775:                assertEquals("truncate minute-1 failed", dateTimeParser
0776:                        .parse("February 12, 2002 12:34:00.000"), DateUtils
0777:                        .truncate(date1, Calendar.MINUTE));
0778:                assertEquals("truncate minute-2 failed", dateTimeParser
0779:                        .parse("November 18, 2001 1:23:00.000"), DateUtils
0780:                        .truncate(date2, Calendar.MINUTE));
0781:                assertEquals("truncate second-1 failed", dateTimeParser
0782:                        .parse("February 12, 2002 12:34:56.000"), DateUtils
0783:                        .truncate(date1, Calendar.SECOND));
0784:                assertEquals("truncate second-2 failed", dateTimeParser
0785:                        .parse("November 18, 2001 1:23:11.000"), DateUtils
0786:                        .truncate(date2, Calendar.SECOND));
0787:                assertEquals("truncate ampm-1 failed", dateTimeParser
0788:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0789:                        .truncate(dateAmPm1, Calendar.AM_PM));
0790:                assertEquals("truncate ampm-2 failed", dateTimeParser
0791:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0792:                        .truncate(dateAmPm2, Calendar.AM_PM));
0793:                assertEquals("truncate ampm-3 failed", dateTimeParser
0794:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0795:                        .truncate(dateAmPm3, Calendar.AM_PM));
0796:                assertEquals("truncate ampm-4 failed", dateTimeParser
0797:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0798:                        .truncate(dateAmPm4, Calendar.AM_PM));
0799:
0800:                // tests public static Date truncate(Object date, int field)
0801:                assertEquals("truncate year-1 failed", dateParser
0802:                        .parse("January 1, 2002"), DateUtils.truncate(
0803:                        (Object) date1, Calendar.YEAR));
0804:                assertEquals("truncate year-2 failed", dateParser
0805:                        .parse("January 1, 2001"), DateUtils.truncate(
0806:                        (Object) date2, Calendar.YEAR));
0807:                assertEquals("truncate month-1 failed", dateParser
0808:                        .parse("February 1, 2002"), DateUtils.truncate(
0809:                        (Object) date1, Calendar.MONTH));
0810:                assertEquals("truncate month-2 failed", dateParser
0811:                        .parse("November 1, 2001"), DateUtils.truncate(
0812:                        (Object) date2, Calendar.MONTH));
0813:                assertEquals("truncate semimonth-1 failed", dateParser
0814:                        .parse("February 1, 2002"), DateUtils.truncate(
0815:                        (Object) date1, DateUtils.SEMI_MONTH));
0816:                assertEquals("truncate semimonth-2 failed", dateParser
0817:                        .parse("November 16, 2001"), DateUtils.truncate(
0818:                        (Object) date2, DateUtils.SEMI_MONTH));
0819:                assertEquals("truncate date-1 failed", dateParser
0820:                        .parse("February 12, 2002"), DateUtils.truncate(
0821:                        (Object) date1, Calendar.DATE));
0822:                assertEquals("truncate date-2 failed", dateParser
0823:                        .parse("November 18, 2001"), DateUtils.truncate(
0824:                        (Object) date2, Calendar.DATE));
0825:                assertEquals("truncate hour-1 failed", dateTimeParser
0826:                        .parse("February 12, 2002 12:00:00.000"), DateUtils
0827:                        .truncate((Object) date1, Calendar.HOUR));
0828:                assertEquals("truncate hour-2 failed", dateTimeParser
0829:                        .parse("November 18, 2001 1:00:00.000"), DateUtils
0830:                        .truncate((Object) date2, Calendar.HOUR));
0831:                assertEquals("truncate minute-1 failed", dateTimeParser
0832:                        .parse("February 12, 2002 12:34:00.000"), DateUtils
0833:                        .truncate((Object) date1, Calendar.MINUTE));
0834:                assertEquals("truncate minute-2 failed", dateTimeParser
0835:                        .parse("November 18, 2001 1:23:00.000"), DateUtils
0836:                        .truncate((Object) date2, Calendar.MINUTE));
0837:                assertEquals("truncate second-1 failed", dateTimeParser
0838:                        .parse("February 12, 2002 12:34:56.000"), DateUtils
0839:                        .truncate((Object) date1, Calendar.SECOND));
0840:                assertEquals("truncate second-2 failed", dateTimeParser
0841:                        .parse("November 18, 2001 1:23:11.000"), DateUtils
0842:                        .truncate((Object) date2, Calendar.SECOND));
0843:                assertEquals("truncate ampm-1 failed", dateTimeParser
0844:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0845:                        .truncate((Object) dateAmPm1, Calendar.AM_PM));
0846:                assertEquals("truncate ampm-2 failed", dateTimeParser
0847:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0848:                        .truncate((Object) dateAmPm2, Calendar.AM_PM));
0849:                assertEquals("truncate ampm-3 failed", dateTimeParser
0850:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0851:                        .truncate((Object) dateAmPm3, Calendar.AM_PM));
0852:                assertEquals("truncate ampm-4 failed", dateTimeParser
0853:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0854:                        .truncate((Object) dateAmPm4, Calendar.AM_PM));
0855:
0856:                assertEquals("truncate calendar second-1 failed",
0857:                        dateTimeParser.parse("February 12, 2002 12:34:56.000"),
0858:                        DateUtils.truncate((Object) cal1, Calendar.SECOND));
0859:                assertEquals("truncate calendar second-2 failed",
0860:                        dateTimeParser.parse("November 18, 2001 1:23:11.000"),
0861:                        DateUtils.truncate((Object) cal2, Calendar.SECOND));
0862:
0863:                assertEquals("truncate ampm-1 failed", dateTimeParser
0864:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0865:                        .truncate((Object) calAmPm1, Calendar.AM_PM));
0866:                assertEquals("truncate ampm-2 failed", dateTimeParser
0867:                        .parse("February 3, 2002 00:00:00.000"), DateUtils
0868:                        .truncate((Object) calAmPm2, Calendar.AM_PM));
0869:                assertEquals("truncate ampm-3 failed", dateTimeParser
0870:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0871:                        .truncate((Object) calAmPm3, Calendar.AM_PM));
0872:                assertEquals("truncate ampm-4 failed", dateTimeParser
0873:                        .parse("February 3, 2002 12:00:00.000"), DateUtils
0874:                        .truncate((Object) calAmPm4, Calendar.AM_PM));
0875:
0876:                try {
0877:                    DateUtils.truncate((Date) null, Calendar.SECOND);
0878:                    fail();
0879:                } catch (IllegalArgumentException ex) {
0880:                }
0881:                try {
0882:                    DateUtils.truncate((Calendar) null, Calendar.SECOND);
0883:                    fail();
0884:                } catch (IllegalArgumentException ex) {
0885:                }
0886:                try {
0887:                    DateUtils.truncate((Object) null, Calendar.SECOND);
0888:                    fail();
0889:                } catch (IllegalArgumentException ex) {
0890:                }
0891:                try {
0892:                    DateUtils.truncate("", Calendar.SECOND);
0893:                    fail();
0894:                } catch (ClassCastException ex) {
0895:                }
0896:
0897:                // Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
0898:                // Test truncate across beginning of daylight saving time
0899:                TimeZone.setDefault(zone);
0900:                dateTimeParser.setTimeZone(zone);
0901:                assertEquals("truncate MET date across DST change-over",
0902:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0903:                        DateUtils.truncate(date3, Calendar.DATE));
0904:                assertEquals("truncate MET date across DST change-over",
0905:                        dateTimeParser.parse("March 30, 2003 00:00:00.000"),
0906:                        DateUtils.truncate((Object) cal3, Calendar.DATE));
0907:                // Test truncate across end of daylight saving time
0908:                assertEquals("truncate MET date across DST change-over",
0909:                        dateTimeParser.parse("October 26, 2003 00:00:00.000"),
0910:                        DateUtils.truncate(date8, Calendar.DATE));
0911:                assertEquals("truncate MET date across DST change-over",
0912:                        dateTimeParser.parse("October 26, 2003 00:00:00.000"),
0913:                        DateUtils.truncate((Object) cal8, Calendar.DATE));
0914:                TimeZone.setDefault(defaultZone);
0915:                dateTimeParser.setTimeZone(defaultZone);
0916:
0917:                // Bug 31395, large dates
0918:                Date endOfTime = new Date(Long.MAX_VALUE); // fyi: Sun Aug 17 07:12:55 CET 292278994 -- 807 millis
0919:                GregorianCalendar endCal = new GregorianCalendar();
0920:                endCal.setTime(endOfTime);
0921:                try {
0922:                    DateUtils.truncate(endCal, Calendar.DATE);
0923:                    fail();
0924:                } catch (ArithmeticException ex) {
0925:                }
0926:                endCal.set(Calendar.YEAR, 280000001);
0927:                try {
0928:                    DateUtils.truncate(endCal, Calendar.DATE);
0929:                    fail();
0930:                } catch (ArithmeticException ex) {
0931:                }
0932:                endCal.set(Calendar.YEAR, 280000000);
0933:                Calendar cal = DateUtils.truncate(endCal, Calendar.DATE);
0934:                assertEquals(0, cal.get(Calendar.HOUR));
0935:            }
0936:
0937:            /**
0938:             * Tests for LANG-59
0939:             *
0940:             * see http://issues.apache.org/jira/browse/LANG-59
0941:             */
0942:            public void testTruncateLang59() throws Exception {
0943:                if (!SystemUtils.isJavaVersionAtLeast(1.4f)) {
0944:                    this 
0945:                            .warn("WARNING: Test for LANG-59 not run since the current version is "
0946:                                    + SystemUtils.JAVA_VERSION);
0947:                    return;
0948:                }
0949:
0950:                // Set TimeZone to Mountain Time
0951:                TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
0952:                TimeZone.setDefault(MST_MDT);
0953:                DateFormat format = new SimpleDateFormat(
0954:                        "yyyy-MM-dd HH:mm:ss.SSS z");
0955:                format.setTimeZone(MST_MDT);
0956:
0957:                Date oct31_01MDT = new Date(1099206000000L);
0958:
0959:                Date oct31MDT = new Date(oct31_01MDT.getTime() - 3600000L); // - 1 hour
0960:                Date oct31_01_02MDT = new Date(oct31_01MDT.getTime() + 120000L); // + 2 minutes
0961:                Date oct31_01_02_03MDT = new Date(
0962:                        oct31_01_02MDT.getTime() + 3000L); // + 3 seconds
0963:                Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT
0964:                        .getTime() + 4L); // + 4 milliseconds
0965:
0966:                assertEquals("Check 00:00:00.000",
0967:                        "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
0968:                assertEquals("Check 01:00:00.000",
0969:                        "2004-10-31 01:00:00.000 MDT", format
0970:                                .format(oct31_01MDT));
0971:                assertEquals("Check 01:02:00.000",
0972:                        "2004-10-31 01:02:00.000 MDT", format
0973:                                .format(oct31_01_02MDT));
0974:                assertEquals("Check 01:02:03.000",
0975:                        "2004-10-31 01:02:03.000 MDT", format
0976:                                .format(oct31_01_02_03MDT));
0977:                assertEquals("Check 01:02:03.004",
0978:                        "2004-10-31 01:02:03.004 MDT", format
0979:                                .format(oct31_01_02_03_04MDT));
0980:
0981:                // ------- Demonstrate Problem -------
0982:                Calendar gval = Calendar.getInstance();
0983:                gval.setTime(new Date(oct31_01MDT.getTime()));
0984:                gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
0985:                assertEquals("Demonstrate Problem", gval.getTime().getTime(),
0986:                        oct31_01MDT.getTime() + 3600000L);
0987:
0988:                // ---------- Test Truncate ----------
0989:                assertEquals("Truncate Calendar.MILLISECOND",
0990:                        oct31_01_02_03_04MDT, DateUtils.truncate(
0991:                                oct31_01_02_03_04MDT, Calendar.MILLISECOND));
0992:
0993:                assertEquals("Truncate Calendar.SECOND", oct31_01_02_03MDT,
0994:                        DateUtils.truncate(oct31_01_02_03_04MDT,
0995:                                Calendar.SECOND));
0996:
0997:                assertEquals("Truncate Calendar.MINUTE", oct31_01_02MDT,
0998:                        DateUtils.truncate(oct31_01_02_03_04MDT,
0999:                                Calendar.MINUTE));
1000:
1001:                assertEquals("Truncate Calendar.HOUR_OF_DAY", oct31_01MDT,
1002:                        DateUtils.truncate(oct31_01_02_03_04MDT,
1003:                                Calendar.HOUR_OF_DAY));
1004:
1005:                assertEquals("Truncate Calendar.HOUR", oct31_01MDT, DateUtils
1006:                        .truncate(oct31_01_02_03_04MDT, Calendar.HOUR));
1007:
1008:                assertEquals("Truncate Calendar.DATE", oct31MDT, DateUtils
1009:                        .truncate(oct31_01_02_03_04MDT, Calendar.DATE));
1010:
1011:                // ---------- Test Round (down) ----------
1012:                assertEquals("Round Calendar.MILLISECOND",
1013:                        oct31_01_02_03_04MDT, DateUtils.round(
1014:                                oct31_01_02_03_04MDT, Calendar.MILLISECOND));
1015:
1016:                assertEquals("Round Calendar.SECOND", oct31_01_02_03MDT,
1017:                        DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));
1018:
1019:                assertEquals("Round Calendar.MINUTE", oct31_01_02MDT, DateUtils
1020:                        .round(oct31_01_02_03_04MDT, Calendar.MINUTE));
1021:
1022:                assertEquals("Round Calendar.HOUR_OF_DAY", oct31_01MDT,
1023:                        DateUtils.round(oct31_01_02_03_04MDT,
1024:                                Calendar.HOUR_OF_DAY));
1025:
1026:                assertEquals("Round Calendar.HOUR", oct31_01MDT, DateUtils
1027:                        .round(oct31_01_02_03_04MDT, Calendar.HOUR));
1028:
1029:                assertEquals("Round Calendar.DATE", oct31MDT, DateUtils.round(
1030:                        oct31_01_02_03_04MDT, Calendar.DATE));
1031:
1032:                // restore default time zone
1033:                TimeZone.setDefault(defaultZone);
1034:            }
1035:
1036:            /**
1037:             * Tests the iterator exceptions
1038:             */
1039:            public void testIteratorEx() throws Exception {
1040:                try {
1041:                    DateUtils.iterator(Calendar.getInstance(), -9999);
1042:                } catch (IllegalArgumentException ex) {
1043:                }
1044:                try {
1045:                    DateUtils
1046:                            .iterator((Date) null, DateUtils.RANGE_WEEK_CENTER);
1047:                    fail();
1048:                } catch (IllegalArgumentException ex) {
1049:                }
1050:                try {
1051:                    DateUtils.iterator((Calendar) null,
1052:                            DateUtils.RANGE_WEEK_CENTER);
1053:                    fail();
1054:                } catch (IllegalArgumentException ex) {
1055:                }
1056:                try {
1057:                    DateUtils.iterator((Object) null,
1058:                            DateUtils.RANGE_WEEK_CENTER);
1059:                    fail();
1060:                } catch (IllegalArgumentException ex) {
1061:                }
1062:                try {
1063:                    DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER);
1064:                    fail();
1065:                } catch (ClassCastException ex) {
1066:                }
1067:            }
1068:
1069:            /**
1070:             * Tests the calendar iterator for week ranges
1071:             */
1072:            public void testWeekIterator() throws Exception {
1073:                Calendar now = Calendar.getInstance();
1074:                for (int i = 0; i < 7; i++) {
1075:                    Calendar today = DateUtils.truncate(now, Calendar.DATE);
1076:                    Calendar sunday = DateUtils.truncate(now, Calendar.DATE);
1077:                    sunday.add(Calendar.DATE, 1 - sunday
1078:                            .get(Calendar.DAY_OF_WEEK));
1079:                    Calendar monday = DateUtils.truncate(now, Calendar.DATE);
1080:                    if (monday.get(Calendar.DAY_OF_WEEK) == 1) {
1081:                        //This is sunday... roll back 6 days
1082:                        monday.add(Calendar.DATE, -6);
1083:                    } else {
1084:                        monday.add(Calendar.DATE, 2 - monday
1085:                                .get(Calendar.DAY_OF_WEEK));
1086:                    }
1087:                    Calendar centered = DateUtils.truncate(now, Calendar.DATE);
1088:                    centered.add(Calendar.DATE, -3);
1089:
1090:                    Iterator it = DateUtils.iterator(now,
1091:                            DateUtils.RANGE_WEEK_SUNDAY);
1092:                    assertWeekIterator(it, sunday);
1093:                    it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY);
1094:                    assertWeekIterator(it, monday);
1095:                    it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE);
1096:                    assertWeekIterator(it, today);
1097:                    it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
1098:                    assertWeekIterator(it, centered);
1099:
1100:                    it = DateUtils.iterator((Object) now,
1101:                            DateUtils.RANGE_WEEK_CENTER);
1102:                    assertWeekIterator(it, centered);
1103:                    it = DateUtils.iterator((Object) now.getTime(),
1104:                            DateUtils.RANGE_WEEK_CENTER);
1105:                    assertWeekIterator(it, centered);
1106:                    try {
1107:                        it.next();
1108:                        fail();
1109:                    } catch (NoSuchElementException ex) {
1110:                    }
1111:                    it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
1112:                    it.next();
1113:                    try {
1114:                        it.remove();
1115:                    } catch (UnsupportedOperationException ex) {
1116:                    }
1117:
1118:                    now.add(Calendar.DATE, 1);
1119:                }
1120:            }
1121:
1122:            /**
1123:             * Tests the calendar iterator for month-based ranges
1124:             */
1125:            public void testMonthIterator() throws Exception {
1126:                Iterator it = DateUtils.iterator(date1,
1127:                        DateUtils.RANGE_MONTH_SUNDAY);
1128:                assertWeekIterator(it, dateParser.parse("January 27, 2002"),
1129:                        dateParser.parse("March 2, 2002"));
1130:
1131:                it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY);
1132:                assertWeekIterator(it, dateParser.parse("January 28, 2002"),
1133:                        dateParser.parse("March 3, 2002"));
1134:
1135:                it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
1136:                assertWeekIterator(it, dateParser.parse("October 28, 2001"),
1137:                        dateParser.parse("December 1, 2001"));
1138:
1139:                it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY);
1140:                assertWeekIterator(it, dateParser.parse("October 29, 2001"),
1141:                        dateParser.parse("December 2, 2001"));
1142:            }
1143:
1144:            /**
1145:             * This checks that this is a 7 element iterator of Calendar objects
1146:             * that are dates (no time), and exactly 1 day spaced after each other.
1147:             */
1148:            private static void assertWeekIterator(Iterator it, Calendar start) {
1149:                Calendar end = (Calendar) start.clone();
1150:                end.add(Calendar.DATE, 6);
1151:
1152:                assertWeekIterator(it, start, end);
1153:            }
1154:
1155:            /**
1156:             * Convenience method for when working with Date objects
1157:             */
1158:            private static void assertWeekIterator(Iterator it, Date start,
1159:                    Date end) {
1160:                Calendar calStart = Calendar.getInstance();
1161:                calStart.setTime(start);
1162:                Calendar calEnd = Calendar.getInstance();
1163:                calEnd.setTime(end);
1164:
1165:                assertWeekIterator(it, calStart, calEnd);
1166:            }
1167:
1168:            /**
1169:             * This checks that this is a 7 divisble iterator of Calendar objects
1170:             * that are dates (no time), and exactly 1 day spaced after each other
1171:             * (in addition to the proper start and stop dates)
1172:             */
1173:            private static void assertWeekIterator(Iterator it, Calendar start,
1174:                    Calendar end) {
1175:                Calendar cal = (Calendar) it.next();
1176:                assertEquals("", start, cal, 0);
1177:                Calendar last = null;
1178:                int count = 1;
1179:                while (it.hasNext()) {
1180:                    //Check this is just a date (no time component)
1181:                    assertEquals("", cal, DateUtils
1182:                            .truncate(cal, Calendar.DATE), 0);
1183:
1184:                    last = cal;
1185:                    cal = (Calendar) it.next();
1186:                    count++;
1187:
1188:                    //Check that this is one day more than the last date
1189:                    last.add(Calendar.DATE, 1);
1190:                    assertEquals("", last, cal, 0);
1191:                }
1192:                if (count % 7 != 0) {
1193:                    throw new AssertionFailedError("There were " + count
1194:                            + " days in this iterator");
1195:                }
1196:                assertEquals("", end, cal, 0);
1197:            }
1198:
1199:            /**
1200:             * Used to check that Calendar objects are close enough
1201:             * delta is in milliseconds
1202:             */
1203:            private static void assertEquals(String message, Calendar cal1,
1204:                    Calendar cal2, long delta) {
1205:                if (Math.abs(cal1.getTime().getTime()
1206:                        - cal2.getTime().getTime()) > delta) {
1207:                    throw new AssertionFailedError(message + " expected "
1208:                            + cal1.getTime() + " but got " + cal2.getTime());
1209:                }
1210:            }
1211:
1212:            void warn(String msg) {
1213:                System.err.println(msg);
1214:            }
1215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.