View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.validator;
17  
18  import java.util.Date;
19  
20  import org.kuali.student.common.validator.DateParseException;
21  import org.kuali.student.common.validator.DateParser;
22  
23  import com.google.gwt.i18n.client.DateTimeFormat;
24  
25  public class ClientDateParser implements DateParser {
26      DateTimeFormat[] formats = {DateTimeFormat.getFormat("yyyy-MM-dd"), DateTimeFormat.getFormat("yyyy-MM-ddTHH:mm:ss,SSS")};
27      
28      public Date parseDate(String input) {
29          Date result = null;
30          
31          for (DateTimeFormat format : formats) {
32                  try {
33                      result = format.parseStrict(input);
34                  } catch (IllegalArgumentException e) {
35                      // just eat it
36                  }
37                  if (result != null) {
38                      break;
39                  }
40              
41          }
42          
43          if (result == null) {
44              throw new DateParseException("Invalid date value: " + input);
45          }
46          
47          return result;
48      }
49      
50      public String toString(Date date){
51          String result = null;
52          DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-ddTHH:mm:ss,SSS");
53          result = format.format(date);
54  
55          return result;        
56      }
57      
58      
59  }