Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/util/DateUtil.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.util;
22  import java.sql.*;
23  
24  /**
25   * A utility class for use with dates.
26   *
27   * @author Chris Bitmead
28   * @created December 13, 2001
29   * @version 0.7
30   * @commentedby Antonio Petrelli
31   */
32  public class DateUtil {
33      /** A pseudo-date representing -infinity.
34       */    
35    static Date _minusInfinity;
36          /** A pseudo-date representing +infinity.
37           */        
38    static Date _plusInfinity;
39          /** Contains 29.
40           */        
41    final static int LEAP_DAYS_IN_FEB = 29;
42          /** Contains 1 (representing February).
43           */        
44    final static int FEB = 1;
45  
46    /**
47           * Return a date represting a long time in the past.
48           *
49           * @return -infinity date.
50           */
51    public static Date minusInfinity() {
52      init();
53      return _minusInfinity;
54    }
55  
56    /**
57           * Return a date representing a long time in the future. Sometimes people who
58           * don't like to use NULL values in databases use this as a kind of NULL
59           * value. Alternatively it can be sometimes useful in a SELECT to force use of
60           * an index.
61           *
62           * @return +infinity date.
63           */
64    public static Date plusInfinity() {
65      init();
66      return _plusInfinity;
67    }
68  
69    /**
70           * Return a Timestamp object representing the time now.
71           *
72           * @return Excuse me what time is it?
73           */
74    public static Timestamp timestampNow() {
75      return new Timestamp(new java.util.Date().getTime());
76    }
77  
78          /** Checks if a year is a leap year.
79           * @param year The year to check.
80           * @return <CODE>true</CODE>: the year is a leap year;
81           * <CODE>false</CODE>: the year is a normal year.
82           */        
83    public static boolean leapYear(int year) {
84      if (year % 400 == 0) {
85        return true;
86      } else if (year % 100 == 0) {
87        return false;
88      } else if (year % 4 == 0) {
89        return true;
90      } else {
91        return false;
92      }
93    }
94  
95          /** Returns the month size.
96           * @param month The month to check.
97           * @return The number of contained days.
98           */        
99    public static int monthSize(int month) {
100     final int monthSizes[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
101     return monthSizes[month];
102   }
103 
104         /** Returns the maximum value of days in a month.
105          * @param month The month to check.
106          * @return The maximum number of days contained in this month.
107          */        
108   public static int maxMonthSize(int month) {
109     if (month == FEB) {
110       return LEAP_DAYS_IN_FEB;
111     } else {
112       return monthSize(month);
113     }
114   }
115 
116   /**
117          * Returns the number of days in a month depending on the year.
118          * Month is zero based.
119          *
120          * @param month The month to check.
121          * @param year The year to use.
122          * @return The number of days in the month at a certain year.
123          */
124   public static int daysInMonth(int month, int year) {
125     if (month == FEB && leapYear(year)) {
126       // February 29th
127       return LEAP_DAYS_IN_FEB;
128     } else {
129       return monthSize(month);
130     }
131   }
132 
133   /**
134          * Checks if a date is vald.
135          * month is zero based. date is not.
136          *
137          * @param date The day in the month.
138          * @param month The month.
139          * @param year The year.
140          * @return <CODE>true</CODE>: the date is valid;
141          * <CODE>false</CODE>: the date is NOT valid.
142          */
143   public static boolean validDate(int date, int month, int year) {
144     return 0 < date && date <= daysInMonth(month, year);
145   }
146 
147   // Hmm. Visual age doesn't seem to call anonymous {} initialisers.
148   // So we have init.
149         /** Initializes...
150          */        
151   static void init() {
152     if (_minusInfinity == null) {
153       synchronized (DateUtil.class) {
154         if (_minusInfinity == null) {
155           java.util.Calendar cal = java.util.Calendar.getInstance();
156           // 0001/01/01
157           cal.set(0001, 0, 1);
158           java.util.Date d = cal.getTime();
159           _minusInfinity = new Date(d.getTime());
160           // 9999/12/31
161           cal.set(9999, 11, 31);
162           d = cal.getTime();
163           _plusInfinity = new Date(d.getTime());
164         }
165       }
166     }
167   }
168 }