1 7 package javax.swing.text; 8 9 import java.awt.event.*; 10 import java.text.*; 11 import java.util.*; 12 import javax.swing.*; 13 import javax.swing.text.*; 14 15 33 public class DateFormatter extends InternationalFormatter { 34 38 public DateFormatter() { 39 this(DateFormat.getDateInstance()); 40 } 41 42 48 public DateFormatter(DateFormat format) { 49 super(format); 50 setFormat(format); 51 } 52 53 63 public void setFormat(DateFormat format) { 64 super.setFormat(format); 65 } 66 67 72 private Calendar getCalendar() { 73 Format f = getFormat(); 74 75 if (f instanceof DateFormat) { 76 return ((DateFormat)f).getCalendar(); 77 } 78 return Calendar.getInstance(); 79 } 80 81 82 86 boolean getSupportsIncrement() { 87 return true; 88 } 89 90 93 Object getAdjustField(int start, Map attributes) { 94 Iterator attrs = attributes.keySet().iterator(); 95 96 while (attrs.hasNext()) { 97 Object key = attrs.next(); 98 99 if ((key instanceof DateFormat.Field) && 100 (key == DateFormat.Field.HOUR1 || 101 ((DateFormat.Field)key).getCalendarField() != -1)) { 102 return key; 103 } 104 } 105 return null; 106 } 107 108 112 Object adjustValue(Object value, Map attributes, Object key, 113 int direction) throws 114 BadLocationException , ParseException { 115 if (key != null) { 116 int field; 117 118 if (key == DateFormat.Field.HOUR1) { 121 key = DateFormat.Field.HOUR0; 122 } 123 field = ((DateFormat.Field)key).getCalendarField(); 124 125 Calendar calendar = getCalendar(); 126 127 if (calendar != null) { 128 calendar.setTime((Date)value); 129 130 int fieldValue = calendar.get(field); 131 132 try { 133 calendar.add(field, direction); 134 value = calendar.getTime(); 135 } catch (Throwable th) { 136 value = null; 137 } 138 return value; 139 } 140 } 141 return null; 142 } 143 } 144 | Popular Tags |