View Javadoc

1   /*
2    * Copyright 2010 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.model.util;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import org.kuali.student.contract.exception.DictionaryExecutionException;
24  
25  /**
26   *
27   * @author nwright
28   */
29  public class DateUtility {
30  
31      public String asYMD(String date)
32              throws ParseException {
33          if (date == null) {
34              return null;
35          }
36          return asYMD(asDate(date));
37      }
38  
39      public Date asDate(String date)
40              throws ParseException {
41          if (date == null) {
42              return null;
43          }
44          String[] formats = {
45              "yyyy-MM-dd",
46              "MM/dd/yyyy"
47          };
48          ParseException pe = null;
49          for (int i = 0; i < formats.length; i++) {
50              DateFormat df = new SimpleDateFormat(formats[i]);
51              try {
52                  return df.parse(date);
53              } catch (ParseException e) {
54                  pe = e;
55              }
56          }
57          throw pe;
58      }
59  
60      public String asYMD(Date date) {
61          if (date == null) {
62              return null;
63          }
64          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
65          return df.format(date);
66      }
67  }