1
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 package ca.uhn.hl7v2.model.primitive;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.GregorianCalendar;
32 import java.io.Serializable;
33
34 import ca.uhn.hl7v2.model.DataTypeException;
35 import ca.uhn.hl7v2.model.DataTypeUtil;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @SuppressWarnings("serial")
55 public class CommonDT implements Serializable {
56
57 private String value;
58 private int year;
59 private int month;
60 private int day;
61
62
63
64
65
66 public CommonDT() {
67
68 value = null;
69 year = 0;
70 month = 0;
71 day = 0;
72 }
73
74
75
76
77
78
79 public CommonDT(String val) throws DataTypeException {
80 this.setValue(val);
81 }
82
83
84
85
86
87
88
89
90
91 public void setValue(Calendar theCalendar) throws DataTypeException {
92 if (theCalendar == null) {
93 setValue((String)null);
94 return;
95 }
96
97 int yr = theCalendar.get(Calendar.YEAR);
98 int mnth = theCalendar.get(Calendar.MONTH) + 1;
99 int dy = theCalendar.get(Calendar.DATE);
100 setYearMonthDayPrecision(yr, mnth, dy);
101 }
102
103
104
105
106
107
108
109
110
111 public void setValue(Date theDate) throws DataTypeException {
112 if (theDate == null) {
113 setValue((String)null);
114 return;
115 }
116
117 Calendar calendar = Calendar.getInstance();
118 calendar.setTime(theDate);
119 setValue(calendar);
120 }
121
122
123
124
125
126
127 public Calendar getValueAsCalendar() {
128 Calendar retVal = Calendar.getInstance();
129 retVal.set(Calendar.DATE, getDay());
130 retVal.set(Calendar.MONTH, getMonth() - 1);
131 retVal.set(Calendar.YEAR, getYear());
132
133
134 retVal.set(Calendar.HOUR_OF_DAY, 0);
135 retVal.set(Calendar.MINUTE, 0);
136 retVal.set(Calendar.SECOND, 0);
137 retVal.set(Calendar.MILLISECOND, 0);
138
139 return retVal;
140 }
141
142
143
144
145
146
147 public Date getValueAsDate() {
148 return getValueAsCalendar().getTime();
149 }
150
151
152
153
154
155
156
157
158 public void setValue(String val) throws DataTypeException {
159
160 if (val != null && !val.equals("") && !val.equals("\"\"")){
161 try {
162 GregorianCalendar cal = new GregorianCalendar();
163 cal.clear();
164 cal.setLenient(false);
165
166
167 if ((val.length() != 4) && (val.length() != 6) && (val.length() != 8)) {
168 String msg =
169 "The length of the DT datatype value does not conform to an allowable"
170 + " format. Format should conform to YYYY[MM[DD]]";
171 throw new DataTypeException(msg);
172 }
173
174 if (val.length() >= 4) {
175
176 int yrInt = Integer.parseInt(val.substring(0, 4));
177
178
179 cal.set(yrInt, Calendar.JANUARY, 1);
180 cal.getTime();
181 year = yrInt;
182 }
183
184 if (val.length() >= 6) {
185
186 int mnthInt = Integer.parseInt(val.substring(4, 6));
187
188
189 cal.set(year, mnthInt - 1, 1);
190 cal.getTime();
191 month = mnthInt;
192
193 }
194
195 if (val.length() == 8) {
196
197 int dayInt = Integer.parseInt(val.substring(6, 8));
198
199
200
201 cal.set(year, month - 1, dayInt);
202 cal.getTime();
203 day = dayInt;
204 }
205
206 value = val;
207 }
208
209 catch (DataTypeException e) {
210 throw e;
211 }
212
213 catch (Exception e) {
214 throw new DataTypeException( e );
215 }
216 }
217 else {
218
219 value = val;
220 }
221
222 }
223
224
225
226
227
228
229 public void setYearPrecision(int yr) throws DataTypeException {
230 try {
231 GregorianCalendar cal = new GregorianCalendar();
232 cal.clear();
233 cal.setLenient(false);
234
235
236 if (Integer.toString(yr).length() != 4) {
237 String msg = "The input year value must be four digits long";
238 throw new DataTypeException(msg);
239 }
240
241
242 cal.set(yr, Calendar.JANUARY, 1);
243 cal.getTime();
244 year = yr;
245 month = 0;
246 day = 0;
247 value = Integer.toString(yr);
248 }
249
250 catch (DataTypeException e) {
251 throw e;
252 }
253
254 catch (Exception e) {
255 throw new DataTypeException( e );
256 }
257
258 }
259
260
261
262
263
264
265
266 public void setYearMonthPrecision(int yr, int mnth) throws DataTypeException {
267 try {
268 GregorianCalendar cal = new GregorianCalendar();
269 cal.clear();
270 cal.setLenient(false);
271
272 if (Integer.toString(yr).length() != 4) {
273 String msg = "The input year value must be four digits long";
274 throw new DataTypeException(msg);
275 }
276
277
278 cal.set(yr, (mnth - 1), 1);
279 cal.getTime();
280 year = yr;
281 month = mnth;
282 day = 0;
283 value = yr + DataTypeUtil.preAppendZeroes(mnth, 2);
284 }
285
286 catch (DataTypeException e) {
287 throw e;
288 }
289
290 catch (Exception e) {
291 throw new DataTypeException( e );
292 }
293 }
294
295
296
297
298
299
300 public void setYearMonthDayPrecision(int yr, int mnth, int dy) throws DataTypeException {
301 try {
302 GregorianCalendar cal = new GregorianCalendar();
303 cal.clear();
304 cal.setLenient(false);
305
306
307 if (Integer.toString(yr).length() != 4) {
308 String msg = "The input year value must be four digits long";
309 throw new DataTypeException(msg);
310 }
311
312 cal.set(yr, (mnth - 1), dy);
313 cal.getTime();
314 year = yr;
315 month = mnth;
316 day = dy;
317 value = yr + DataTypeUtil.preAppendZeroes(mnth, 2) + DataTypeUtil.preAppendZeroes(dy, 2);
318 }
319
320 catch (DataTypeException e) {
321 throw e;
322 }
323
324 catch (Exception e) {
325 throw new DataTypeException( e );
326 }
327
328 }
329
330
331
332
333 public String getValue() {
334 return value;
335 }
336
337
338
339
340 public int getYear() {
341 return year;
342 }
343
344
345
346
347 public int getMonth() {
348 return month;
349 }
350
351
352
353
354 public int getDay() {
355 return day;
356 }
357
358
359
360
361
362
363 public static String toHl7DTFormat(GregorianCalendar cal) throws DataTypeException {
364 String val;
365 try {
366
367
368 cal.setLenient(false);
369 int calYear = cal.get(GregorianCalendar.YEAR);
370 int calMonth = cal.get(GregorianCalendar.MONTH) + 1;
371 int calDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
372 CommonDTitive/CommonDT.html#CommonDT">CommonDT dt = new CommonDT();
373 dt.setYearMonthDayPrecision(calYear, calMonth, calDay);
374 val = dt.getValue();
375 }
376
377 catch (DataTypeException e) {
378 throw e;
379 }
380
381 catch (Exception e) {
382 throw new DataTypeException( e );
383 }
384 return val;
385 }
386
387 }