Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Utc |
|
| 1.2;1.2 | ||||
Utc$AjcClosure1 |
|
| 1.2;1.2 | ||||
Utc$AjcClosure3 |
|
| 1.2;1.2 | ||||
Utc$AjcClosure5 |
|
| 1.2;1.2 |
1 | 1 | /** |
2 | * Copyright (c) 2012-2015, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.jdbc; | |
31 | ||
32 | import com.jcabi.aspects.Immutable; | |
33 | import com.jcabi.aspects.Loggable; | |
34 | import java.sql.PreparedStatement; | |
35 | import java.sql.ResultSet; | |
36 | import java.sql.SQLException; | |
37 | import java.sql.Timestamp; | |
38 | import java.util.Calendar; | |
39 | import java.util.Date; | |
40 | import java.util.SimpleTimeZone; | |
41 | import lombok.EqualsAndHashCode; | |
42 | import lombok.ToString; | |
43 | ||
44 | /** | |
45 | * UTC time zone manipulator. | |
46 | * | |
47 | * <p>When it's necessary to save date/time to the DB in UTC timezone, use | |
48 | * this class: | |
49 | * | |
50 | * <pre> new JdbcSession(source) | |
51 | * .sql("INSERT INTO payment (amount, date) VALUES (?, ?)") | |
52 | * .set(500) | |
53 | * .set(new Utc()) // current date to be set, in UTC timezone | |
54 | * .insert(Outcome.VOID);</pre> | |
55 | * | |
56 | * <p>This class also helps during date/time retrieval: | |
57 | * | |
58 | * <pre> Date date = new JdbcSession(source) | |
59 | * .sql("SELECT date FROM payment WHERE id = 555") | |
60 | * .select( | |
61 | * new Outcome<Date>() { | |
62 | * @Override | |
63 | * public Date handle(final ResultSet rset) throws SQLException { | |
64 | * return Utc.getTimestamp(rset, 1); | |
65 | * } | |
66 | * } | |
67 | * );</pre> | |
68 | * | |
69 | * <p>{@link Timestamp} is used because {@link java.sql.Date} | |
70 | * supports only dates (without time). | |
71 | * | |
72 | * @author Yegor Bugayenko (yegor@teamed.io) | |
73 | * @version $Id: 488dc4183cc9a5a42fd1937b13c23b86db527370 $ | |
74 | * @since 0.1.8 | |
75 | */ | |
76 | @Immutable | |
77 | 2 | @ToString |
78 | 0 | @EqualsAndHashCode(of = "date") |
79 | public final class Utc { | |
80 | ||
81 | /** | |
82 | * The calendar to use. | |
83 | */ | |
84 | 1 | private static final Calendar CALENDAR = |
85 | Calendar.getInstance(new SimpleTimeZone(0, "UTC")); | |
86 | ||
87 | /** | |
88 | * The date to work with. | |
89 | */ | |
90 | private final transient long date; | |
91 | ||
92 | /** | |
93 | * Public ctor, with current date. | |
94 | */ | |
95 | public Utc() { | |
96 | 0 | this(new Date()); |
97 | 0 | } |
98 | ||
99 | /** | |
100 | * Public ctor. | |
101 | * @param when The date to use. | |
102 | */ | |
103 | 2 | public Utc(final Date when) { |
104 | 2 | this.date = when.getTime(); |
105 | 2 | } |
106 | ||
107 | /** | |
108 | * Get date that is encapsulated. | |
109 | * @return The date | |
110 | */ | |
111 | @Loggable(Loggable.DEBUG) | |
112 | public Date getDate() { | |
113 | 4 | return new Date(this.date); |
114 | } | |
115 | ||
116 | /** | |
117 | * Convert date to timestamp and save to the statement. | |
118 | * @param stmt The statement | |
119 | * @param pos Position in the statement | |
120 | * @throws SQLException If some SQL problem inside | |
121 | */ | |
122 | @Loggable(Loggable.DEBUG) | |
123 | public void setTimestamp(final PreparedStatement stmt, final int pos) | |
124 | throws SQLException { | |
125 | 4 | stmt.setTimestamp( |
126 | pos, | |
127 | new Timestamp(this.date), | |
128 | Utc.CALENDAR | |
129 | ); | |
130 | 2 | } |
131 | ||
132 | /** | |
133 | * Retrieve timestamp from the result set. | |
134 | * @param rset The result set | |
135 | * @param pos Position in the result set | |
136 | * @return The date | |
137 | * @throws SQLException If some SQL problem inside | |
138 | */ | |
139 | @Loggable(Loggable.DEBUG) | |
140 | public static Date getTimestamp(final ResultSet rset, final int pos) | |
141 | throws SQLException { | |
142 | 2 | final Timestamp stamp = rset.getTimestamp(pos, Utc.CALENDAR); |
143 | 1 | Date when = null; |
144 | 1 | if (stamp != null) { |
145 | 1 | when = new Date(stamp.getTime()); |
146 | } | |
147 | 1 | return when; |
148 | } | |
149 | ||
150 | } |