View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache 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.apache.org/licenses/LICENSE-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   */
17  package org.asteriskjava.util;
18  
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.TimeZone;
24  
25  /**
26   * Utility class to obtain the current date and allows to override with a fixed
27   * value for unit testing. Includes some convenience methods for date
28   * conversion.
29   * <p>
30   * Client code is not supposed to use this class.
31   * 
32   * @author srt
33   * @version $Id$
34   */
35  public class DateUtil
36  {
37      private static final String DATE_TIME_PATTERN = "yy-MM-dd HH:mm:ss";
38  
39      private static Date currentDate;
40  
41      private DateUtil()
42      {
43          // hide constructor
44      }
45  
46      /**
47       * If set to a non null value uses the date given as current date on calls
48       * to getDate(). Set to null to restore the normal behavior.
49       * 
50       * @param currentDate the date to return on calls to getDate() or
51       *            <code>null</code> to return the real current date.
52       */
53      public static void overrideCurrentDate(Date currentDate)
54      {
55          DateUtil.currentDate = currentDate;
56      }
57  
58      /**
59       * Returns the real current date or the date set with overrideCurrentDate().
60       * 
61       * @return the real current date or the date set with overrideCurrentDate().
62       */
63      public static Date getDate()
64      {
65          if (currentDate == null)
66          {
67              return new Date();
68          }
69          else
70          {
71              return currentDate;
72          }
73      }
74  
75      /**
76       * Converts a date in the form of "yy-MM-dd HH:mm:ss" to a Date object using
77       * the default time zone.
78       * 
79       * @param s date string in the form of "yy-MM-dd HH:mm:ss"
80       * @return the corresponding Java date object or <code>null</code> if it is not parsable.
81       */
82      public static Date parseDateTime(String s)
83      {
84          return parseDateTime(s, null);
85      }
86  
87      /**
88       * Converts a date in the form of "yy-MM-dd HH:mm:ss" to a Date object using
89       * the given time zone.
90       * 
91       * @param s date string in the form of "yy-MM-dd HH:mm:ss"
92       * @param tz the timezone to use or <code>null</code> for the default time zone.
93       * @return the corresponding Java date object or <code>null</code> if it is not parsable.
94       */
95      public static Date parseDateTime(String s, TimeZone tz)
96      {
97          DateFormat df;
98  
99          if (s == null)
100         {
101             return null;
102         }
103 
104         df = new SimpleDateFormat(DATE_TIME_PATTERN);
105         if (tz != null)
106         {
107             df.setTimeZone(tz);
108         }
109         try
110         {
111             return df.parse(s);
112         }
113         catch (ParseException e)
114         {
115             return null;
116         }
117     }
118 }