Source Code Cross Referenced for UtilDateTime.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » base » 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 » SourceTap CRM » org.ofbiz.base.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: UtilDateTime.java,v 1.1 2003/08/15 20:23:20 ajzeneski Exp $
003:         *
004:         *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.base.util;
025:
026:        import java.text.SimpleDateFormat;
027:        import java.util.Calendar;
028:        import java.util.Date;
029:
030:        /**
031:         * Utility class for handling java.util.Date, the java.sql data/time classes and related
032:         *
033:         * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
034:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
035:         * @author     <a href="mailto:johan@ibibi.com">Johan Isacsson</a>
036:         * @version    $Revision: 1.1 $
037:         * @since      2.0
038:         */
039:        public class UtilDateTime {
040:
041:            /** Return a Timestamp for right now
042:             * @return Timestamp for right now
043:             */
044:            public static java.sql.Timestamp nowTimestamp() {
045:                return new java.sql.Timestamp(System.currentTimeMillis());
046:            }
047:
048:            /** 
049:             * Return a string formatted as yyyyMMddHHmmss      
050:             * @return String formatted for right now
051:             */
052:            public static String nowDateString() {
053:                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
054:                return df.format(new Date());
055:            }
056:
057:            /** Return a Date for right now
058:             * @return Date for right now
059:             */
060:            public static java.util.Date nowDate() {
061:                return new java.util.Date();
062:            }
063:
064:            public static java.sql.Timestamp getDayStart(
065:                    java.sql.Timestamp stamp) {
066:                return getDayStart(stamp, 0);
067:            }
068:
069:            public static java.sql.Timestamp getDayStart(
070:                    java.sql.Timestamp stamp, int daysLater) {
071:                Calendar tempCal = Calendar.getInstance();
072:                tempCal.setTime(new java.util.Date(stamp.getTime()));
073:                tempCal.set(tempCal.get(Calendar.YEAR), tempCal
074:                        .get(Calendar.MONTH), tempCal
075:                        .get(Calendar.DAY_OF_MONTH), 0, 0, 0);
076:                tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
077:                java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal
078:                        .getTime().getTime());
079:                retStamp.setNanos(0);
080:                return retStamp;
081:            }
082:
083:            public static java.sql.Timestamp getNextDayStart(
084:                    java.sql.Timestamp stamp) {
085:                return getDayStart(stamp, 1);
086:            }
087:
088:            public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp) {
089:                return getDayEnd(stamp, 0);
090:            }
091:
092:            public static java.sql.Timestamp getDayEnd(
093:                    java.sql.Timestamp stamp, int daysLater) {
094:                Calendar tempCal = Calendar.getInstance();
095:                tempCal.setTime(new java.util.Date(stamp.getTime()));
096:                tempCal.set(tempCal.get(Calendar.YEAR), tempCal
097:                        .get(Calendar.MONTH), tempCal
098:                        .get(Calendar.DAY_OF_MONTH), 23, 59, 59);
099:                tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
100:                java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal
101:                        .getTime().getTime());
102:                retStamp.setNanos(999999999);
103:                return retStamp;
104:            }
105:
106:            /**
107:             * Return the date for the first day of the month
108:             * @param stamp
109:             * @return
110:             */
111:            public static java.sql.Timestamp getMonthStart(
112:                    java.sql.Timestamp stamp) {
113:                return getMonthStart(stamp, 0);
114:            }
115:
116:            public static java.sql.Timestamp getMonthStart(
117:                    java.sql.Timestamp stamp, int daysLater) {
118:                Calendar tempCal = Calendar.getInstance();
119:                tempCal.setTime(new java.util.Date(stamp.getTime()));
120:                tempCal.set(tempCal.get(Calendar.YEAR), tempCal
121:                        .get(Calendar.MONTH), 1, 0, 0, 0);
122:                tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
123:                java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal
124:                        .getTime().getTime());
125:                retStamp.setNanos(0);
126:                return retStamp;
127:            }
128:
129:            /**
130:             * Return the date for the first day of the week
131:             * @param stamp
132:             * @return
133:             */
134:            public static java.sql.Timestamp getWeekStart(
135:                    java.sql.Timestamp stamp) {
136:                return getWeekStart(stamp, 0);
137:            }
138:
139:            public static java.sql.Timestamp getWeekStart(
140:                    java.sql.Timestamp stamp, int daysLater) {
141:                Calendar tempCal = Calendar.getInstance();
142:                tempCal.setTime(new java.util.Date(stamp.getTime()));
143:                tempCal.set(tempCal.get(Calendar.YEAR), tempCal
144:                        .get(Calendar.MONTH), tempCal
145:                        .get(Calendar.DAY_OF_MONTH), 0, 0, 0);
146:                tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
147:                tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
148:                java.sql.Timestamp retStamp = new java.sql.Timestamp(tempCal
149:                        .getTime().getTime());
150:                retStamp.setNanos(0);
151:                return retStamp;
152:            }
153:
154:            /** Converts a date String into a java.sql.Date
155:             * @param date The date String: MM/DD/YYYY
156:             * @return A java.sql.Date made from the date String
157:             */
158:            public static java.sql.Date toSqlDate(String date) {
159:                java.util.Date newDate = toDate(date, "00:00:00");
160:
161:                if (newDate != null)
162:                    return new java.sql.Date(newDate.getTime());
163:                else
164:                    return null;
165:            }
166:
167:            /** Makes a java.sql.Date from separate Strings for month, day, year
168:             * @param monthStr The month String
169:             * @param dayStr The day String
170:             * @param yearStr The year String
171:             * @return A java.sql.Date made from separate Strings for month, day, year
172:             */
173:            public static java.sql.Date toSqlDate(String monthStr,
174:                    String dayStr, String yearStr) {
175:                java.util.Date newDate = toDate(monthStr, dayStr, yearStr, "0",
176:                        "0", "0");
177:
178:                if (newDate != null)
179:                    return new java.sql.Date(newDate.getTime());
180:                else
181:                    return null;
182:            }
183:
184:            /** Makes a java.sql.Date from separate ints for month, day, year
185:             * @param month The month int
186:             * @param day The day int
187:             * @param year The year int
188:             * @return A java.sql.Date made from separate ints for month, day, year
189:             */
190:            public static java.sql.Date toSqlDate(int month, int day, int year) {
191:                java.util.Date newDate = toDate(month, day, year, 0, 0, 0);
192:
193:                if (newDate != null)
194:                    return new java.sql.Date(newDate.getTime());
195:                else
196:                    return null;
197:            }
198:
199:            /** Converts a time String into a java.sql.Time
200:             * @param time The time String: either HH:MM or HH:MM:SS
201:             * @return A java.sql.Time made from the time String
202:             */
203:            public static java.sql.Time toSqlTime(String time) {
204:                java.util.Date newDate = toDate("1/1/1970", time);
205:
206:                if (newDate != null)
207:                    return new java.sql.Time(newDate.getTime());
208:                else
209:                    return null;
210:            }
211:
212:            /** Makes a java.sql.Time from separate Strings for hour, minute, and second.
213:             * @param hourStr The hour String
214:             * @param minuteStr The minute String
215:             * @param secondStr The second String
216:             * @return A java.sql.Time made from separate Strings for hour, minute, and second.
217:             */
218:            public static java.sql.Time toSqlTime(String hourStr,
219:                    String minuteStr, String secondStr) {
220:                java.util.Date newDate = toDate("0", "0", "0", hourStr,
221:                        minuteStr, secondStr);
222:
223:                if (newDate != null)
224:                    return new java.sql.Time(newDate.getTime());
225:                else
226:                    return null;
227:            }
228:
229:            /** Makes a java.sql.Time from separate ints for hour, minute, and second.
230:             * @param hour The hour int
231:             * @param minute The minute int
232:             * @param second The second int
233:             * @return A java.sql.Time made from separate ints for hour, minute, and second.
234:             */
235:            public static java.sql.Time toSqlTime(int hour, int minute,
236:                    int second) {
237:                java.util.Date newDate = toDate(0, 0, 0, hour, minute, second);
238:
239:                if (newDate != null)
240:                    return new java.sql.Time(newDate.getTime());
241:                else
242:                    return null;
243:            }
244:
245:            /** Converts a date and time String into a Timestamp
246:             * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
247:             * @return The corresponding Timestamp
248:             */
249:            public static java.sql.Timestamp toTimestamp(String dateTime) {
250:                java.util.Date newDate = toDate(dateTime);
251:
252:                if (newDate != null)
253:                    return new java.sql.Timestamp(newDate.getTime());
254:                else
255:                    return null;
256:            }
257:
258:            /** Converts a date String and a time String into a Timestamp
259:             * @param date The date String: MM/DD/YYYY
260:             * @param time The time String: either HH:MM or HH:MM:SS
261:             * @return A Timestamp made from the date and time Strings
262:             */
263:            public static java.sql.Timestamp toTimestamp(String date,
264:                    String time) {
265:                java.util.Date newDate = toDate(date, time);
266:
267:                if (newDate != null)
268:                    return new java.sql.Timestamp(newDate.getTime());
269:                else
270:                    return null;
271:            }
272:
273:            /** Makes a Timestamp from separate Strings for month, day, year, hour, minute, and second.
274:             * @param monthStr The month String
275:             * @param dayStr The day String
276:             * @param yearStr The year String
277:             * @param hourStr The hour String
278:             * @param minuteStr The minute String
279:             * @param secondStr The second String
280:             * @return A Timestamp made from separate Strings for month, day, year, hour, minute, and second.
281:             */
282:            public static java.sql.Timestamp toTimestamp(String monthStr,
283:                    String dayStr, String yearStr, String hourStr,
284:                    String minuteStr, String secondStr) {
285:                java.util.Date newDate = toDate(monthStr, dayStr, yearStr,
286:                        hourStr, minuteStr, secondStr);
287:
288:                if (newDate != null)
289:                    return new java.sql.Timestamp(newDate.getTime());
290:                else
291:                    return null;
292:            }
293:
294:            /** Makes a Timestamp from separate ints for month, day, year, hour, minute, and second.
295:             * @param month The month int
296:             * @param day The day int
297:             * @param year The year int
298:             * @param hour The hour int
299:             * @param minute The minute int
300:             * @param second The second int
301:             * @return A Timestamp made from separate ints for month, day, year, hour, minute, and second.
302:             */
303:            public static java.sql.Timestamp toTimestamp(int month, int day,
304:                    int year, int hour, int minute, int second) {
305:                java.util.Date newDate = toDate(month, day, year, hour, minute,
306:                        second);
307:
308:                if (newDate != null)
309:                    return new java.sql.Timestamp(newDate.getTime());
310:                else
311:                    return null;
312:            }
313:
314:            /** Converts a date and time String into a Date
315:             * @param dateTime A combined data and time string in the format "MM/DD/YYYY HH:MM:SS", the seconds are optional
316:             * @return The corresponding Date
317:             */
318:            public static java.util.Date toDate(String dateTime) {
319:                // dateTime must have one space between the date and time...
320:                String date = dateTime.substring(0, dateTime.indexOf(" "));
321:                String time = dateTime.substring(dateTime.indexOf(" ") + 1);
322:
323:                return toDate(date, time);
324:            }
325:
326:            /** Converts a date String and a time String into a Date
327:             * @param date The date String: MM/DD/YYYY
328:             * @param time The time String: either HH:MM or HH:MM:SS
329:             * @return A Date made from the date and time Strings
330:             */
331:            public static java.util.Date toDate(String date, String time) {
332:                if (date == null || time == null)
333:                    return null;
334:                String month;
335:                String day;
336:                String year;
337:                String hour;
338:                String minute;
339:                String second;
340:
341:                int dateSlash1 = date.indexOf("/");
342:                int dateSlash2 = date.lastIndexOf("/");
343:
344:                if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
345:                    return null;
346:                int timeColon1 = time.indexOf(":");
347:                int timeColon2 = time.lastIndexOf(":");
348:
349:                if (timeColon1 <= 0)
350:                    return null;
351:                month = date.substring(0, dateSlash1);
352:                day = date.substring(dateSlash1 + 1, dateSlash2);
353:                year = date.substring(dateSlash2 + 1);
354:                hour = time.substring(0, timeColon1);
355:
356:                if (timeColon1 == timeColon2) {
357:                    minute = time.substring(timeColon1 + 1);
358:                    second = "0";
359:                } else {
360:                    minute = time.substring(timeColon1 + 1, timeColon2);
361:                    second = time.substring(timeColon2 + 1);
362:                }
363:
364:                return toDate(month, day, year, hour, minute, second);
365:            }
366:
367:            /** Makes a Date from separate Strings for month, day, year, hour, minute, and second.
368:             * @param monthStr The month String
369:             * @param dayStr The day String
370:             * @param yearStr The year String
371:             * @param hourStr The hour String
372:             * @param minuteStr The minute String
373:             * @param secondStr The second String
374:             * @return A Date made from separate Strings for month, day, year, hour, minute, and second.
375:             */
376:            public static java.util.Date toDate(String monthStr, String dayStr,
377:                    String yearStr, String hourStr, String minuteStr,
378:                    String secondStr) {
379:                int month, day, year, hour, minute, second;
380:
381:                try {
382:                    month = Integer.parseInt(monthStr);
383:                    day = Integer.parseInt(dayStr);
384:                    year = Integer.parseInt(yearStr);
385:                    hour = Integer.parseInt(hourStr);
386:                    minute = Integer.parseInt(minuteStr);
387:                    second = Integer.parseInt(secondStr);
388:                } catch (Exception e) {
389:                    return null;
390:                }
391:                return toDate(month, day, year, hour, minute, second);
392:            }
393:
394:            /** Makes a Date from separate ints for month, day, year, hour, minute, and second.
395:             * @param month The month int
396:             * @param day The day int
397:             * @param year The year int
398:             * @param hour The hour int
399:             * @param minute The minute int
400:             * @param second The second int
401:             * @return A Date made from separate ints for month, day, year, hour, minute, and second.
402:             */
403:            public static java.util.Date toDate(int month, int day, int year,
404:                    int hour, int minute, int second) {
405:                Calendar calendar = Calendar.getInstance();
406:
407:                try {
408:                    calendar.set(year, month - 1, day, hour, minute, second);
409:                } catch (Exception e) {
410:                    return null;
411:                }
412:                return new java.util.Date(calendar.getTime().getTime());
413:            }
414:
415:            /** Makes a date String in the format MM/DD/YYYY from a Date
416:             * @param date The Date
417:             * @return A date String in the format MM/DD/YYYY
418:             */
419:            public static String toDateString(java.util.Date date) {
420:                if (date == null)
421:                    return "";
422:                Calendar calendar = Calendar.getInstance();
423:
424:                calendar.setTime(date);
425:                int month = calendar.get(Calendar.MONTH) + 1;
426:                int day = calendar.get(Calendar.DAY_OF_MONTH);
427:                int year = calendar.get(Calendar.YEAR);
428:                String monthStr;
429:                String dayStr;
430:                String yearStr;
431:
432:                if (month < 10) {
433:                    monthStr = "0" + month;
434:                } else {
435:                    monthStr = "" + month;
436:                }
437:                if (day < 10) {
438:                    dayStr = "0" + day;
439:                } else {
440:                    dayStr = "" + day;
441:                }
442:                yearStr = "" + year;
443:                return monthStr + "/" + dayStr + "/" + yearStr;
444:            }
445:
446:            /** Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.
447:             * @param date The Date
448:             * @return A time String in the format HH:MM:SS or HH:MM
449:             */
450:            public static String toTimeString(java.util.Date date) {
451:                if (date == null)
452:                    return "";
453:                Calendar calendar = Calendar.getInstance();
454:
455:                calendar.setTime(date);
456:                return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY),
457:                        calendar.get(Calendar.MINUTE), calendar
458:                                .get(Calendar.SECOND)));
459:            }
460:
461:            /** Makes a time String in the format HH:MM:SS from a separate ints for hour, minute, and second. If the seconds are 0, then the output is in HH:MM.
462:             * @param hour The hour int
463:             * @param minute The minute int
464:             * @param second The second int
465:             * @return A time String in the format HH:MM:SS or HH:MM
466:             */
467:            public static String toTimeString(int hour, int minute, int second) {
468:                String hourStr;
469:                String minuteStr;
470:                String secondStr;
471:
472:                if (hour < 10) {
473:                    hourStr = "0" + hour;
474:                } else {
475:                    hourStr = "" + hour;
476:                }
477:                if (minute < 10) {
478:                    minuteStr = "0" + minute;
479:                } else {
480:                    minuteStr = "" + minute;
481:                }
482:                if (second < 10) {
483:                    secondStr = "0" + second;
484:                } else {
485:                    secondStr = "" + second;
486:                }
487:                if (second == 0)
488:                    return hourStr + ":" + minuteStr;
489:                else
490:                    return hourStr + ":" + minuteStr + ":" + secondStr;
491:            }
492:
493:            /** Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date. If the seconds are 0 they are left off.
494:             * @param date The Date
495:             * @return A combined data and time string in the format "MM/DD/YYYY HH:MM:SS" where the seconds are left off if they are 0.
496:             */
497:            public static String toDateTimeString(java.util.Date date) {
498:                if (date == null)
499:                    return "";
500:                String dateString = toDateString(date);
501:                String timeString = toTimeString(date);
502:
503:                if (dateString != null && timeString != null)
504:                    return dateString + " " + timeString;
505:                else
506:                    return "";
507:            }
508:
509:            /** Makes a Timestamp for the beginning of the month
510:             * @return A Timestamp of the beginning of the month
511:             */
512:            public static java.sql.Timestamp monthBegin() {
513:                Calendar mth = Calendar.getInstance();
514:
515:                mth.set(Calendar.DAY_OF_MONTH, 1);
516:                mth.set(Calendar.HOUR_OF_DAY, 0);
517:                mth.set(Calendar.MINUTE, 0);
518:                mth.set(Calendar.SECOND, 0);
519:                mth.set(Calendar.AM_PM, Calendar.AM);
520:                return new java.sql.Timestamp(mth.getTime().getTime());
521:            }
522:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.