Source Code Cross Referenced for DateConverter.java in  » Swing-Library » jide-common » com » jidesoft » converter » 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 » Swing Library » jide common » com.jidesoft.converter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * @(#) DateConverter.java
003:         *
004:         * Copyright 2002 - 2003 JIDE Software. All rights reserved.
005:         */
006:        package com.jidesoft.converter;
007:
008:        import java.text.DateFormat;
009:        import java.text.ParseException;
010:        import java.text.SimpleDateFormat;
011:        import java.util.Calendar;
012:        import java.util.Date;
013:
014:        /**
015:         * Converter which converts Date to String and converts it back.
016:         */
017:        public class DateConverter implements  ObjectConverter {
018:
019:            public static final ConverterContext DATETIME_CONTEXT = new ConverterContext(
020:                    "DateTime");
021:            public static final ConverterContext TIME_CONTEXT = new ConverterContext(
022:                    "Time");
023:            public static final ConverterContext DATE_CONTEXT = new ConverterContext(
024:                    "Date");
025:
026:            private DateFormat _shortFormat = SimpleDateFormat
027:                    .getDateInstance(DateFormat.SHORT);
028:            private DateFormat _mediumFormat = SimpleDateFormat
029:                    .getDateInstance(DateFormat.MEDIUM);
030:            private DateFormat _longFormat = SimpleDateFormat
031:                    .getDateInstance(DateFormat.LONG);
032:
033:            private DateFormat _defaultFormat = SimpleDateFormat
034:                    .getDateInstance(DateFormat.DEFAULT);
035:
036:            private DateFormat _shortDatetimeFormat = SimpleDateFormat
037:                    .getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
038:            private DateFormat _mediumDatetimeFormat = SimpleDateFormat
039:                    .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
040:            private DateFormat _longDatetimeFormat = SimpleDateFormat
041:                    .getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
042:
043:            private DateFormat _defaultDatetimeFormat = SimpleDateFormat
044:                    .getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
045:
046:            private DateFormat _shortTimeFormat = SimpleDateFormat
047:                    .getTimeInstance(DateFormat.SHORT);
048:            private DateFormat _mediumTimeFormat = SimpleDateFormat
049:                    .getTimeInstance(DateFormat.MEDIUM);
050:            private DateFormat _longTimeFormat = SimpleDateFormat
051:                    .getTimeInstance(DateFormat.LONG);
052:
053:            private DateFormat _defaultTimeFormat = SimpleDateFormat
054:                    .getTimeInstance(DateFormat.DEFAULT);
055:
056:            /**
057:             * Creates a DateConverter.
058:             */
059:            public DateConverter() {
060:            }
061:
062:            /**
063:             * Converts the object to String. The object can be a Calendar, a Date or a Number. As long as the DateFormat
064:             * can format it correctly, it will be converted to a String. If the object is already a String, we will
065:             * return it directly as it is.
066:             *
067:             * @param object
068:             * @param context
069:             * @return the string
070:             */
071:            public String toString(Object object, ConverterContext context) {
072:                if (object == null) {
073:                    return "";
074:                } else {
075:                    if (object instanceof  Calendar) {
076:                        object = ((Calendar) object).getTime();
077:                    }
078:
079:                    if (object instanceof  Date || object instanceof  Number) {
080:                        if (context != null
081:                                && context.getUserObject() instanceof  DateFormat) {
082:                            return ((DateFormat) context.getUserObject())
083:                                    .format(object);
084:                        } else if (DATETIME_CONTEXT.equals(context)) {
085:                            return _defaultDatetimeFormat.format(object);
086:                        } else if (TIME_CONTEXT.equals(context)) {
087:                            return _defaultTimeFormat.format(object);
088:                        } else {
089:                            return _defaultFormat.format(object);
090:                        }
091:                    } else if (object instanceof  String) {
092:                        return (String) object;
093:                    } else {
094:                        return null;
095:                    }
096:                }
097:            }
098:
099:            public boolean supportToString(Object object,
100:                    ConverterContext context) {
101:                return true;
102:            }
103:
104:            /**
105:             * Converts from a String to a Date.
106:             *
107:             * @param string  the string to be converted.
108:             * @param context the context. It could be DATETIME_CONTEXT, DATE_CONTEXT or TIME_CONTEXT.
109:             * @return the Date. If the string is null or empty, null will be returned. If the string cannot be parsed as a date, the string itself will be returned.
110:             */
111:            public Object fromString(String string, ConverterContext context) {
112:                if (string == null || string.trim().length() == 0) {
113:                    return null;
114:                }
115:
116:                try {
117:                    Object userObject = context != null ? context
118:                            .getUserObject() : null;
119:                    if (userObject instanceof  DateFormat) {
120:                        return ((DateFormat) userObject).parse(string);
121:                    } else if (DATETIME_CONTEXT.equals(context)) {
122:                        return _defaultDatetimeFormat.parse(string);
123:                    } else if (TIME_CONTEXT.equals(context)) {
124:                        return _defaultTimeFormat.parse(string);
125:                    } else {
126:                        return _defaultFormat.parse(string);
127:                    }
128:                } catch (ParseException e1) { // if current formatter doesn't work try those default ones.
129:                    if (DATETIME_CONTEXT.equals(context)) {
130:                        try {
131:                            return _shortDatetimeFormat.parse(string);
132:                        } catch (ParseException e2) {
133:                            try {
134:                                return _mediumDatetimeFormat.parse(string);
135:                            } catch (ParseException e3) {
136:                                try {
137:                                    return _longDatetimeFormat.parse(string);
138:                                } catch (ParseException e4) {
139:                                    return string; // nothing works just return null so that old value will be kept.
140:                                }
141:                            }
142:                        }
143:                    } else if (TIME_CONTEXT.equals(context)) {
144:                        try {
145:                            return _shortTimeFormat.parse(string);
146:                        } catch (ParseException e2) {
147:                            try {
148:                                return _mediumTimeFormat.parse(string);
149:                            } catch (ParseException e3) {
150:                                try {
151:                                    return _longTimeFormat.parse(string);
152:                                } catch (ParseException e4) {
153:                                    return string; // nothing works just return null so that old value will be kept.
154:                                }
155:                            }
156:                        }
157:                    } else {
158:                        try {
159:                            return _shortFormat.parse(string);
160:                        } catch (ParseException e2) {
161:                            try {
162:                                return _mediumFormat.parse(string);
163:                            } catch (ParseException e3) {
164:                                try {
165:                                    return _longFormat.parse(string);
166:                                } catch (ParseException e4) {
167:                                    return string; // nothing works just return null so that old value will be kept.
168:                                }
169:                            }
170:                        }
171:                    }
172:                }
173:            }
174:
175:            public boolean supportFromString(String string,
176:                    ConverterContext context) {
177:                return true;
178:            }
179:
180:            /**
181:             * Gets DefaultFormat to format an calendar.
182:             *
183:             * @return DefaultFormat
184:             */
185:            public DateFormat getDefaultFormat() {
186:                return _defaultFormat;
187:            }
188:
189:            /**
190:             * Sets DefaultFormat to format an calendar.
191:             *
192:             * @param defaultFormat
193:             */
194:            public void setDefaultFormat(DateFormat defaultFormat) {
195:                _defaultFormat = defaultFormat;
196:            }
197:
198:            /**
199:             * Gets DefaultTimeFormat to format an calendar. This is used
200:             * only when context is {@link #TIME_CONTEXT}.
201:             *
202:             * @return DefaultTimeFormat
203:             */
204:            public DateFormat getDefaultTimeFormat() {
205:                return _defaultTimeFormat;
206:            }
207:
208:            /**
209:             * Sets DefaultTimeFormat to format an calendar. This is used
210:             * only when context is {@link #TIME_CONTEXT}.
211:             *
212:             * @param defaultTimeFormat
213:             */
214:            public void setDefaultTimeFormat(DateFormat defaultTimeFormat) {
215:                _defaultTimeFormat = defaultTimeFormat;
216:            }
217:
218:            /**
219:             * Gets DefaultDatetimeFormat to format an calendar. This is used
220:             * only when context is {@link #DATETIME_CONTEXT}.
221:             *
222:             * @return DefaultDatetimeFormat
223:             */
224:            public DateFormat getDefaultDatetimeFormat() {
225:                return _defaultDatetimeFormat;
226:            }
227:
228:            /**
229:             * Sets DefaultDatetimeFormat to format an calendar. This is used
230:             * only when context is {@link #DATETIME_CONTEXT}.
231:             *
232:             * @param defaultDatetimeFormat
233:             */
234:            public void setDefaultDatetimeFormat(
235:                    DateFormat defaultDatetimeFormat) {
236:                _defaultDatetimeFormat = defaultDatetimeFormat;
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.