1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.label;
17
18 import java.text.ParseException;
19 import java.text.ParsePosition;
20 import java.text.SimpleDateFormat;
21 import java.util.Calendar;
22 import java.util.Date;
23
24 /***
25 * @author pramirez
26 * @version $Revision$
27 *
28 */
29 public class DateTimeFormatter {
30 private static SimpleDateFormat year = new SimpleDateFormat("yyyy");
31 private static SimpleDateFormat month = new SimpleDateFormat("-MM");
32 private static SimpleDateFormat doy = new SimpleDateFormat("-DDD");
33 private static SimpleDateFormat day = new SimpleDateFormat("-dd");
34 private static SimpleDateFormat hour = new SimpleDateFormat("'T'HH");
35 private static SimpleDateFormat minute = new SimpleDateFormat(":mm");
36 private static SimpleDateFormat second = new SimpleDateFormat(":ss");
37 private static SimpleDateFormat millisecond = new SimpleDateFormat(".SSS");
38
39 static {
40 month.setLenient(false);
41 day.setLenient(false);
42 doy.setLenient(false);
43 hour.setLenient(false);
44 minute.setLenient(false);
45 second.setLenient(false);
46 millisecond.setLenient(false);
47 }
48
49 public static Date parse(String datetime) throws ParseException {
50 ParsePosition position = new ParsePosition(0);
51 Date date = null;
52 Calendar calendar = null;
53 Calendar workingCalendar = Calendar.getInstance();
54
55
56 date = year.parse(datetime, position);
57 if (date == null)
58 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
59
60 calendar = Calendar.getInstance();
61 calendar.setLenient(false);
62 calendar.setTime(date);
63
64 if (position.getIndex() < datetime.length()) {
65 date = month.parse(datetime, position);
66
67
68 if (date == null) {
69 date = doy.parse(datetime, position);
70 if (date == null)
71 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
72 else {
73
74 workingCalendar.setTime(date);
75 calendar.set(Calendar.DAY_OF_YEAR, workingCalendar.get(Calendar.DAY_OF_YEAR));
76 }
77 } else {
78
79 workingCalendar.setTime(date);
80 calendar.set(Calendar.MONTH, workingCalendar.get(Calendar.MONTH));
81
82
83 if (position.getIndex() < datetime.length()) {
84 date = day.parse(datetime, position);
85 if (date == null)
86 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
87 else {
88
89 workingCalendar.setTime(date);
90 calendar.set(Calendar.DAY_OF_MONTH, workingCalendar.get(Calendar.DAY_OF_MONTH));
91 }
92 }
93 }
94 }
95
96
97
98 if (position.getIndex() < datetime.length()) {
99 date = hour.parse(datetime, position);
100 if (date == null)
101 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
102 else {
103
104 workingCalendar.setTime(date);
105 calendar.set(Calendar.HOUR_OF_DAY, workingCalendar.get(Calendar.HOUR_OF_DAY));
106 }
107 }
108
109
110 if (position.getIndex() < datetime.length() && datetime.charAt(position.getIndex()) != 'Z') {
111 date = minute.parse(datetime, position);
112 if (date == null)
113 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
114 else {
115
116 workingCalendar.setTime(date);
117 calendar.set(Calendar.MINUTE, workingCalendar.get(Calendar.MINUTE));
118 }
119 }
120
121
122 if (position.getIndex() < datetime.length() && datetime.charAt(position.getIndex()) != 'Z') {
123 date = second.parse(datetime, position);
124 if (date == null)
125 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
126 else {
127
128 workingCalendar.setTime(date);
129 calendar.set(Calendar.SECOND, workingCalendar.get(Calendar.SECOND));
130 }
131 }
132
133
134 if (position.getIndex() < datetime.length() && datetime.charAt(position.getIndex()) != 'Z') {
135 date = millisecond.parse(datetime, position);
136 if (date == null)
137 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
138 else {
139
140 workingCalendar.setTime(date);
141 calendar.set(Calendar.MILLISECOND, workingCalendar.get(Calendar.MILLISECOND));
142 }
143 }
144
145
146 if (position.getIndex() < (datetime.length() - 2) || (position.getIndex() == (datetime.length() - 1) && datetime.charAt(position.getIndex()) != 'Z'))
147 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
148
149
150
151 Date returnDate = null;
152 try {
153 returnDate = calendar.getTime();
154 } catch (IllegalArgumentException ia) {
155 throw new ParseException("Could not create a date from " + datetime, position.getIndex());
156 }
157
158 return returnDate;
159 }
160 }