Source Code Cross Referenced for JDateTimeMoreTest.java in  » Development » jodd » jodd » datetime » 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 » Development » jodd » jodd.datetime 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003:        package jodd.datetime;
004:
005:        import java.util.GregorianCalendar;
006:        import java.util.Calendar;
007:
008:        import junit.framework.TestCase;
009:
010:        public class JDateTimeMoreTest extends TestCase {
011:
012:            public void test1582() {
013:                JDateTime jdt1582 = new JDateTime(1582, 10, 4);
014:                assertEquals(4, jdt1582.getDayOfWeek());
015:                jdt1582.addDay(1);
016:                assertEquals(1582, jdt1582.getYear());
017:                assertEquals(10, jdt1582.getMonth());
018:                assertEquals(15, jdt1582.getDay());
019:                assertEquals(5, jdt1582.getDayOfWeek());
020:            }
021:
022:            public void testCompareToAndAdd() {
023:                JDateTime gt1 = new JDateTime();
024:                if (gt1.getDay() > 28) { // back and forth adds works without corrections
025:                    gt1.setDay(28); // for days that exists in all months
026:                }
027:
028:                // check for year 1582
029:                if (gt1.getMonth() == 10) {
030:                    if ((gt1.getDay() > 4) && ((gt1.getDay() < 15))) {
031:                        gt1.setDay(4);
032:                    }
033:                }
034:
035:                JDateTime gt2 = (JDateTime) gt1.clone();
036:                assertEquals(0, gt1.compareTo(gt2));
037:
038:                for (int i = 1; i < 1000; i++) {
039:                    gt2.add(i, 0, 0);
040:                    assertEquals(-1, gt1.compareTo(gt2));
041:                    gt2.addYear(-2 * i);
042:                    assertEquals(1, gt1.compareTo(gt2));
043:                    gt2.addYear(i);
044:                    assertEquals(0, gt1.compareTo(gt2));
045:                }
046:
047:                for (int i = 1; i < 60000; i++) {
048:                    gt2.add(0, i, 0);
049:                    assertEquals(-1, gt1.compareTo(gt2));
050:                    gt2.addMonth(-i);
051:                    assertEquals(0, gt1.compareTo(gt2));
052:                }
053:                for (int i = 1; i < 5000; i++) { // Year 1582, months moving
054:                    gt2.add(0, i, 0); // before it still doesn't work
055:                    assertEquals(-1, gt1.compareTo(gt2));
056:                    gt2.addMonth(-2 * i);
057:                    assertEquals(1, gt1.compareTo(gt2));
058:                    gt2.addMonth(i);
059:                    assertEquals(0, gt1.compareTo(gt2));
060:                }
061:
062:                for (int i = 1; i < 10000; i++) {
063:                    gt2.add(0, 0, i);
064:                    assertEquals(-1, gt1.compareTo(gt2));
065:                    gt2.addDay(-3 * i);
066:                    assertEquals(1, gt1.compareTo(gt2));
067:                    gt2.addDay(2 * i);
068:                    assertEquals(0, gt1.compareTo(gt2));
069:                }
070:
071:                for (int i = 1; i < 100000; i++) {
072:                    gt2.addTime(i, 0, 0);
073:                    assertEquals(-1, gt1.compareTo(gt2));
074:                    gt2.addHour(-4 * i);
075:                    assertEquals(1, gt1.compareTo(gt2));
076:                    gt2.addHour(3 * i);
077:                    assertEquals(0, gt1.compareTo(gt2));
078:                }
079:
080:                for (int i = 1; i < 100000; i++) {
081:                    gt2.addTime(0, i, 0);
082:                    assertEquals(-1, gt1.compareTo(gt2));
083:                    gt2.addMinute(-5 * i);
084:                    assertEquals(1, gt1.compareTo(gt2));
085:                    gt2.addMinute(4 * i);
086:                    assertEquals(0, gt1.compareTo(gt2));
087:                }
088:
089:                for (int i = 1; i < 1000000; i++) {
090:                    gt2.addTime(0, 0, i);
091:                    assertEquals(-1, gt1.compareTo(gt2));
092:                    gt2.addSecond(-7 * i);
093:                    assertEquals(1, gt1.compareTo(gt2));
094:                    gt2.addSecond(6 * i);
095:                    assertEquals(0, gt1.compareTo(gt2));
096:                }
097:
098:                for (int i = 1; i < 1000000; i++) {
099:                    gt2.addTime(0, 0, i / 1000.0);
100:                    assertEquals(-1, gt1.compareTo(gt2));
101:                    gt2.addMillisecond(-8 * i);
102:                    assertEquals(1, gt1.compareTo(gt2));
103:                    gt2.addMillisecond(7 * i);
104:                    assertEquals(0, gt1.compareTo(gt2));
105:                }
106:            }
107:
108:            public void testWeekOfYear() {
109:                JDateTime gt = new JDateTime();
110:                GregorianCalendar gc = new GregorianCalendar();
111:                int[] _fdiw = { 0, GregorianCalendar.MONDAY,
112:                        GregorianCalendar.TUESDAY, GregorianCalendar.WEDNESDAY,
113:                        GregorianCalendar.THURSDAY, GregorianCalendar.FRIDAY,
114:                        GregorianCalendar.SATURDAY, GregorianCalendar.SUNDAY };
115:
116:                // test all starting dates (first day in week)
117:                for (int fdiw = 1; fdiw <= 7; fdiw++) {
118:                    gc.setFirstDayOfWeek(_fdiw[fdiw]);
119:
120:                    // test all minimal days in first week
121:                    for (int min = 1; min <= 7; min++) {
122:                        gc.setMinimalDaysInFirstWeek(min);
123:                        gt.setWeekDefinitionAlt(fdiw, min);
124:
125:                        // test many years
126:                        for (int y = 1800; y < 3000; y++) {
127:
128:                            if (y == 1916) {
129:                                continue; // skip this year due to specific daylight savings
130:                            }
131:
132:                            gt.set(y, 1, 1);
133:                            gc.set(y, 0, 1);
134:
135:                            int total = gt.isLeapYear() ? 366 : 365;
136:
137:                            // test all days
138:                            for (int i = 0; i < total; i++) {
139:                                assertEquals(gc
140:                                        .get(GregorianCalendar.DAY_OF_MONTH),
141:                                        gt.getDay());
142:                                assertEquals(
143:                                        gc.get(GregorianCalendar.MONTH) + 1, gt
144:                                                .getMonth());
145:                                assertEquals(gc.get(GregorianCalendar.YEAR), gt
146:                                        .getYear());
147:                                assertEquals(gc
148:                                        .get(GregorianCalendar.DAY_OF_YEAR), gt
149:                                        .getDayOfYear());
150:                                int dow = gc.get(GregorianCalendar.DAY_OF_WEEK) - 1;
151:                                if (dow == 0) {
152:                                    dow = 7;
153:                                }
154:                                assertEquals(dow, gt.getDayOfWeek());
155:                                assertEquals(gc
156:                                        .get(GregorianCalendar.WEEK_OF_YEAR),
157:                                        gt.getWeekOfYear());
158:                                assertEquals(gc
159:                                        .get(GregorianCalendar.WEEK_OF_MONTH),
160:                                        gt.getWeekOfMonth());
161:
162:                                gt.addDay(1);
163:                                gc.roll(GregorianCalendar.DAY_OF_YEAR, true);
164:                            }
165:                        }
166:                    }
167:                }
168:            }
169:
170:            // ---------------------------------------------------------------- specific problems
171:
172:            public void testMillisProblems() {
173:                GregorianCalendar gc = new GregorianCalendar();
174:                JDateTime jdt = new JDateTime();
175:
176:                long now = 1183243766625l;
177:                gc.setTimeInMillis(now);
178:                jdt.setTimeInMillis(now);
179:                assertEquals(now, jdt.getTimeInMillis());
180:                assertEquals(now, jdt.getTimeInMillis());
181:
182:                JDateTime jdt2 = new JDateTime();
183:                jdt2.setTimeInMillis(jdt.getTimeInMillis());
184:                assertEquals(jdt.toString(), jdt2.toString());
185:                assertEquals(gc.getTimeInMillis(), jdt2.getTimeInMillis());
186:                assertEquals(now, jdt2.getTimeInMillis());
187:
188:                int year = 1970;
189:                jdt.setYear(year);
190:                jdt.setMillisecond(0);
191:                gc.set(Calendar.YEAR, year);
192:                gc.set(Calendar.MILLISECOND, 0);
193:                int month = 1;
194:
195:                jdt.setMonth(month);
196:                gc.set(Calendar.MONTH, month - 1);
197:                for (int sec = 0; sec < 60; sec++) {
198:                    jdt.setSecond(sec);
199:                    gc.set(Calendar.SECOND, sec);
200:                    assertEquals(gc.getTimeInMillis(), jdt.getTimeInMillis());
201:
202:                    jdt.setTimeInMillis(gc.getTimeInMillis());
203:                    gc.setTimeInMillis(gc.getTimeInMillis());
204:                    assertEquals(gc.getTimeInMillis(), jdt.getTimeInMillis());
205:                }
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.