001    package railo.commons.i18n;
002    
003    import java.text.DateFormat;
004    import java.text.SimpleDateFormat;
005    import java.util.Locale;
006    import java.util.Map;
007    import java.util.TimeZone;
008    
009    import org.apache.commons.collections.map.ReferenceMap;
010    
011    
012    public class FormatUtil {
013     
014            private final static Map<String,DateFormat[]> formats=new ReferenceMap(ReferenceMap.SOFT,ReferenceMap.SOFT);
015            
016            public static DateFormat[] getDateTimeFormats(Locale locale,TimeZone tz,boolean lenient) {
017    
018                    String id="dt-"+locale.hashCode()+"-"+tz.getID()+"-"+lenient;
019                    DateFormat[] df=(DateFormat[]) formats.get(id);
020                    if(df==null) {
021                            df= new DateFormat[]{
022                                            DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,locale),
023                            DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.LONG,locale),
024                            DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.MEDIUM,locale),
025                            DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.SHORT,locale),
026    
027                            DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.FULL,locale),
028                            DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,locale),
029                            DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM,locale),
030                            DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT,locale),
031    
032                            DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.FULL,locale),
033                            DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.LONG,locale),
034                            DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale),
035                            DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT,locale),
036    
037                            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.FULL,locale),
038                            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG,locale),
039                            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM,locale),
040                            DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT,locale)
041                    };
042                            for(int i=0;i<df.length;i++){
043                                    df[i].setLenient(lenient);
044                                    df[i].setTimeZone(tz);
045                            }
046                            
047                            formats.put(id, df);
048                    }
049                    
050                    return df;
051            }
052            public static DateFormat[] getDateFormats(Locale locale,TimeZone tz,boolean lenient) {
053                    String id="d-"+locale.hashCode()+"-"+tz.getID()+"-"+lenient;
054                    DateFormat[] df=(DateFormat[]) formats.get(id);
055                    if(df==null) {
056                            df= new DateFormat[]{
057                    DateFormat.getDateInstance(DateFormat.FULL,locale),
058                    DateFormat.getDateInstance(DateFormat.LONG,locale),
059                    DateFormat.getDateInstance(DateFormat.MEDIUM,locale),
060                    DateFormat.getDateInstance(DateFormat.SHORT,locale)
061                    };
062    
063                            for(int i=0;i<df.length;i++){
064                                    df[i].setLenient(lenient);
065                                    df[i].setTimeZone(tz);
066                            }
067                            formats.put(id, df);
068                    }
069                    return df;
070            }
071            
072    
073            /**
074             * CFML Supported LS Formats
075             * @param locale
076             * @param tz
077             * @param lenient
078             * @return
079             */
080            public static DateFormat[] getCFMLFormats(TimeZone tz,boolean lenient) {
081                    String id="cfml-"+Locale.ENGLISH.hashCode()+"-"+tz.getID()+"-"+lenient;
082                    DateFormat[] df=(DateFormat[]) formats.get(id);
083                    if(df==null) {
084                            df= new SimpleDateFormat[]{
085                                              new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH)
086                                             ,new SimpleDateFormat("MMM dd, yyyy H:mm:ss a",Locale.ENGLISH)  
087                                             ,new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss a zzz",Locale.ENGLISH)
088                                             ,new SimpleDateFormat("MMMM d yyyy HH:mm:ssZ",Locale.ENGLISH)
089                                             ,new SimpleDateFormat("MMMM d yyyy HH:mm:ss",Locale.ENGLISH)
090                                             ,new SimpleDateFormat("MMMM d yyyy HH:mm",Locale.ENGLISH)
091                                             ,new SimpleDateFormat("EEE, MMM dd, yyyy HH:mm:ssZ",Locale.ENGLISH)
092                                             ,new SimpleDateFormat("EEE, MMM dd, yyyy HH:mm:ss",Locale.ENGLISH)
093                                         ,new SimpleDateFormat("EEEE, MMMM dd, yyyy H:mm:ss a zzz",Locale.ENGLISH)
094                                             ,new SimpleDateFormat("dd-MMM-yy HH:mm a",Locale.ENGLISH)
095                                             ,new SimpleDateFormat("dd-MMMM-yy HH:mm a",Locale.ENGLISH)
096                                             ,new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz",Locale.ENGLISH)
097                                             ,new SimpleDateFormat("EEE d, MMM yyyy HH:mm:ss zz",Locale.ENGLISH)
098                                             ,new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH)
099                                             ,new SimpleDateFormat("MMMM, dd yyyy HH:mm:ssZ",Locale.ENGLISH)
100                                             ,new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss",Locale.ENGLISH)
101                                             ,new SimpleDateFormat("yyyy/MM/dd HH:mm:ss zz",Locale.ENGLISH)
102                                             ,new SimpleDateFormat("dd MMM yyyy HH:mm:ss zz",Locale.ENGLISH)
103                                             ,new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZ (z)",Locale.ENGLISH)
104                                             //,new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.ENGLISH)
105                                    };
106    
107                            for(int i=0;i<df.length;i++){
108                                    df[i].setLenient(lenient);
109                                    df[i].setTimeZone(tz);
110                            }
111                            formats.put(id, df);
112                    }
113                    return df;
114            }
115            
116            
117            
118            public static DateFormat[] getTimeFormats(Locale locale,TimeZone tz,boolean lenient) {
119                    String id="t-"+locale.hashCode()+"-"+tz.getID()+"-"+lenient;
120                    DateFormat[] df=(DateFormat[]) formats.get(id);
121                    if(df==null) {
122                            df= new DateFormat[]{
123                            DateFormat.getTimeInstance(DateFormat.FULL,locale),
124                            DateFormat.getTimeInstance(DateFormat.LONG,locale),
125                            DateFormat.getTimeInstance(DateFormat.MEDIUM,locale),
126                            DateFormat.getTimeInstance(DateFormat.SHORT,locale)
127                    };
128    
129                            for(int i=0;i<df.length;i++){
130                                     df[i].setLenient(lenient);
131                                     df[i].setTimeZone(tz);
132                            }
133                            formats.put(id, df);
134                    }
135                    return df;
136            }
137    }