File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueDate.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.Date;
10 import java.sql.PreparedStatement;
11 import java.sql.SQLException;
12 import org.h2.constant.ErrorCode;
13 import org.h2.message.DbException;
14 import org.h2.util.DateTimeUtils;
15 import org.h2.util.MathUtils;
16 import org.h2.util.StringUtils;
17
18 /**
19 * Implementation of the DATE data type.
20 */
21 public class ValueDate extends Value {
22
23 /**
24 * The precision in digits.
25 */
26 public static final int PRECISION = 8;
27
28 /**
29 * The display size of the textual representation of a date.
30 * Example: 2000-01-02
31 */
32 static final int DISPLAY_SIZE = 10;
33
34 private final long dateValue;
35
36 private ValueDate(long dateValue) {
37 this.dateValue = dateValue;
38 }
39
40 /**
41 * Get or create a date value for the given date.
42 *
43 * @param dateValue the date value
44 * @return the value
45 */
46 public static ValueDate fromDateValue(long dateValue) {
47 return (ValueDate) Value.cache(new ValueDate(dateValue));
48 }
49
50 /**
51 * Get or create a date value for the given date.
52 *
53 * @param date the date
54 * @return the value
55 */
56 public static ValueDate get(Date date) {
57 return fromDateValue(DateTimeUtils.dateValueFromDate(date.getTime()));
58 }
59
60 /**
61 * Parse a string to a ValueDate.
62 *
63 * @param s the string to parse
64 * @return the date
65 */
66 public static ValueDate parse(String s) {
67 try {
68 return fromDateValue(DateTimeUtils.parseDateValue(s, 0, s.length()));
69 } catch (Exception e) {
70 throw DbException.get(ErrorCode.INVALID_DATETIME_CONSTANT_2,
71 e, "DATE", s);
72 }
73 }
74
75 public long getDateValue() {
76 return dateValue;
77 }
78
79 public Date getDate() {
80 return DateTimeUtils.convertDateValueToDate(dateValue);
81 }
82
83 public int getType() {
84 return Value.DATE;
85 }
86
87 public String getString() {
88 StringBuilder buff = new StringBuilder(DISPLAY_SIZE);
89 appendDate(buff, dateValue);
90 return buff.toString();
91 }
92
93 public String getSQL() {
94 return "DATE '" + getString() + "'";
95 }
96
97 public long getPrecision() {
98 return PRECISION;
99 }
100
101 public int getDisplaySize() {
102 return DISPLAY_SIZE;
103 }
104
105 protected int compareSecure(Value o, CompareMode mode) {
106 return MathUtils.compareLong(dateValue, ((ValueDate) o).dateValue);
107 }
108
109 public boolean equals(Object other) {
110 if (this == other) {
111 return true;
112 }
113 return other instanceof ValueDate && dateValue == (((ValueDate) other).dateValue);
114 }
115
116 public int hashCode() {
117 return (int) (dateValue ^ (dateValue >>> 32));
118 }
119
120 public Object getObject() {
121 return getDate();
122 }
123
124 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
125 prep.setDate(parameterIndex, getDate());
126 }
127
128 /**
129 * Append a date to the string builder.
130 *
131 * @param buff the target string builder
132 * @param dateValue the date value
133 */
134 static void appendDate(StringBuilder buff, long dateValue) {
135 int y = DateTimeUtils.yearFromDateValue(dateValue);
136 int m = DateTimeUtils.monthFromDateValue(dateValue);
137 int d = DateTimeUtils.dayFromDateValue(dateValue);
138 if (y > 0 && y < 10000) {
139 StringUtils.appendZeroPadded(buff, 4, y);
140 } else {
141 buff.append(y);
142 }
143 buff.append('-');
144 StringUtils.appendZeroPadded(buff, 2, m);
145 buff.append('-');
146 StringUtils.appendZeroPadded(buff, 2, d);
147 }
148
149 }
150