1 package com.quadcap.sql.types; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.text.SimpleDateFormat ; 47 48 import java.util.Calendar ; 49 import java.util.Date ; 50 51 import com.quadcap.sql.io.ExternalizeProxy; 52 53 58 public class ValueDate extends ValueDateTime implements Externalizable , ExternalizeProxy { 59 static SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); 60 61 public ValueDate() {} 62 63 public ValueDate(long val) { 64 super(val); 65 } 66 67 public ValueDate(String str) throws antlr.RecognitionException { 68 try { 69 synchronized (df) { 70 val = df.parse(str).getTime(); 71 } 72 } catch (Throwable e) { 73 throw new antlr.RecognitionException(e.toString()); 74 } 75 } 76 77 public String toString() { 78 synchronized (df) { 79 return df.format(new java.sql.Date (val)); 80 } 81 } 82 83 public Value binop(int op, ValueString r) throws ValueException { 84 try { 85 return ValueDateTime.binop(op, this, new ValueDate(r.val)); 86 } catch (antlr.RecognitionException e) { 87 throw new ValueException("Not a date: " + val); 88 } 89 } 90 91 public Value binop(int op, Value l) throws ValueException { 92 return l.binop(op, this); 93 } 94 95 public Value binop(int op, ValueInterval r) throws ValueException { 96 switch (op) { 97 case Op.PLUS: 98 if (r.ym) { 99 Calendar c = getCal(); 100 c.add(Calendar.MONTH, (int)r.val); 101 return new ValueDate(c.getTime().getTime()); 102 } else { 103 return new ValueDate(val + r.val); 104 } 105 case Op.MINUS: 106 if (r.ym) { 107 Calendar c = getCal(); 108 c.add(Calendar.MONTH, 0 - (int)r.val); 109 return new ValueDate(c.getTime().getTime()); 110 } else { 111 return new ValueDate(val - r.val); 112 } 113 default: 114 throw badBinop(op, r); 115 } 116 } 117 118 public Value unop(int op) throws ValueException { 119 switch (op) { 120 case Op.NULL: 121 return ValueBoolean.falseBoolean; 122 default: 123 throw new ValueException("Unary op: " + Op.toString(op) + 124 " not implemented for this type"); 125 } 126 } 127 128 public void readExternal(ObjectInput in) 129 throws IOException , ClassNotFoundException 130 { 131 super.readExternal(in); 132 } 133 134 public void writeExternal(ObjectOutput out) 135 throws IOException 136 { 137 super.writeExternal(out); 138 } 139 140 public Object readObject(ObjectInput in) 141 throws IOException , ClassNotFoundException 142 { 143 ValueDate v = new ValueDate(); 144 v.readExternal(in); 145 return v; 146 } 147 148 public void writeObject(ObjectOutput out, Object object) 149 throws IOException 150 { 151 ((ValueDate)object).writeExternal(out); 152 } 153 154 public Object asJavaObject() { 155 return new java.sql.Date (val); 156 } 157 158 public void fromJavaObject(Object obj) throws ValueException { 159 if (obj instanceof Date ) { 160 val = ((Date )obj).getTime(); 161 } else { 162 throw new ValueException("bad type: " + obj); 163 } 164 } 165 166 public Type getType() { 167 return TypeDate.typeDate; 168 } 169 170 public Value convert(TypeDate t) { 171 return this; 172 } 173 174 public Value convert(TypeTimestamp t) { 175 return new ValueTimestamp(val); 176 } 177 } 178 | Popular Tags |