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


001:        /*
002:         * Copyright 2005-2007 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.service.impl;
017:
018:        import java.sql.Timestamp;
019:        import java.text.DateFormat;
020:        import java.text.ParseException;
021:        import java.text.SimpleDateFormat;
022:        import java.util.Calendar;
023:        import java.util.Date;
024:
025:        import org.apache.commons.lang.StringUtils;
026:        import org.apache.commons.lang.time.DurationFormatUtils;
027:        import org.kuali.core.service.DateTimeService;
028:        import org.springframework.transaction.annotation.Transactional;
029:
030:        /**
031:         * This class is the service implementation for a DateTime structure. This is
032:         * the default, Kuali delivered implementation.
033:         */
034:        @Transactional
035:        public class DateTimeServiceImpl implements  DateTimeService {
036:            private String[] sqlDateFormats;
037:
038:            private String stringDateFormat;
039:
040:            private String stringDateTimeFormat;
041:
042:            /**
043:             * @see org.kuali.core.service.DateTimeService#toDateString(java.util.Date)
044:             */
045:            public String toDateString(Date date) {
046:                return toString(date, stringDateFormat);
047:            }
048:
049:            /**
050:             * @see org.kuali.core.service.DateTimeService#toDateTimeString(java.util.Date)
051:             */
052:            public String toDateTimeString(Date date) {
053:                return toString(date, stringDateTimeFormat);
054:            }
055:
056:            /**
057:             * @see org.kuali.core.service.DateTimeService#toString(java.util.Date,
058:             *      java.lang.String)
059:             */
060:            public String toString(Date date, String pattern) {
061:                DateFormat dateFormat = new SimpleDateFormat(pattern);
062:                dateFormat.setLenient(false);
063:                return dateFormat.format(date);
064:            }
065:
066:            /**
067:             * @see org.kuali.core.service.DateTimeService#getCurrentDate()
068:             */
069:            public Date getCurrentDate() {
070:                Calendar c = Calendar.getInstance();
071:                c.setTime(new Date());
072:                return c.getTime();
073:            }
074:
075:            /**
076:             * @see org.kuali.core.service.DateTimeService#getCurrentTimestamp()
077:             */
078:            public Timestamp getCurrentTimestamp() {
079:                return new java.sql.Timestamp(getCurrentDate().getTime());
080:            }
081:
082:            /**
083:             * @see org.kuali.core.service.DateTimeService#getCurrentSqlDate()
084:             */
085:            public java.sql.Date getCurrentSqlDate() {
086:                return new java.sql.Date(getCurrentDate().getTime());
087:            }
088:
089:            /**
090:             * @see org.kuali.core.service.DateTimeService#getCurrentSqlDateMidnight()
091:             */
092:            public java.sql.Date getCurrentSqlDateMidnight() {
093:                // simple and not unduely inefficient way to truncate the time component
094:                return java.sql.Date.valueOf(getCurrentSqlDate().toString());
095:            }
096:
097:            /**
098:             * @see org.kuali.core.service.DateTimeService#getCurrentCalendar()
099:             */
100:            public Calendar getCurrentCalendar() {
101:                return getCalendar(getCurrentDate());
102:            }
103:
104:            /**
105:             * @see org.kuali.core.service.DateTimeService#getCalendar
106:             */
107:            public Calendar getCalendar(Date date) {
108:                if (date == null) {
109:                    throw new IllegalArgumentException("invalid (null) date");
110:                }
111:
112:                Calendar currentCalendar = Calendar.getInstance();
113:                currentCalendar.setTime(date);
114:
115:                return currentCalendar;
116:            }
117:
118:            /**
119:             * @see org.kuali.core.service.DateTimeService#convertToDate(java.lang.String)
120:             */
121:            public Date convertToDate(String dateString) throws ParseException {
122:                return parse(dateString, stringDateFormat);
123:            }
124:
125:            /**
126:             * @see org.kuali.core.service.DateTimeService#convertToDateTime(java.lang.String)
127:             */
128:            public Date convertToDateTime(String dateTimeString)
129:                    throws ParseException {
130:                return parse(dateTimeString, stringDateTimeFormat);
131:            }
132:
133:            /**
134:             * @see org.kuali.core.service.DateTimeService#convertToSqlTimestamp(java.lang.String)
135:             */
136:            public java.sql.Timestamp convertToSqlTimestamp(String timeString)
137:                    throws ParseException {
138:                if (!StringUtils.isBlank(timeString)) {
139:                    return new java.sql.Timestamp(parse(timeString,
140:                            stringDateTimeFormat).getTime());
141:                }
142:                return null;
143:            }
144:
145:            /**
146:             * @see org.kuali.core.service.DateTimeService#convertToSqlDate(java.lang.String)
147:             */
148:            public java.sql.Date convertToSqlDate(String dateString)
149:                    throws ParseException {
150:                if (StringUtils.isBlank(dateString)) {
151:                    throw new IllegalArgumentException(
152:                            "invalid (blank) timeString");
153:                }
154:                dateString = dateString.trim();
155:                java.sql.Date date = null;
156:                StringBuffer exceptionMessage = new StringBuffer(
157:                        "Date string '")
158:                        .append(dateString)
159:                        .append(
160:                                "' could not be converted using any of the accepted formats: ");
161:                for (String dateFormatString : sqlDateFormats) {
162:                    try {
163:                        return new java.sql.Date(parse(dateString,
164:                                dateFormatString).getTime());
165:                    } catch (ParseException e) {
166:                        exceptionMessage.append(dateFormatString).append(
167:                                " (error offset=").append(e.getErrorOffset())
168:                                .append("),");
169:                    }
170:                }
171:                if (date == null) {
172:                    throw new ParseException(exceptionMessage.toString()
173:                            .substring(0, exceptionMessage.length() - 1), 0);
174:                }
175:                return date;
176:            }
177:
178:            /**
179:             * @throws ParseException
180:             * @see org.kuali.core.service.DateTimeService#convertToSqlDate(java.sql.Timestamp)
181:             */
182:            public java.sql.Date convertToSqlDate(Timestamp timestamp)
183:                    throws ParseException {
184:                return new java.sql.Date(timestamp.getTime());
185:            }
186:
187:            public int dateDiff(Date startDate, Date endDate, boolean inclusive) {
188:                Calendar startDateCalendar = Calendar.getInstance();
189:                startDateCalendar.setTime(startDate);
190:
191:                Calendar endDateCalendar = Calendar.getInstance();
192:                endDateCalendar.setTime(endDate);
193:
194:                int startDateOffset = -(startDateCalendar
195:                        .get(Calendar.ZONE_OFFSET) + startDateCalendar
196:                        .get(Calendar.DST_OFFSET))
197:                        / (60 * 1000);
198:
199:                int endDateOffset = -(endDateCalendar.get(Calendar.ZONE_OFFSET) + endDateCalendar
200:                        .get(Calendar.DST_OFFSET))
201:                        / (60 * 1000);
202:
203:                if (startDateOffset > endDateOffset) {
204:                    startDateCalendar.add(Calendar.MINUTE, endDateOffset
205:                            - startDateOffset);
206:                }
207:
208:                if (inclusive) {
209:                    startDateCalendar.add(Calendar.DATE, -1);
210:                }
211:
212:                int dateDiff = Integer.parseInt(DurationFormatUtils
213:                        .formatDuration(endDateCalendar.getTimeInMillis()
214:                                - startDateCalendar.getTimeInMillis(), "d",
215:                                true));
216:
217:                return dateDiff;
218:            }
219:
220:            private Date parse(String dateString, String pattern)
221:                    throws ParseException {
222:                if (!StringUtils.isBlank(dateString)) {
223:                    DateFormat dateFormat = new SimpleDateFormat(pattern);
224:                    dateFormat.setLenient(false);
225:                    return dateFormat.parse(dateString);
226:                }
227:                return null;
228:            }
229:
230:            /**
231:             * @param sqlDateFormats
232:             *            the sqlDateFormats to set
233:             */
234:            public void setSqlDateFormats(String[] sqlDateFormats) {
235:                this .sqlDateFormats = sqlDateFormats;
236:            }
237:
238:            /**
239:             * @param stringDateFormat
240:             *            the stringDateFormat to set
241:             */
242:            public void setStringDateFormat(String stringDateFormat) {
243:                this .stringDateFormat = stringDateFormat;
244:            }
245:
246:            /**
247:             * @param stringDateTimeFormat
248:             *            the stringDateTimeFormat to set
249:             */
250:            public void setStringDateTimeFormat(String stringDateTimeFormat) {
251:                this.stringDateTimeFormat = stringDateTimeFormat;
252:            }
253:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.