KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueDate


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.sql.Date JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.h2.message.Message;
12 import org.h2.util.DateTimeUtils;
13
14 /**
15  * @author Thomas
16  */

17 public class ValueDate extends Value {
18     public static final int PRECISION = 8;
19
20     private Date JavaDoc value;
21
22     private ValueDate(Date JavaDoc value) {
23         this.value = value;
24     }
25
26     public static Date JavaDoc parseDate(String JavaDoc s) throws SQLException JavaDoc {
27         return (Date JavaDoc) DateTimeUtils.parseDateTime(s, Value.DATE, Message.DATE_CONSTANT_1);
28     }
29
30     public Date JavaDoc getDate() {
31         // this class is mutable - must copy the object
32
return (Date JavaDoc)value.clone();
33     }
34     
35     public Date JavaDoc getDateNoCopy() {
36         return value;
37     }
38
39     public String JavaDoc 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 JavaDoc 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 JavaDoc getObject() {
66         // this class is mutable - must copy the object
67
return getDate();
68     }
69
70     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
71         prep.setDate(parameterIndex, value);
72     }
73
74     public static ValueDate get(Date JavaDoc date) {
75         date = DateTimeUtils.cloneAndNormalizeDate(date);
76         return getNoCopy(date);
77     }
78     
79     public static ValueDate getNoCopy(Date JavaDoc date) {
80         return (ValueDate) Value.cache(new ValueDate(date));
81     }
82
83 // public String getJavaString() {
84
// return "Date.valueOf(\"" + toString() + "\")";
85
// }
86

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