Source Code Cross Referenced for DateTimeInfo.java in  » Portal » Open-Portal » com » sun » portal » common » 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 » Portal » Open Portal » com.sun.portal.common.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // ***************************************************************
002:        // *                                                             *
003:        // * File: DateTimeInfo.java                                     *
004:        // *                                                             *
005:        // *  Copyright (c) 2001 Sun Microsystems, Inc.                  *
006:        // *  All rights reserved.                                       *
007:        // *                                                             *
008:        // *                                                             *
009:        // * Date      - Dec/11/2001                                     *
010:        // * Author    - alejandro.abdelnur@sun.com                      *
011:        // *                                                             *
012:        // ***************************************************************
013:
014:        package com.sun.portal.common.util;
015:
016:        import java.util.Map;
017:        import java.util.HashMap;
018:        import java.util.Locale;
019:        import java.util.Date;
020:        import java.util.Calendar;
021:        import java.util.ResourceBundle;
022:
023:        import java.text.DateFormat;
024:
025:        /**
026:         *
027:         * @author <A HREF="mailto:alejandro.abdelnur@sun.com">Alejandro Abdelnur</A>
028:         *
029:         */
030:        public class DateTimeInfo {
031:
032:            private static final Map _store = new HashMap();
033:
034:            public static DateTimeInfo getInstance() {
035:                return getInstance(Locale.getDefault());
036:            }
037:
038:            public static DateTimeInfo getInstance(Locale locale) {
039:                DateTimeInfo dti = (DateTimeInfo) _store.get(locale);
040:                if (dti == null) {
041:                    synchronized (_store) {
042:                        dti = (DateTimeInfo) _store.get(locale);
043:                        if (dti == null) {
044:                            dti = new DateTimeInfo(locale);
045:                            _store.put(locale, dti);
046:                        }
047:                    }
048:                }
049:                return dti;
050:            }
051:
052:            private static final String[] DAYS = { "1", "2", "3", "4", "5",
053:                    "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
054:                    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
055:                    "26", "27", "28", "29", "30", "31" };
056:
057:            private static long _lastCalculation = 0;
058:            private static String[] YEARS;
059:
060:            private static final String[] HOURS_24 = { "0", "1", "2", "3", "4",
061:                    "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
062:                    "15", "16", "17", "18", "19", "20", "21", "22", "23" };
063:
064:            private static final String[] HOURS_12 = { "1", "2", "3", "4", "5",
065:                    "6", "7", "8", "9", "10", "11", "12" };
066:
067:            private static final String[] MINUTES = { "00", "01", "02", "03",
068:                    "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
069:                    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
070:                    "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
071:                    "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
072:                    "44", "45", "46", "47", "48", "49", "50", "51", "52", "53",
073:                    "54", "55", "56", "57", "58", "59" };
074:
075:            private static final String[] SECONDS = MINUTES;
076:
077:            private static final String BUNDLE_NAME = "java.text.resources.LocaleElements";
078:
079:            private Locale _locale;
080:            private int[] _dateOrder;
081:            private String[] _abbrMonths;
082:            private String[] _months;
083:            private String[] _days;
084:            private boolean _24Hours;
085:            private String[] _hours;
086:            private String[] _minutes;
087:            private String[] _seconds;
088:            private String[] _amPmMarkers;
089:            private int[] _timeOrder;
090:            private char _dateElementSeparator;
091:
092:            private DateTimeInfo(Locale locale) {
093:                _locale = locale;
094:
095:                ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME,
096:                        _locale);
097:                _dateOrder = obtainDateOrder(bundle);
098:                _abbrMonths = obtainAbbrMonths(bundle);
099:                _months = obtainMonths(bundle);
100:                _days = DAYS;
101:                _24Hours = obtainIs24Hours(bundle);
102:                _hours = (_24Hours) ? HOURS_24 : HOURS_12;
103:                _minutes = MINUTES;
104:                _seconds = SECONDS;
105:                _amPmMarkers = obtainAmPmMarkers(bundle);
106:                _timeOrder = obtainTimeOrder();
107:                _dateElementSeparator = obtainDateElementSeparator(bundle);
108:            }
109:
110:            public Locale getLocale() {
111:                return _locale;
112:            }
113:
114:            public int[] getDateOrder() {
115:                return _dateOrder;
116:            }
117:
118:            public String[] getAbbrMonths() {
119:                return _abbrMonths;
120:            }
121:
122:            public String[] getMonths() {
123:                return _months;
124:            }
125:
126:            public String[] getDays() {
127:                return _days;
128:            }
129:
130:            public String[] getYears() {
131:                if ((System.currentTimeMillis() - _lastCalculation) > (1000L * 60 * 60 * 24)) {
132:                    recalculateYears();
133:                }
134:                return YEARS;
135:            }
136:
137:            public String[] getYears(int begin, int howMany) {
138:                String[] years = new String[howMany];
139:                for (int i = 0; i < years.length; i++) {
140:                    years[i] = Integer.toString(begin++);
141:                }
142:                return years;
143:            }
144:
145:            public int[] getTimeOrder() {
146:                return _timeOrder;
147:            }
148:
149:            public char getDateElementSeparator() {
150:                return _dateElementSeparator;
151:            }
152:
153:            public boolean is24Hours() {
154:                return _24Hours;
155:            }
156:
157:            public String[] getHours() {
158:                return _hours;
159:            }
160:
161:            public String[] getMinutes() {
162:                return _minutes;
163:            }
164:
165:            public String[] getSeconds() {
166:                return _seconds;
167:            }
168:
169:            public String[] getAmPmMarkers() {
170:                return _amPmMarkers;
171:            }
172:
173:            public int toIndexValue(int section, int value) {
174:                switch (section) {
175:                case Calendar.DAY_OF_MONTH:
176:                case Calendar.HOUR:
177:                    value--;
178:                    break;
179:                default:
180:                    break;
181:                }
182:                return value;
183:            }
184:
185:            public int fromStringValue(int section, String str) {
186:                int value = -1;
187:                switch (section) {
188:                case Calendar.YEAR:
189:                    value = Integer.parseInt(str);
190:                    break;
191:                case Calendar.MONTH:
192:                    /*
193:                    for (int i=0;value==-1 && i<_abbrMonths.length;i++) {
194:                        if (_abbrMonths[i].equalsIgnoreCase(str)) {
195:                            value = i;
196:                        }
197:                    }
198:                     */
199:                    value = Integer.parseInt(str) - 1;
200:                    break;
201:                case Calendar.DAY_OF_MONTH:
202:                    value = Integer.parseInt(str);
203:                    break;
204:                case Calendar.HOUR:
205:                    value = Integer.parseInt(str);
206:                    break;
207:                case Calendar.HOUR_OF_DAY:
208:                    value = Integer.parseInt(str);
209:                    break;
210:                case Calendar.MINUTE:
211:                    value = Integer.parseInt(str);
212:                    break;
213:                case Calendar.SECOND:
214:                    value = Integer.parseInt(str);
215:                    break;
216:                case Calendar.AM_PM:
217:                    for (int i = 0; value == -1 && i < _amPmMarkers.length; i++) {
218:                        if (_amPmMarkers[i].equalsIgnoreCase(str)) {
219:                            value = i;
220:                        }
221:                    }
222:                    break;
223:                default:
224:                    break;
225:                }
226:                return value;
227:            }
228:
229:            // --- private initialization stuff
230:
231:            private synchronized void recalculateYears() {
232:                _lastCalculation = System.currentTimeMillis();
233:                Date date = new Date();
234:                Calendar calendar = Calendar.getInstance();
235:                calendar.setTime(date);
236:                int year = calendar.get(calendar.YEAR);
237:                YEARS = new String[10];
238:                for (int i = 0; i < YEARS.length; i++) {
239:                    YEARS[i] = Integer.toString(year++);
240:                }
241:            }
242:
243:            private int[] obtainDateOrder(ResourceBundle bundle) {
244:                String[] patterns = bundle.getStringArray("DateTimePatterns");
245:                char[] datePattern = patterns[7].toCharArray();
246:                int[] localeOrder = new int[4];
247:                for (int i = 0; i < localeOrder.length; i++) {
248:                    localeOrder[i] = -1;
249:                }
250:
251:                int pos = 0;
252:                for (int i = 0; pos < localeOrder.length
253:                        && i < datePattern.length; i++) {
254:                    switch (datePattern[i]) {
255:                    case 'M':
256:                        if (pos == 0 || localeOrder[pos - 1] != Calendar.MONTH) {
257:                            localeOrder[pos++] = Calendar.MONTH;
258:                        }
259:                        break;
260:                    case 'd':
261:                        if (pos == 0
262:                                || localeOrder[pos - 1] != Calendar.DAY_OF_MONTH) {
263:                            localeOrder[pos++] = Calendar.DAY_OF_MONTH;
264:                        }
265:                        break;
266:                    case 'y':
267:                        if (pos == 0 || localeOrder[pos - 1] != Calendar.YEAR) {
268:                            localeOrder[pos++] = Calendar.YEAR;
269:                        }
270:                        break;
271:                    default:
272:                        break;
273:                    }
274:                }
275:                return localeOrder;
276:            }
277:
278:            private char obtainDateElementSeparator(ResourceBundle bundle) {
279:                char separator = '/';
280:                String[] patterns = bundle.getStringArray("DateTimePatterns");
281:                char[] datePattern = patterns[7].toCharArray();
282:                boolean found = false;
283:                for (int i = 0; i < datePattern.length && !found; i++) {
284:                    char c = datePattern[i];
285:                    if (c != 'd' && c != 'M' && c != 'y') {
286:                        separator = c;
287:                        found = true;
288:                    }
289:                }
290:                return separator;
291:            }
292:
293:            private String[] obtainAbbrMonths(ResourceBundle bundle) {
294:                String[] months = bundle.getStringArray("MonthAbbreviations");
295:                String[] realMonths = new String[12];
296:                System.arraycopy(months, 0, realMonths, 0, realMonths.length);
297:                return realMonths;
298:            }
299:
300:            private String[] obtainMonths(ResourceBundle bundle) {
301:                String[] months = bundle.getStringArray("MonthNames");
302:                String[] realMonths = new String[12];
303:                System.arraycopy(months, 0, realMonths, 0, realMonths.length);
304:                return realMonths;
305:            }
306:
307:            private boolean obtainIs24Hours(ResourceBundle bundle) {
308:                String hourPattern = bundle.getStringArray("DateTimePatterns")[2];
309:                return (hourPattern.indexOf("H") > -1);
310:            }
311:
312:            private String[] obtainAmPmMarkers(ResourceBundle bundle) {
313:                return bundle.getStringArray("AmPmMarkers");
314:            }
315:
316:            private int[] obtainTimeOrder() {
317:                int[] order;
318:                if (is24Hours()) {
319:                    order = new int[3];
320:                    order[0] = Calendar.HOUR_OF_DAY;
321:                    order[1] = Calendar.MINUTE;
322:                    order[2] = Calendar.SECOND;
323:                } else {
324:                    order = new int[4];
325:                    order[0] = Calendar.HOUR;
326:                    order[1] = Calendar.MINUTE;
327:                    order[2] = Calendar.SECOND;
328:                    order[3] = Calendar.AM_PM;
329:                }
330:                return order;
331:            }
332:
333:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.