View Javadoc

1   /*
2    * Copyright  2004-2005 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 net.sf.asterisk.util;
18  
19  import java.util.Date;
20  
21  /***
22   * Utility class to obtain the current date and allows to override with a fixed value for testing.
23   * 
24   * @author srt
25   * @version $Id: DateUtil.java,v 1.1 2005/03/11 15:31:47 srt Exp $
26   */
27  public class DateUtil
28  {
29      private static Date currentDate;
30  
31      private DateUtil()
32      {
33      }
34  
35      /***
36       * If set to a non null value uses the date given as current date on calls to getDate(). Set to
37       * null to restore the normal behavior.
38       * 
39       * @param currentDate the date to return on calls to getDate() or <code>null</code> to return
40       * the real current date.
41       */
42      public static void overrideCurrentDate(Date currentDate)
43      {
44          DateUtil.currentDate = currentDate;
45      }
46  
47      /***
48       * Returns the real current date or the date set with overrideCurrentDate().
49       * 
50       * @return the real current date or the date set with overrideCurrentDate().
51       */
52      public static Date getDate()
53      {
54          if (currentDate != null)
55          {
56              return currentDate;
57          }
58          else
59          {
60              return new Date();
61          }
62      }
63  }