1 16 package org.apache.taglibs.dbtags.resultset; 17 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import java.sql.Types ; 21 import java.text.DateFormat ; 22 import java.text.SimpleDateFormat ; 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 26 import javax.servlet.jsp.JspTagException ; 27 28 34 public class BaseDateTimeGetterTag extends BaseGetterTag { 35 36 protected static HashMap _mapDateTimeStyle = new HashMap (4); 37 protected String _format = null; 38 39 static 40 { 41 _mapDateTimeStyle.put ("FULL", new Integer (DateFormat.FULL)); 42 _mapDateTimeStyle.put ("LONG", new Integer (DateFormat.LONG)); 43 _mapDateTimeStyle.put ("MEDIUM", new Integer (DateFormat.MEDIUM)); 44 _mapDateTimeStyle.put ("SHORT", new Integer (DateFormat.SHORT)); 45 } 46 47 public void setFormat (String strFormat) { 48 _format = strFormat; 49 } 50 51 60 protected String getDateAsString(int position, int nType) throws JspTagException { 61 62 ResultSet rset = getResultSet(); 63 64 String string = null; 65 try { 66 67 switch (nType) { 68 case Types.DATE: 69 { 70 java.sql.Date date = rset.getDate (position); 71 72 if (date != null) { 73 if (_format == null) 74 return date.toString (); 75 76 DateFormat fmt = getDateFormat (nType, _format, getLocale (_locale)); 77 78 string = fmt.format (date); 79 } 80 break; 81 } 82 case Types.TIME: 83 { 84 java.sql.Time time = rset.getTime (position); 85 86 if (time != null) { 87 if (_format == null) 88 return time.toString (); 89 90 DateFormat fmt = getDateFormat (nType, _format, getLocale (_locale)); 91 92 string = fmt.format (time); 93 } 94 break; 95 } 96 case Types.TIMESTAMP: 97 { 98 java.sql.Timestamp times = rset.getTimestamp (position); 99 100 if (times != null) { 101 if (_format == null) 102 return times.toString (); 103 104 DateFormat fmt = getDateFormat (nType, _format, getLocale (_locale)); 105 106 string = fmt.format (times); 107 } 108 break; 109 } 110 default: 111 throw new JspTagException (); 112 } 113 } catch (SQLException e) { 114 throw new JspTagException (e.toString()); 115 } 116 117 return string; 118 119 } 120 121 private DateFormat getDateFormat (int nType, String strFormat, Locale loc) throws JspTagException 122 { 123 switch (nType) { 124 case Types.DATE: 125 { 126 if (!_mapDateTimeStyle.containsKey (strFormat)) 127 return new SimpleDateFormat (strFormat, loc); 128 129 Integer iDateStyle = (Integer )_mapDateTimeStyle.get (strFormat); 130 return DateFormat.getDateInstance (iDateStyle.intValue(), loc); 131 } 132 133 case Types.TIME: 134 { 135 if (!_mapDateTimeStyle.containsKey (strFormat)) 136 return new SimpleDateFormat (strFormat, loc); 137 138 Integer iTimeStyle = (Integer )_mapDateTimeStyle.get (strFormat); 139 return DateFormat.getTimeInstance (iTimeStyle.intValue(), loc); 140 } 141 142 case Types.TIMESTAMP: 143 { 144 Integer iDateStyle, iTimeStyle; 145 int pos = strFormat.indexOf (","); 146 147 if (pos == -1) { 148 if (!_mapDateTimeStyle.containsKey (strFormat)) 149 return new SimpleDateFormat (strFormat, loc); 150 151 iDateStyle = iTimeStyle = (Integer )_mapDateTimeStyle.get (strFormat); 152 } else { 153 String strDateStyle = strFormat.substring (0, pos); 154 String strTimeStyle = strFormat.substring (pos + 1); 155 156 if (!_mapDateTimeStyle.containsKey (strDateStyle) || !_mapDateTimeStyle.containsKey (strTimeStyle)) 157 return new SimpleDateFormat (strFormat, loc); 158 159 iDateStyle = (Integer )_mapDateTimeStyle.get (strDateStyle); 160 iTimeStyle = (Integer )_mapDateTimeStyle.get (strTimeStyle); 161 } 162 163 return DateFormat.getDateTimeInstance (iDateStyle.intValue (), iTimeStyle.intValue (), loc); 164 } 165 166 default: 167 throw new JspTagException ("Not a Date/Time JDBC type: " + nType); 168 } 169 } 170 171 172 } 173 | Popular Tags |