Source Code Cross Referenced for DateResolver.java in  » Database-Client » SQLMinus » 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 » Database Client » SQLMinus » util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Author: rahul_kumar $
003:         * $Id: DateResolver.java,v 1.1 2004/01/03 07:47:33 rahul_kumar Exp rahul $
004:         * This is free software, as software should be; you can redistribute 
005:         * it and/or modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2.1 of the License, or (at your option) any later version.
008:
009:         * See LICENSE.txt for the full license covering this software/program/code.
010:         */
011:        package util;
012:
013:        import java.util.*;
014:        import java.text.*;
015:        import util.ArrayUtil;
016:
017:        /**
018:         * Does date resolution for single and ranges of dates as per our
019:         * format for filtering.
020:         * No, it doesnt yet take in full dates.
021:         * @author rahul kumar <rahul_kumar@yahoo.com>
022:         * @see XXX
023:         */
024:
025:        public class DateResolver {
026:
027:            /** ctor/constructor.
028:             */
029:            public DateResolver() {
030:            }
031:
032:            public static void main(String args[]) {
033:                System.out.println(formatDate("d"));
034:                System.out.println(formatDate("m"));
035:                System.out.println(formatDate("y"));
036:                System.out.println(formatDate("d-1"));
037:                System.out.println(formatDate("d+1"));
038:                System.out.println(formatDate("m+1"));
039:                System.out.println(formatDate("y+1"));
040:                System.out.println(formatDate(":d+1"));
041:                System.out.println(formatDate("d+1:"));
042:                System.out.println(formatDate("d-1:d+1"));
043:                System.out.println(formatDate("m-1:m+1"));
044:                System.out.println(formatDate("y-1:y+1"));
045:                System.out.println(formatDate(":y+1"));
046:                System.out.println(formatDate("y+1:"));
047:                System.out.println("\n parseToDateString()");
048:                System.out.println(parseToDateString("d"));
049:                System.out.println(parseToDateString("m"));
050:                System.out.println(parseToDateString("y"));
051:                System.out.println(parseToDateString("d-1"));
052:                System.out.println(parseToDateString("d+1"));
053:                System.out.println(parseToDateString("m+1"));
054:                System.out.println(parseToDateString("y+1"));
055:
056:            }
057:
058:            /**
059:             *  valid formats for date
060:             *  [:][dmy][+-](\d+)?[:]
061:             *  d, m , y
062:             *  d+1
063:             *  m+1 next month
064:             *  y+1 next year
065:             *  :d+1
066:             *  d+1:
067:             *  :y-1
068:             * 
069:             *  
070:             */
071:            public static String formatDate(String s) {
072:                // check for single strings
073:                // split with :. if none forget, else make ranges
074:                s = s.trim();
075:                //System.out.print(  "==GOT:"+ s + "  ");
076:                // shortcut for today
077:                if (s.equals("d")) {
078:                    return NextDateString(Calendar.DATE, 0);
079:                } else if (s.equals("m")) {
080:                    // shortcut for this month
081:                    String start = StartDateString(Calendar.MONTH, 0);
082:                    String end = EndDateString(Calendar.MONTH, 0);
083:                    return start + ":" + end;
084:                } else if (s.equals("y")) {
085:                    // shortcut for this year
086:                    String start = StartDateString(Calendar.YEAR, 0);
087:                    String end = EndDateString(Calendar.YEAR, 0);
088:                    return start + ":" + end;
089:                }
090:
091:                boolean range = false;
092:                if (s.indexOf(':') > -1)
093:                    range = true;
094:                if (!range) {
095:                    // single code passed
096:                    String ret[] = parseToDateRange(s);
097:                    if (ret[1] == null)
098:                        return ret[0];
099:                    return ret[0] + ":" + ret[1];
100:                }
101:                String p[] = ArrayUtil.split(s, ':');
102:                String start = "";
103:                String end = "";
104:                if (p[0] != null && p[0].length() > 0)
105:                    start = parseToDateString(p[0]);
106:                if (p.length == 2)
107:                    if (p[1] != null && p[1].length() > 0)
108:                        end = parseToDateString(p[1]);
109:                return start + ":" + end;
110:            }
111:
112:            public static String[] parseToDateRange(String dcode) {
113:                // now that this is called externally too, i need a sanity check
114:                //System.out.println(  "inside parseToDateRange:"+dcode+"]");
115:                if (dcode.length() == 1)
116:                    dcode = dcode + "+0";
117:
118:                String speriod;
119:                if (dcode.charAt(1) == '+')
120:                    speriod = dcode.substring(2);
121:                else
122:                    speriod = dcode.substring(1);
123:                int period = Integer.parseInt(speriod);
124:                char sunit = dcode.charAt(0);
125:                int unit = toUnit(sunit);
126:                //System.out.println(  speriod +":"+sunit+":"+unit+":"+"");
127:                String ret[] = new String[2];
128:                ret[0] = StartDateString(unit, period);
129:                if (unit != Calendar.DATE)
130:                    ret[1] = EndDateString(unit, period);
131:                //System.out.println(  ret[0]+" : "+ret[1]);
132:
133:                return ret;
134:            }
135:
136:            /** takes a user entered code such as "m+1" or "y-1" or "d+1" and
137:             * returns the date string for that.
138:             */
139:            public static String parseToDateString(String dcode) {
140:                //System.out.println(  "inside parseToDateString:"+dcode);
141:                String speriod;
142:                //treat a shortcut
143:                if (dcode.equals("="))
144:                    dcode = "d";
145:                // dirty but will work for now
146:                if (dcode.length() == 1)
147:                    dcode = dcode + "+0";
148:                // hack since parse fails if a + is encountered
149:                if (dcode.charAt(1) == '+')
150:                    speriod = dcode.substring(2);
151:                else
152:                    speriod = dcode.substring(1);
153:                // how many months, years, days to add
154:                int period = Integer.parseInt(speriod);
155:                char sunit = dcode.charAt(0);
156:                int unit = toUnit(sunit);
157:                return NextDateString(unit, period);
158:            }
159:
160:            public static int toUnit(char c) {
161:                if (c == 'm')
162:                    return Calendar.MONTH;
163:                if (c == 'y')
164:                    return Calendar.YEAR;
165:                if (c == 'd')
166:                    return Calendar.DATE;
167:                return Calendar.DATE;
168:            }
169:
170:            public static String NextDateString(int unit, int period) {
171:                java.util.Date mydate = new java.util.Date();
172:                Calendar cal = Calendar.getInstance();
173:                cal.setTime(mydate);
174:                cal.add(unit, period);
175:                mydate = cal.getTime();
176:                SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd");
177:                String dateString = sdf.format(mydate);
178:                return dateString;
179:            }
180:
181:            public static String StartDateString(int unit, int period) {
182:                java.util.Date mydate = new java.util.Date();
183:                Calendar cal = Calendar.getInstance();
184:                cal.setTime(mydate);
185:                cal.add(unit, period);
186:                if (unit == Calendar.YEAR)
187:                    cal.set(Calendar.MONTH, 0);
188:                if (unit == Calendar.YEAR || unit == Calendar.MONTH)
189:                    cal.set(Calendar.DATE, 1);
190:
191:                mydate = cal.getTime();
192:                SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd");
193:                String dateString = sdf.format(mydate);
194:                return dateString;
195:            }
196:
197:            public static String EndDateString(int unit, int period) {
198:                java.util.Date mydate = new java.util.Date();
199:                Calendar cal = Calendar.getInstance();
200:                cal.setTime(mydate);
201:                cal.add(unit, period);
202:                if (unit == Calendar.YEAR) {
203:                    cal.add(Calendar.YEAR, 1);
204:                    cal.set(Calendar.MONTH, 0);
205:                } else {
206:                    cal.add(Calendar.MONTH, 1);
207:                }
208:                cal.set(Calendar.DATE, 1);
209:
210:                mydate = cal.getTime();
211:                SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd");
212:                String dateString = sdf.format(mydate);
213:                return dateString;
214:            }
215:
216:            public static String getDBString(String s, Map htParams) {
217:                return htParams.get("fs_date") + s + htParams.get("fe_date");
218:            }
219:
220:            ////// START INSTANCE VARIABLES //////
221:
222:            ////// START CONSTANTS AND CLASS LEVEL VARIABLES  //////
223:            static final String P = "DateResolver"; // used in exception strings
224:
225:        } // end of class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.