View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id$ 
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          //Parse year first
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          //Check to see if there is more of a date string to process
64          if (position.getIndex() < datetime.length()) {
65              date = month.parse(datetime, position);
66              
67              //If it was not parseable check to see if it is doy format otherw
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                      //Date was doy format. Set day of year and move on.
74                      workingCalendar.setTime(date);
75                      calendar.set(Calendar.DAY_OF_YEAR, workingCalendar.get(Calendar.DAY_OF_YEAR));
76                  }
77              } else {
78                  //Date was month based format. Set month.
79                  workingCalendar.setTime(date);
80                  calendar.set(Calendar.MONTH, workingCalendar.get(Calendar.MONTH));
81                  
82                  //Check to see if there is a day of the month present
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                          //Date had a month. Set month and move on.
89                          workingCalendar.setTime(date);
90                          calendar.set(Calendar.DAY_OF_MONTH, workingCalendar.get(Calendar.DAY_OF_MONTH));
91                      }
92                  }
93              }
94          }
95          
96          //Now we should be at the time string if there is any
97          //First will come the hours
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                 //Date had an hour. Set and move on
104                 workingCalendar.setTime(date);
105                 calendar.set(Calendar.HOUR_OF_DAY, workingCalendar.get(Calendar.HOUR_OF_DAY));
106             }
107         }
108         
109         //Check for minutes
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                 //Date had minutes. Set and move on
116                 workingCalendar.setTime(date);
117                 calendar.set(Calendar.MINUTE, workingCalendar.get(Calendar.MINUTE));
118             }
119         }
120         
121         //Check for seconds
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                 //Date had minutes. Set and move on
128                 workingCalendar.setTime(date);
129                 calendar.set(Calendar.SECOND, workingCalendar.get(Calendar.SECOND));
130             }
131         }
132             
133         //Check for milliseconds
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                 //Date had minutes. Set and move on
140                 workingCalendar.setTime(date);
141                 calendar.set(Calendar.MILLISECOND, workingCalendar.get(Calendar.MILLISECOND));
142             }
143         }
144         
145         //Have we parse the full date? If not throw an error
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         //Date is not actually calculated in a calendar until you make a call to getTime.
150         //Must check that date is fine.
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 }