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


001:        /**
002:         * Take the input string (of a date) and use it to create a
003:         * string that will be speakable by the TTS server
004:         */package com.sun.mobile.util;
005:
006:        import java.util.Calendar;
007:        import java.util.Date;
008:        import java.util.Locale;
009:        import java.util.ResourceBundle;
010:
011:        import java.util.MissingResourceException;
012:
013:        import java.text.DateFormat;
014:        import java.text.MessageFormat;
015:        import java.text.ParsePosition;
016:        import java.text.SimpleDateFormat;
017:
018:        import com.iplanet.am.util.Debug;
019:
020:        public class SpeakableDate {
021:
022:            private String dateString = null;
023:            private String localeString = null;
024:
025:            private Locale locale = null;
026:
027:            private Date date = null;
028:            private Calendar today = null;
029:
030:            private long start_of_todays_time_in_milliseconds = 0;
031:
032:            private static Debug debug = Debug.getInstance("mapJsp");
033:
034:            // ResourceBundle for this class
035:            ResourceBundle resource_bundle = null;
036:            String resource_bundle_name = "SpeakableDate";
037:
038:            boolean already_got_resource_bundle = false;
039:
040:            private static String[] resource_names_for_days_of_month = {
041:                    "first", "second", "third", "fourth", "fifth", "sixth",
042:                    "seventh", "eighth", "ninth", "tenth", "eleventh",
043:                    "twelfth", "thirteenth", "fourteenth", "fifteenth",
044:                    "sixteenth", "seventeenth", "eighteenth", "nineteenth",
045:                    "twentieth", "twenty_first", "twenty_second",
046:                    "twenty_third", "twenty_fourth", "twenty_fifth",
047:                    "twenty_sixth", "twenty_seventh", "twenty_eighth",
048:                    "twenty_ninth", "thirtieth", "thirty_first" };
049:
050:            public SpeakableDate() {
051:                // set Today
052:                setToday();
053:            }
054:
055:            /*
056:             * A convenience method for logging useful information into mapJsp
057:             */
058:            private void log(String day, Calendar calendar) {
059:                if (debug.messageEnabled()) {
060:                    Date date = calendar.getTime();
061:                    debug.message("Start of " + day + ": " + date.getTime()
062:                            + " (" + date + ")");
063:                }
064:            }
065:
066:            private void setToday() {
067:                // get the time right now
068:                today = Calendar.getInstance();
069:                log("Rightnow", today);
070:
071:                /*
072:                 * reset all time fields so that we have the absolute start of "today"
073:                 */
074:                today.set(Calendar.AM_PM, Calendar.AM);
075:                today.set(Calendar.HOUR_OF_DAY, 0);
076:                today.set(Calendar.HOUR, 0);
077:                today.set(Calendar.MINUTE, 0);
078:                today.set(Calendar.SECOND, 0);
079:                today.set(Calendar.MILLISECOND, 0);
080:                log("Today", today);
081:
082:                start_of_todays_time_in_milliseconds = today.getTime()
083:                        .getTime();
084:            }
085:
086:            /*
087:             * Test to see if the date is "today".
088:             */
089:            private boolean isToday(Date date) {
090:                long time_in_milliseconds = date.getTime();
091:
092:                if (time_in_milliseconds >= start_of_todays_time_in_milliseconds) {
093:                    Calendar tomorrow = (Calendar) today.clone();
094:                    tomorrow.add(Calendar.DATE, 1);
095:                    log("Tomorrow", tomorrow);
096:
097:                    if (time_in_milliseconds < tomorrow.getTime().getTime()) {
098:                        return true;
099:                    }
100:                }
101:
102:                return (false);
103:            }
104:
105:            /*
106:             * Test to see if the date is "yesterday".
107:             */
108:            private boolean isYesterday(Date date) {
109:                long time_in_milliseconds = date.getTime();
110:
111:                if (time_in_milliseconds < start_of_todays_time_in_milliseconds) {
112:                    Calendar yesterday = (Calendar) today.clone();
113:                    yesterday.add(Calendar.DATE, -1);
114:                    log("Yesterday", yesterday);
115:
116:                    if (time_in_milliseconds >= yesterday.getTime().getTime()) {
117:                        return true;
118:                    }
119:                }
120:
121:                return (false);
122:            }
123:
124:            private String getDay(Calendar calendar) {
125:                switch (calendar.get(Calendar.DAY_OF_WEEK)) {
126:                case Calendar.SUNDAY:
127:                    return (getResourceString("sunday", "sunday"));
128:
129:                case Calendar.MONDAY:
130:                    return (getResourceString("monday", "monday"));
131:
132:                case Calendar.TUESDAY:
133:                    return (getResourceString("tuesday", "tuesday"));
134:
135:                case Calendar.WEDNESDAY:
136:                    return (getResourceString("wednesday", "wednesday"));
137:
138:                case Calendar.THURSDAY:
139:                    return (getResourceString("thursday", "thursday"));
140:
141:                case Calendar.FRIDAY:
142:                    return (getResourceString("friday", "friday"));
143:
144:                case Calendar.SATURDAY:
145:                    return (getResourceString("saturday", "saturday"));
146:
147:                default:
148:                    return ("");
149:                }
150:            }
151:
152:            private String getMonth(Calendar calendar) {
153:                switch (calendar.get(Calendar.MONTH)) {
154:                case Calendar.JANUARY:
155:                    return (getResourceString("january", "january"));
156:
157:                case Calendar.FEBRUARY:
158:                    return (getResourceString("february", "february"));
159:
160:                case Calendar.MARCH:
161:                    return (getResourceString("march", "march"));
162:
163:                case Calendar.APRIL:
164:                    return (getResourceString("april", "april"));
165:
166:                case Calendar.MAY:
167:                    return (getResourceString("may", "may"));
168:
169:                case Calendar.JUNE:
170:                    return (getResourceString("june", "june"));
171:
172:                case Calendar.JULY:
173:                    return (getResourceString("july", "july"));
174:
175:                case Calendar.AUGUST:
176:                    return (getResourceString("august", "august"));
177:
178:                case Calendar.SEPTEMBER:
179:                    return (getResourceString("september", "september"));
180:
181:                case Calendar.OCTOBER:
182:                    return (getResourceString("october", "october"));
183:
184:                case Calendar.NOVEMBER:
185:                    return (getResourceString("november", "november"));
186:
187:                case Calendar.DECEMBER:
188:                    return (getResourceString("december", "december"));
189:
190:                default:
191:                    return ("");
192:                }
193:            }
194:
195:            private String getDayOfMonth(Calendar calendar) {
196:                String key = null;
197:
198:                int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
199:                if ((day_of_month >= calendar.getMinimum(Calendar.DAY_OF_MONTH))
200:                        && (day_of_month <= calendar
201:                                .getMaximum(Calendar.DAY_OF_MONTH))) {
202:                    key = resource_names_for_days_of_month[day_of_month - 1];
203:                } else {
204:                    // this will almost never happen
205:                    key = resource_names_for_days_of_month[0];
206:                }
207:
208:                return (getResourceString(key, key));
209:            }
210:
211:            private String getAmPm(Calendar cal) {
212:                switch (cal.get(Calendar.AM_PM)) {
213:                case Calendar.AM:
214:                    return (getResourceString("am", "am"));
215:
216:                case Calendar.PM:
217:                    return (getResourceString("pm", "pm"));
218:
219:                default:
220:                    return ("");
221:                }
222:            }
223:
224:            public void setDateString(String string) {
225:                dateString = string;
226:
227:                if (debug.messageEnabled()) {
228:                    debug.message("Setting the date string to : " + dateString);
229:                }
230:            }
231:
232:            public void setLocaleString(String string) {
233:                localeString = string;
234:
235:                if (debug.messageEnabled()) {
236:                    debug.message("Setting the locale string to : "
237:                            + localeString);
238:                }
239:
240:                locale = com.iplanet.am.util.Locale.getLocale(string);
241:            }
242:
243:            private ResourceBundle getResourceBundle() {
244:                if (!already_got_resource_bundle) {
245:                    // get the resource bundle for this locale
246:                    try {
247:                        if (locale != null) {
248:                            resource_bundle = ResourceBundle.getBundle(
249:                                    resource_bundle_name, locale);
250:                        } else {
251:                            resource_bundle = ResourceBundle
252:                                    .getBundle(resource_bundle_name);
253:                        }
254:                    } catch (MissingResourceException e) {
255:                        if (debug.messageEnabled()) {
256:                            debug.message("Could not get ResourceBundle for: "
257:                                    + resource_bundle_name + "; locale = "
258:                                    + locale);
259:                        }
260:                    }
261:
262:                    already_got_resource_bundle = true;
263:                }
264:
265:                return (resource_bundle);
266:            }
267:
268:            /*
269:             * This is the method which is going to be called from JSP
270:             */
271:            public String getSpeakableDate() {
272:                // First take the date string that we were given and create a Date object
273:                if (date == null) {
274:                    date = constructDateFromString(dateString);
275:
276:                    // If there was an error, the returned Date object will be null
277:                    if (date == null) {
278:                        return ("");
279:                    }
280:                }
281:
282:                // Check if this is today
283:                if (isToday(date)) {
284:                    return getResourceString("today", "today");
285:                }
286:
287:                // Check if this is yesterday
288:                if (isYesterday(date)) {
289:                    return getResourceString("yesterday", "yesterday");
290:                }
291:
292:                String format_string = getResourceString("date", "");
293:                if (format_string.equals("")) {
294:                    return date.toString();
295:                }
296:
297:                /*
298:                 * Otherwise return the string formatted the way I need it for the
299:                 * speech server
300:                 */
301:                Calendar calendar_date = Calendar.getInstance();
302:                calendar_date.setTime(date);
303:
304:                String[] args = { getDay(calendar_date),
305:                        getMonth(calendar_date), getDayOfMonth(calendar_date) };
306:
307:                return (MessageFormat.format(format_string, args));
308:            }
309:
310:            public String getSpeakableTime() {
311:                // First take the date string that we were given and create a Date object
312:                if (date == null) {
313:                    date = constructDateFromString(dateString);
314:
315:                    // If there was an error, the returned Date object will be null
316:                    if (date == null) {
317:                        return ("");
318:                    }
319:                }
320:
321:                Calendar calendar_date = Calendar.getInstance();
322:                calendar_date.setTime(date);
323:
324:                // we'll use this to format the time
325:                String format_string = null;
326:
327:                int minutes = calendar_date.get(Calendar.MINUTE);
328:                if (minutes == 0) {
329:                    format_string = getResourceString("time_hr", "");
330:                } else {
331:                    format_string = getResourceString("time_hr_min", "");
332:                }
333:
334:                if (format_string.equals("")) {
335:                    return date.toString();
336:                }
337:
338:                String hour_string = null;
339:
340:                int hour = calendar_date.get(Calendar.HOUR);
341:                if (hour != 0) {
342:                    hour_string = Integer.toString(hour);
343:                } else {
344:                    hour_string = getResourceString("hour_zero", "0");
345:                }
346:
347:                String[] args = { hour_string, Integer.toString(minutes),
348:                        getAmPm(calendar_date) };
349:
350:                return (MessageFormat.format(format_string, args));
351:            }
352:
353:            private String getResourceString(String key, String default_value) {
354:                ResourceBundle bundle = getResourceBundle();
355:                if (bundle == null) {
356:                    return (default_value);
357:                }
358:
359:                String value = null;
360:                try {
361:                    value = bundle.getString(key);
362:                } catch (MissingResourceException e) {
363:                    value = default_value;
364:
365:                    if (debug.messageEnabled()) {
366:                        debug.message("Could not get resource bundle key: "
367:                                + key);
368:                    }
369:                }
370:
371:                return (value);
372:            }
373:
374:            /*
375:             * Take the input string and parse it back into a Date object.
376:             * - the MAP taglibs deliver a date string that looks like:
377:             *   "Mon Oct 13 11:28:39 PDT 2003"
378:             *   "10/13/03 11:28 AM"
379:             */
380:            public Date constructDateFromString(String dateString) {
381:                if (debug.messageEnabled()) {
382:                    debug.message("parsing now: " + dateString);
383:                }
384:
385:                // try parsing this string into something
386:                ParsePosition pos = new ParsePosition(0);
387:
388:                DateFormat df;
389:                if (locale == null) {
390:                    df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
391:                            DateFormat.MEDIUM);
392:                } else {
393:                    df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
394:                            DateFormat.MEDIUM, locale);
395:                }
396:
397:                // convert to simple date format object
398:                SimpleDateFormat sdf = (SimpleDateFormat) df;
399:
400:                // make my own formatting pattern, "10/13/03 11:28 AM"
401:                sdf.applyPattern("M/d/yy h:mm a");
402:
403:                Date parsed_date = sdf.parse(dateString, pos);
404:                if (parsed_date == null) {
405:                    // maybe it is of this type "Mon Oct 13 11:28:39 PDT 2003"
406:                    sdf.applyPattern("EE MMM d H:mm:ss z yyyy");
407:                    pos.setIndex(0);
408:
409:                    // parse it back
410:                    parsed_date = sdf.parse(dateString, pos);
411:                }
412:
413:                return parsed_date;
414:            }
415:
416:            // for testing this from the command line
417:            public static void main(String args[]) {
418:                // the string to format
419:                String str = "Mon Oct 13 11:28:39 PDT 2003";
420:
421:                // input arguments?
422:                if (args.length > 0) {
423:                    str = args[0];
424:                }
425:
426:                // create this Object
427:                SpeakableDate sd = new SpeakableDate();
428:                sd.setDateString(str);
429:
430:                String speakable_date = sd.getSpeakableDate();
431:                System.out.println("Speakable date: " + speakable_date);
432:
433:                String speakable_time = sd.getSpeakableTime();
434:                System.out.println("Speakable time: " + speakable_time);
435:            }
436:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.