1 package fr.ifremer.quadrige2.core.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import com.google.common.base.Preconditions;
29 import com.google.common.base.Splitter;
30 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.version.Version;
35 import org.nuiton.version.Versions;
36
37 import java.text.SimpleDateFormat;
38 import java.util.Calendar;
39 import java.util.Date;
40 import java.util.TimeZone;
41
42
43
44
45 public class DateVersions {
46
47
48
49
50 protected DateVersions() {
51
52 }
53
54 private static final Log log = LogFactory.getLog(DateVersions.class);
55
56
57
58
59
60
61
62
63
64 public static Date convertVersion2Date(Version version) {
65 Preconditions.checkNotNull(version);
66 return convertVersion2Date(version.toString(), null);
67 }
68
69
70
71
72
73
74
75
76
77
78 public static Date convertVersion2Date(String version, TimeZone timezone) {
79 Preconditions.checkNotNull(version);
80
81 String[] versionParts = Splitter.on('.').splitToList(version).toArray(new String[4]);
82
83 Calendar result = Calendar.getInstance(timezone != null ? timezone : TimeZone.getDefault());
84
85 boolean parseError = false;
86 try {
87
88 int year = Integer.parseInt(versionParts[0]);
89 result.set(Calendar.YEAR, year);
90
91
92 int month = Integer.parseInt(versionParts[1]);
93 result.set(Calendar.MONTH, month - 1);
94
95
96 int day = Integer.parseInt(versionParts[2]);
97 result.set(Calendar.DAY_OF_MONTH, day);
98
99 if (StringUtils.isBlank(versionParts[3])) {
100 result.set(Calendar.SECOND, 0);
101 result.set(Calendar.MINUTE, 0);
102 result.set(Calendar.HOUR_OF_DAY, 0);
103 }
104 else {
105 String time = versionParts[3];
106 if (time.length() > 6) {
107 parseError = true;
108 }
109 else {
110 time = StringUtils.leftPad(time, 6, '0');
111
112
113 int seconds = Integer.parseInt(time.substring(time.length() - 2));
114 result.set(Calendar.SECOND, seconds);
115
116
117 int minutes = Integer.parseInt(time.substring(time.length() - 4, time.length() - 2));
118 result.set(Calendar.MINUTE, minutes);
119
120
121 int hours = Integer.parseInt(time.substring(0, time.length() - 4));
122 result.set(Calendar.HOUR_OF_DAY, hours);
123 }
124 }
125
126
127 result.set(Calendar.MILLISECOND, 0);
128
129 } catch (Exception ex) {
130 parseError = true;
131 }
132 if (parseError) {
133 throw new Quadrige2TechnicalException(String.format("the database version '%s' can't be converted to a synchronization date", version));
134 }
135
136 return result.getTime();
137 }
138
139
140
141
142
143
144
145
146
147
148 public static Date safeConvertVersion2Date(String version, TimeZone timezone) {
149 if (StringUtils.isBlank(version)) {
150 return null;
151 }
152 try {
153 return convertVersion2Date(version, timezone);
154 } catch (Quadrige2TechnicalException e) {
155 log.error(e.getMessage(), e);
156 }
157 return null;
158 }
159
160
161
162
163
164
165
166
167
168 public static Date safeConvertVersion2Date(String version) {
169 return convertVersion2Date(version, null);
170 }
171
172
173
174
175
176
177
178 public static Version convertDate2Version(Date date) {
179 return convertDate2Version(date, null);
180 }
181
182
183
184
185
186
187
188 public static Version convertDate2Version(Date date, TimeZone timezone) {
189 Preconditions.checkNotNull(date);
190
191 SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HHmmss");
192 if (timezone != null) formatter.setTimeZone(timezone);
193 String dateStr = formatter.format(date);
194 return Versions.valueOf(dateStr);
195 }
196 }