Source Code Cross Referenced for DateUtils.java in  » ERP-CRM-Financial » Kuali-Financial-System » org » kuali » core » util » 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 » ERP CRM Financial » Kuali Financial System » org.kuali.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006 The Kuali Foundation.
003:         * 
004:         * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
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.kuali.core.util;
017:
018:        import java.sql.Timestamp;
019:        import java.util.Calendar;
020:        import java.util.Date;
021:        import java.util.GregorianCalendar;
022:
023:        /**
024:         * Utility methods for comparing dates
025:         * 
026:         * 
027:         */
028:        public class DateUtils extends org.apache.commons.lang.time.DateUtils {
029:            /**
030:             * Adds null-safety to commons.DateUtils isSameDay method.
031:             * 
032:             * @return true if both dates are null or represent the same day
033:             */
034:            public static boolean isSameDay(Date date1, Date date2) {
035:                boolean same = false;
036:
037:                if ((date1 == null) && (date2 == null)) {
038:                    same = true;
039:                } else if ((date1 != null) && (date2 != null)) {
040:                    return org.apache.commons.lang.time.DateUtils.isSameDay(
041:                            date1, date2);
042:                } else {
043:                    same = false;
044:                }
045:
046:                return same;
047:            }
048:
049:            /**
050:             * Adds null-safety to commons.DateUtils isSameDay method.
051:             * 
052:             * @return true if both calendars are null or represent the same day
053:             */
054:            public static boolean isSameDay(Calendar cal1, Calendar cal2) {
055:                boolean same = false;
056:
057:                if ((cal1 == null) && (cal2 == null)) {
058:                    same = true;
059:                } else if ((cal1 != null) && (cal2 != null)) {
060:                    return org.apache.commons.lang.time.DateUtils.isSameDay(
061:                            cal1, cal2);
062:                } else {
063:                    same = false;
064:                }
065:
066:                return same;
067:            }
068:
069:            /**
070:             * Converts the given java.util.Date into an equivalent java.sql.Date
071:             * 
072:             * @param date
073:             * @return java.sql.Date constructed from the given java.util.Date
074:             */
075:            public static java.sql.Date convertToSqlDate(java.util.Date date) {
076:                return new java.sql.Date(date.getTime());
077:            }
078:
079:            /**
080:             * Convert the given java.sql.date into a java.sql.date of which all the time fields are set to 0.
081:             * 
082:             * @param date
083:             * @return
084:             */
085:            public static java.sql.Date clearTimeFields(java.sql.Date date) {
086:                Calendar timelessCal = new GregorianCalendar();
087:                timelessCal.setTime(date);
088:                timelessCal.set(Calendar.HOUR_OF_DAY, 0);
089:                timelessCal.set(Calendar.MINUTE, 0);
090:                timelessCal.set(Calendar.SECOND, 0);
091:                timelessCal.set(Calendar.MILLISECOND, 0);
092:
093:                return new java.sql.Date(timelessCal.getTimeInMillis());
094:            }
095:
096:            /**
097:             * Convert the given java.util.date into a java.util.date of which all the time fields are set to 0.
098:             * 
099:             * @param date
100:             * @return
101:             */
102:            public static java.util.Date clearTimeFields(java.util.Date date) {
103:                Calendar timelessCal = new GregorianCalendar();
104:                timelessCal.setTime(date);
105:                timelessCal.set(Calendar.HOUR_OF_DAY, 0);
106:                timelessCal.set(Calendar.MINUTE, 0);
107:                timelessCal.set(Calendar.SECOND, 0);
108:                timelessCal.set(Calendar.MILLISECOND, 0);
109:
110:                return new java.util.Date(timelessCal.getTimeInMillis());
111:            }
112:
113:            /**
114:             * @param startDateTime
115:             * @param endDateTime
116:             * @return the difference in days between the second timestamp and first
117:             */
118:            public static double getDifferenceInDays(Timestamp startDateTime,
119:                    Timestamp endDateTime) {
120:                int difference = 0;
121:
122:                Calendar startCalendar = Calendar.getInstance();
123:                startCalendar.setTime(startDateTime);
124:
125:                Calendar endCalendar = Calendar.getInstance();
126:                endCalendar.setTime(endDateTime);
127:
128:                // First, get difference in whole days
129:                Calendar startCompare = Calendar.getInstance();
130:                startCompare.setTime(startDateTime);
131:                startCompare.set(Calendar.HOUR_OF_DAY, 0);
132:                startCompare.set(Calendar.MINUTE, 0);
133:                startCompare.set(Calendar.SECOND, 0);
134:                startCompare.set(Calendar.MILLISECOND, 0);
135:
136:                Calendar endCompare = Calendar.getInstance();
137:                endCompare.setTime(endDateTime);
138:                endCompare.set(Calendar.HOUR_OF_DAY, 0);
139:                endCompare.set(Calendar.MINUTE, 0);
140:                endCompare.set(Calendar.SECOND, 0);
141:                endCompare.set(Calendar.MILLISECOND, 0);
142:
143:                return (endCompare.getTimeInMillis() - startCompare
144:                        .getTimeInMillis())
145:                        / (24 * 60 * 60 * 1000);
146:            }
147:
148:            /**
149:             * @param startDateTime
150:             * @param endDateTime
151:             * @return the difference in hours between the second timestamp and first
152:             */
153:            public static double getDifferenceInHours(Timestamp startDateTime,
154:                    Timestamp endDateTime) {
155:                int difference = 0;
156:
157:                Calendar startCalendar = Calendar.getInstance();
158:                startCalendar.setTime(startDateTime);
159:
160:                Calendar endCalendar = Calendar.getInstance();
161:                endCalendar.setTime(endDateTime);
162:
163:                // First, get difference in whole days
164:                Calendar startCompare = Calendar.getInstance();
165:                startCompare.setTime(startDateTime);
166:                startCompare.set(Calendar.HOUR_OF_DAY, 0);
167:                startCompare.set(Calendar.MINUTE, 0);
168:
169:                Calendar endCompare = Calendar.getInstance();
170:                endCompare.setTime(endDateTime);
171:                endCompare.set(Calendar.HOUR_OF_DAY, 0);
172:                endCompare.set(Calendar.MINUTE, 0);
173:
174:                return (endCalendar.getTimeInMillis() - startCalendar
175:                        .getTimeInMillis())
176:                        / (60.0000 * 60.0000 * 1000.0000);
177:            }
178:
179:            /**
180:             * 
181:             * This method is a utility method to create a new java.sql.Date in one line.
182:             * 
183:             * @param year
184:             * @param month
185:             * @param day
186:             * 
187:             * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
188:             *         millisecond
189:             * 
190:             */
191:            public static java.sql.Date newDate(Integer year, Integer month,
192:                    Integer day) {
193:
194:                // test for null arguments
195:                if (year == null) {
196:                    throw new IllegalArgumentException(
197:                            "Argument 'year' passed in was null.");
198:                }
199:                if (month == null) {
200:                    throw new IllegalArgumentException(
201:                            "Argument 'month' passed in was null.");
202:                }
203:                if (day == null) {
204:                    throw new IllegalArgumentException(
205:                            "Argument 'day' passed in was null.");
206:                }
207:
208:                Calendar calendar = Calendar.getInstance();
209:                calendar.set(Calendar.YEAR, year);
210:                calendar.set(Calendar.MONTH, month);
211:                calendar.set(Calendar.DAY_OF_MONTH, day);
212:                calendar.clear(Calendar.HOUR_OF_DAY);
213:                calendar.clear(Calendar.MINUTE);
214:                calendar.clear(Calendar.SECOND);
215:                calendar.clear(Calendar.MILLISECOND);
216:
217:                return new java.sql.Date(calendar.getTimeInMillis());
218:            }
219:
220:            /**
221:             * 
222:             * This method is a utility method to create a new java.sql.Date in one line.
223:             * 
224:             * @param year
225:             * @param month
226:             * @param day
227:             * @param hour
228:             * @param minute
229:             * @param second
230:             * 
231:             * @return a populated java.sql.Date with the year, month, hour, minute, and second populated, with no value for millisecond.
232:             * 
233:             */
234:            public static java.sql.Date newDate(Integer year, Integer month,
235:                    Integer day, Integer hour, Integer minute, Integer second) {
236:
237:                // test for null arguments
238:                if (year == null) {
239:                    throw new IllegalArgumentException(
240:                            "Argument 'year' passed in was null.");
241:                }
242:                if (month == null) {
243:                    throw new IllegalArgumentException(
244:                            "Argument 'month' passed in was null.");
245:                }
246:                if (day == null) {
247:                    throw new IllegalArgumentException(
248:                            "Argument 'day' passed in was null.");
249:                }
250:                if (hour == null) {
251:                    throw new IllegalArgumentException(
252:                            "Argument 'hour' passed in was null.");
253:                }
254:                if (minute == null) {
255:                    throw new IllegalArgumentException(
256:                            "Argument 'minute' passed in was null.");
257:                }
258:                if (second == null) {
259:                    throw new IllegalArgumentException(
260:                            "Argument 'second' passed in was null.");
261:                }
262:
263:                Calendar calendar = Calendar.getInstance();
264:                calendar.set(Calendar.YEAR, year);
265:                calendar.set(Calendar.MONTH, month);
266:                calendar.set(Calendar.DAY_OF_MONTH, day);
267:                calendar.set(Calendar.HOUR_OF_DAY, hour);
268:                calendar.set(Calendar.MINUTE, minute);
269:                calendar.set(Calendar.SECOND, second);
270:                calendar.clear(Calendar.MILLISECOND);
271:
272:                return new java.sql.Date(calendar.getTimeInMillis());
273:            }
274:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.