1 17 18 package org.apache.catalina.util; 19 20 import java.text.SimpleDateFormat ; 21 import java.util.Properties ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 import java.util.TimeZone ; 25 26 45 public class Strftime { 46 protected static Properties translate; 47 protected SimpleDateFormat simpleDateFormat; 48 49 52 static { 53 translate = new Properties (); 54 translate.put("a","EEE"); 55 translate.put("A","EEEE"); 56 translate.put("b","MMM"); 57 translate.put("B","MMMM"); 58 translate.put("c","EEE MMM d HH:mm:ss yyyy"); 59 60 translate.put("d","dd"); 64 translate.put("D","MM/dd/yy"); 65 translate.put("e","dd"); translate.put("F","yyyy-MM-dd"); 67 translate.put("g","yy"); 68 translate.put("G","yyyy"); 69 translate.put("H","HH"); 70 translate.put("h","MMM"); 71 translate.put("I","hh"); 72 translate.put("j","DDD"); 73 translate.put("k","HH"); translate.put("l","hh"); translate.put("m","MM"); 76 translate.put("M","mm"); 77 translate.put("n","\n"); 78 translate.put("p","a"); 79 translate.put("P","a"); translate.put("r","hh:mm:ss a"); 81 translate.put("R","HH:mm"); 82 translate.put("S","ss"); 85 translate.put("t","\t"); 86 translate.put("T","HH:mm:ss"); 87 90 93 translate.put("V","ww"); 95 98 translate.put("X","HH:mm:ss"); 101 translate.put("x","MM/dd/yy"); 102 translate.put("y","yy"); 103 translate.put("Y","yyyy"); 104 translate.put("Z","z"); 105 translate.put("z","Z"); 106 translate.put("%","%"); 107 } 108 109 110 115 public Strftime( String origFormat ) { 116 String convertedFormat = convertDateFormat( origFormat ); 117 simpleDateFormat = new SimpleDateFormat ( convertedFormat ); 118 } 119 120 126 public Strftime( String origFormat, Locale locale ) { 127 String convertedFormat = convertDateFormat( origFormat ); 128 simpleDateFormat = new SimpleDateFormat ( convertedFormat, locale ); 129 } 130 131 137 public String format( Date date ) { 138 return simpleDateFormat.format( date ); 139 } 140 141 146 public TimeZone getTimeZone() { 147 return simpleDateFormat.getTimeZone(); 148 } 149 150 155 public void setTimeZone( TimeZone timeZone ) { 156 simpleDateFormat.setTimeZone( timeZone ); 157 } 158 159 167 protected String convertDateFormat( String pattern ) { 168 boolean inside = false; 169 boolean mark = false; 170 boolean modifiedCommand = false; 171 172 StringBuffer buf = new StringBuffer (); 173 174 for(int i = 0; i < pattern.length(); i++) { 175 char c = pattern.charAt(i); 176 177 if ( c=='%' && !mark ) { 178 mark=true; 179 } else { 180 if ( mark ) { 181 if ( modifiedCommand ) { 182 modifiedCommand = false; 184 mark = false; 185 } else { 186 inside = translateCommand( buf, pattern, i, inside ); 187 if ( c=='O' || c=='E' ) { 189 modifiedCommand = true; 190 } else { 191 mark=false; 192 } 193 } 194 } else { 195 if ( !inside && c != ' ' ) { 196 buf.append("'"); 198 inside = true; 199 } 200 201 buf.append(c); 202 } 203 } 204 } 205 206 if ( buf.length() > 0 ) { 207 char lastChar = buf.charAt( buf.length() - 1 ); 208 209 if( lastChar!='\'' && inside ) { 210 buf.append('\''); 211 } 212 } 213 return buf.toString(); 214 } 215 216 protected String quote( String str, boolean insideQuotes ) { 217 String retVal = str; 218 if ( !insideQuotes ) { 219 retVal = '\'' + retVal + '\''; 220 } 221 return retVal; 222 } 223 224 234 protected boolean translateCommand( StringBuffer buf, String pattern, int index, boolean oldInside ) { 235 char firstChar = pattern.charAt( index ); 236 boolean newInside = oldInside; 237 238 if ( firstChar == 'O' || firstChar == 'E' ) { 241 if ( index + 1 < pattern.length() ) { 242 newInside = translateCommand( buf, pattern, index + 1, oldInside ); 243 } else { 244 buf.append( quote("%" + firstChar, oldInside ) ); 245 } 246 } else { 247 String command = translate.getProperty( String.valueOf( firstChar ) ); 248 249 if ( command == null ) { 251 buf.append( quote( "%" + firstChar, oldInside ) ); 252 } else { 253 if ( oldInside ) { 255 buf.append( '\'' ); 256 } 257 buf.append( command ); 258 newInside = false; 259 } 260 } 261 return newInside; 262 } 263 } 264 | Popular Tags |