1 5 package org.h2.value; 6 7 import java.sql.Date ; 8 import java.sql.PreparedStatement ; 9 import java.sql.SQLException ; 10 11 import org.h2.message.Message; 12 import org.h2.util.DateTimeUtils; 13 14 17 public class ValueDate extends Value { 18 public static final int PRECISION = 8; 19 20 private Date value; 21 22 private ValueDate(Date value) { 23 this.value = value; 24 } 25 26 public static Date parseDate(String s) throws SQLException { 27 return (Date ) DateTimeUtils.parseDateTime(s, Value.DATE, Message.DATE_CONSTANT_1); 28 } 29 30 public Date getDate() { 31 return (Date )value.clone(); 33 } 34 35 public Date getDateNoCopy() { 36 return value; 37 } 38 39 public String getSQL() { 40 return "DATE '" + getString() + "'"; 41 } 42 43 public int getType() { 44 return Value.DATE; 45 } 46 47 protected int compareSecure(Value o, CompareMode mode) { 48 ValueDate v = (ValueDate) o; 49 int c = value.compareTo(v.value); 50 return c == 0 ? 0 : (c < 0 ? -1 : 1); 51 } 52 53 public String getString() { 54 return value.toString(); 55 } 56 57 public long getPrecision() { 58 return PRECISION; 59 } 60 61 public int hashCode() { 62 return value.hashCode(); 63 } 64 65 public Object getObject() { 66 return getDate(); 68 } 69 70 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 71 prep.setDate(parameterIndex, value); 72 } 73 74 public static ValueDate get(Date date) { 75 date = DateTimeUtils.cloneAndNormalizeDate(date); 76 return getNoCopy(date); 77 } 78 79 public static ValueDate getNoCopy(Date date) { 80 return (ValueDate) Value.cache(new ValueDate(date)); 81 } 82 83 87 public int getDisplaySize() { 88 return "2001-01-01".length(); 89 } 90 91 protected boolean isEqual(Value v) { 92 return v instanceof ValueDate && value.equals(((ValueDate)v).value); 93 } 94 95 } 96 | Popular Tags |