View Javadoc

1   /*
2    * Copyright 2004-2005 Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public 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.gnu.org/licenses/gpl.txt
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   */
17  package com.geekologue.md4j.util;
18  
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  /***
24   * Conversion utilities without checked exceptions
25   * 
26   * @author manos
27   * 
28   */
29  public class ConvertUtils {
30      /***
31       * This method attempts to parse the given string to a date
32       * 
33       * @param date
34       *            the date as a string
35       * @param pattern
36       *            the pattern to use for parsing the date
37       * @return the Date constructed by parsing the given string or null if the
38       *         string was null
39       * @throws ConversionException
40       *             upon failure to parse using the given pattern
41       */
42      public static final Date parseDate(String date, String pattern) {
43          Date d = null;
44          if (date != null) {
45              try {
46                  d = new SimpleDateFormat(pattern).parse(date);
47              } catch (ParseException e) {
48                  throw new ConversionException(
49                          "Could not parse to date, string: " + date
50                                  + ", pattern: " + pattern, e);
51              }
52          }
53          return (d);
54      }
55  
56      /***
57       * This method attempts to parse the given string to a date using MM/dd/yyyy
58       * as a pattern
59       * 
60       * @param date
61       *            the date as a string
62       * @return the Date constructed by parsing the given string or null if the
63       *         string was null
64       * @throws ConversionException
65       *             upon failure to parse using the default pattern
66       */
67      public static final Date parseDate(String date) {
68          return parseDate(date, "DD/mm/yyyy");
69      }
70  }