Source Code Cross Referenced for DateHelper.java in  » Content-Management-System » hippo-cms » nl » hippo » 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 » Content Management System » hippo cms » nl.hippo.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Hippo (www.hippo.nl)
003:         *
004:         * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 nl.hippo.util;
017:
018:        import java.text.DateFormat;
019:        import java.text.ParseException;
020:        import java.text.SimpleDateFormat;
021:        import java.util.Calendar;
022:        import java.util.Date;
023:        import java.util.GregorianCalendar;
024:        import java.util.Locale;
025:
026:        public class DateHelper {
027:            public static int oneWeekAgo() {
028:                GregorianCalendar date = new GregorianCalendar(); // today
029:                date.add(Calendar.DATE, -7); // one week ago
030:                int fromDate = dateAsYYYYMMDD(date);
031:                //System.out.println("DateHelper: oneWeekAgo: " + fromDate);
032:                return fromDate;
033:            }
034:
035:            public static String timeOffset(String format, int years,
036:                    int months, int days) {
037:                GregorianCalendar date = new GregorianCalendar(); // today
038:                if (years != 0)
039:                    date.add(Calendar.YEAR, years);
040:                if (months != 0)
041:                    date.add(Calendar.MONTH, months);
042:                if (days != 0)
043:                    date.add(Calendar.DATE, days);
044:
045:                SimpleDateFormat sdf = new SimpleDateFormat(format);
046:
047:                return sdf.format(date.getTime());
048:            }
049:
050:            public static int twoWeeksAgo() {
051:                GregorianCalendar date = new GregorianCalendar(); // today
052:                date.add(Calendar.DATE, -14); // two weeks ago
053:                int fromDate = dateAsYYYYMMDD(date);
054:                //System.out.println("DateHelper: twoWeeksAgo: " + fromDate);
055:                return fromDate;
056:            }
057:
058:            public static int oneMonthAgo() {
059:                GregorianCalendar date = new GregorianCalendar(); // today
060:                date.add(Calendar.MONTH, -1); // one month ago
061:                int fromDate = dateAsYYYYMMDD(date);
062:                //System.out.println("DateHelper: oneMonthAgo: " + fromDate);
063:                return fromDate;
064:            }
065:
066:            public static int twoMonthsAgo() {
067:                GregorianCalendar date = new GregorianCalendar(); // today
068:                date.add(Calendar.MONTH, -2); // two months ago
069:                int fromDate = dateAsYYYYMMDD(date);
070:                return fromDate;
071:            }
072:
073:            public static int sixMonthsAgo() {
074:                GregorianCalendar date = new GregorianCalendar(); // today
075:                date.add(Calendar.MONTH, -6);
076:                int fromDate = dateAsYYYYMMDD(date);
077:                return fromDate;
078:            }
079:
080:            public static int oneYearAgo() {
081:                GregorianCalendar date = new GregorianCalendar(); // today
082:                date.add(Calendar.YEAR, -1);
083:                int fromDate = dateAsYYYYMMDD(date);
084:                return fromDate;
085:            }
086:
087:            public static int dateAsYYYYMMDD(GregorianCalendar date) {
088:                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
089:                String dateString = dateFormat.format(date.getTime());
090:                return Integer.parseInt(dateString);
091:            }
092:
093:            public static int compareDates(String date1, String format1,
094:                    String date2, String format2) throws ParseException {
095:                DateFormat firstDateFormat;
096:                if (isStringNullOrEmpty(format1)) {
097:                    firstDateFormat = createIsoDateFormat();
098:                } else {
099:                    firstDateFormat = new SimpleDateFormat(format1);
100:                }
101:
102:                DateFormat secondDateFormat;
103:                if (isStringNullOrEmpty(format2)) {
104:                    secondDateFormat = createIsoDateFormat();
105:                } else {
106:                    secondDateFormat = new SimpleDateFormat(format2);
107:                }
108:                Date firstDate = firstDateFormat.parse(date1);
109:                Date secondDate = secondDateFormat.parse(date2);
110:
111:                return firstDate.compareTo(secondDate);
112:            }
113:
114:            /**
115:             * @param string
116:             * @return
117:             */
118:            private static boolean isStringNullOrEmpty(String string) {
119:                return string == null || string.equals("");
120:            }
121:
122:            private static DateFormat createIsoDateFormat() {
123:                DateFormat result;
124:                result = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",
125:                        Locale.ENGLISH);
126:                return result;
127:            }
128:
129:            public static String reformatDate(String date,
130:                    String sourcePattern, String pattern) throws ParseException {
131:                DateFormat df = new SimpleDateFormat(sourcePattern,
132:                        Locale.ENGLISH);
133:                DateFormat df2 = new SimpleDateFormat(pattern);
134:                String reformattedDate = null;
135:                Date tempDate = df.parse(date);
136:                reformattedDate = df2.format(tempDate);
137:                return reformattedDate;
138:            }
139:
140:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.