Source Code Cross Referenced for DateFormats.java in  » Database-ORM » MMBase » org » mmbase » 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 ORM » MMBase » org.mmbase.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util;
011:
012:        import java.util.*;
013:        import java.text.*;
014:
015:        import org.mmbase.util.logging.Logger;
016:        import org.mmbase.util.logging.Logging;
017:
018:        /**
019:         * Utility function to create DateFormat instances.
020:         * 
021:         * @author Michiel Meeuwissen
022:         * @since  MMBase-1.7.1
023:         * @version $Id: DateFormats.java,v 1.5 2007/06/20 14:00:10 michiel Exp $
024:         */
025:        public class DateFormats {
026:
027:            private static final Logger log = Logging
028:                    .getLoggerInstance(DateFormats.class);
029:
030:            /**
031:             * Creates a DateFormat instance, based on a String.
032:             *
033:             * @param format The format defining the DateFormat. This can be constants like :FULL, :FULL.FULL, :LONG, :MEDIUM or :SHORT. 
034:             *               It can also be 'e' for weekday. Also 'RFC822' or 'rfc822' is possible then the 
035:             *               quite complicated http://www.faqs.org/rfcs/rfc822.html compliant date and time
036:             *               is made, which comes in handy when you need to create a rss feed f.e.
037:             *               Or none of those, then a SimpleDateFormat is instantiated.
038:             * @param timeZone A String describing the timeZone (see DateFormat#setTimeZone)
039:             * @param locale   Most DateFormat's need a Locale too.
040:             * @throws IllegalArgumentException
041:             */
042:            public static DateFormat getInstance(String format,
043:                    String timeZone, Locale locale) {
044:                DateFormat df;
045:                if (format.length() > 0 && format.charAt(0) == ':') {
046:                    log.debug("found symbolic format");
047:                    if (format.charAt(1) == '.') {
048:                        df = DateFormat
049:                                .getTimeInstance(getDateFormatStyle(format
050:                                        .substring(2)), locale);
051:                    } else if (format.indexOf('.') == -1) {
052:                        df = DateFormat
053:                                .getDateInstance(getDateFormatStyle(format
054:                                        .substring(1)), locale);
055:                    } else {
056:                        int i = format.indexOf('.');
057:                        df = DateFormat.getDateTimeInstance(
058:                                getDateFormatStyle(format.substring(1, i)),
059:                                getDateFormatStyle(format.substring(i + 1)),
060:                                locale);
061:                    }
062:                } else if (format.equals("e")) {
063:                    df = new DayOfWeekDateFormat();
064:                } else if (format.equals("RFC822") || format.equals("rfc822")) {
065:                    df = new SimpleDateFormat("EE, dd MMM yyyy hh:mm:ss Z",
066:                            Locale.US);
067:                    timeZone = "UTC";
068:                } else {
069:                    df = new SimpleDateFormat(format, locale);
070:                }
071:                if (!(timeZone == null || timeZone.equals(""))) {
072:                    df.setTimeZone(TimeZone.getTimeZone(timeZone));
073:                } else {
074:                    df
075:                            .setTimeZone(org.mmbase.util.dateparser.DateParser.defaultTimeZone);
076:                }
077:                return df;
078:
079:            }
080:
081:            /**
082:             * Converts a string to a DateFormat constant.
083:             *
084:             * @param style A string describing the dateformat style (FULL, LONG, MEDIUM, SHORT)
085:             * @return A DateFormat style constant.
086:             * @see    java.text.DateFormat
087:             */
088:            private static int getDateFormatStyle(String style) {
089:                if ("FULL".equals(style)) {
090:                    return DateFormat.FULL;
091:                } else if ("LONG".equals(style)) {
092:                    return DateFormat.LONG;
093:                } else if ("MEDIUM".equals(style)) {
094:                    return DateFormat.MEDIUM;
095:                } else if ("SHORT".equals(style)) {
096:                    return DateFormat.SHORT;
097:                } else {
098:                    throw new IllegalArgumentException(
099:                            "Unknown DateFormat Style " + style);
100:                }
101:            }
102:
103:            /**
104:             * There is no DateFormat which can return the day of the week as a number available in
105:             * java.text package.  This provides one. 
106:             */
107:
108:            protected static class DayOfWeekDateFormat extends DateFormat {
109:                private TimeZone zone = null;
110:
111:                public Date parse(String source, ParsePosition pos) {
112:                    Calendar calendar = Calendar
113:                            .getInstance(zone != null ? zone
114:                                    : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
115:                    int day = source.charAt(0) - '0';
116:                    pos.setIndex(pos.getIndex() + 1);
117:                    calendar.set(Calendar.DAY_OF_WEEK, day);
118:                    return calendar.getTime();
119:                }
120:
121:                public StringBuffer format(Date date, StringBuffer toAppendTo,
122:                        FieldPosition pos) {
123:                    Calendar calendar = Calendar
124:                            .getInstance(zone != null ? zone
125:                                    : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
126:                    calendar.setTime(date);
127:                    // pos.setBeginIndex(0); pos.setEndIndex(1);
128:                    toAppendTo.append(calendar.get(Calendar.DAY_OF_WEEK));
129:                    return toAppendTo;
130:                }
131:
132:                public void setTimeZone(TimeZone value) {
133:                    zone = value;
134:                }
135:
136:            }
137:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.