A renderer for Date and Calendar objects. It understands a variety of format names as shown in #formatToInt field. By default it assumes "short" format. A prefix of "date:" or "time:" shows only those components of the time object.
| 40 | |
| 41 | |
| 42 | public class DateRenderer implements AttributeRenderer { |
| 43 | public static final Map<String, Integer> formatToInt = new HashMap<String, Integer>() { |
| 44 | { |
| 45 | put("short", DateFormat.SHORT); |
| 46 | put("medium", DateFormat.MEDIUM); |
| 47 | put("long", DateFormat.LONG); |
| 48 | put("full", DateFormat.FULL); |
| 49 | put("date:short", DateFormat.SHORT); |
| 50 | put("date:medium", DateFormat.MEDIUM); |
| 51 | put("date:long", DateFormat.LONG); |
| 52 | put("date:full", DateFormat.FULL); |
| 53 | put("time:short", DateFormat.SHORT); |
| 54 | put("time:medium", DateFormat.MEDIUM); |
| 55 | put("time:long", DateFormat.LONG); |
| 56 | put("time:full", DateFormat.FULL); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | @Override |
| 61 | public String toString(Object o, String formatString, Locale locale) { |
| 62 | Date d; |
| 63 | if ( formatString==null ) formatString = "short"; |
| 64 | if ( o instanceof Calendar ) d = ((Calendar)o).getTime(); |
| 65 | else d = (Date)o; |
| 66 | Integer styleI = formatToInt.get(formatString); |
| 67 | DateFormat f; |
| 68 | if ( styleI==null ) f = new SimpleDateFormat(formatString, locale); |
| 69 | else { |
| 70 | int style = styleI.intValue(); |
| 71 | if ( formatString.startsWith("date:") ) f = DateFormat.getDateInstance(style, locale); |
| 72 | else if ( formatString.startsWith("time:") ) f = DateFormat.getTimeInstance(style, locale); |
| 73 | else f = DateFormat.getDateTimeInstance(style, style, locale); |
| 74 | } |
| 75 | return f.format(d); |
| 76 | } |
| 77 | } |