1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }