001 /*
002 * $Id: DateUtil.java,v 1.3 2012/02/19 17:35:46 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2012
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 *
012 * All Rights Reserved
013 *
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.
016 *
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 *
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
025 * GNU Lesser Public License for more details.
026 *
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program. If not, see http://www.gnu.org/licenses.
029 */
030
031 package edu.wisc.ssec.mcidasv.data.dateChooser;
032
033 import java.util.Calendar;
034 import java.util.Date;
035
036 /**
037 * A utility class for some date operations.
038 *
039 * @author Kai Toedter
040 * @version $LastChangedRevision: 95 $
041 * @version $LastChangedDate: 2006-05-05 18:43:15 +0200 (Fr, 05 Mai 2006) $
042 */
043 public class DateUtil {
044 protected Date minSelectableDate;
045
046 protected Date maxSelectableDate;
047
048 protected Date defaultMinSelectableDate;
049
050 protected Date defaultMaxSelectableDate;
051
052 public DateUtil() {
053 Calendar tmpCalendar = Calendar.getInstance();
054 tmpCalendar.set(1, 0, 1, 1, 1);
055 defaultMinSelectableDate = tmpCalendar.getTime();
056 minSelectableDate = defaultMinSelectableDate;
057 tmpCalendar.set(9999, 0, 1, 1, 1);
058 defaultMaxSelectableDate = tmpCalendar.getTime();
059 maxSelectableDate = defaultMaxSelectableDate;
060 }
061
062 /**
063 * Sets a valid date range for selectable dates. If max is before min, the
064 * default range with no limitation is set.
065 *
066 * @param min
067 * the minimum selectable date or null (then the minimum date is
068 * set to 01\01\0001)
069 * @param max
070 * the maximum selectable date or null (then the maximum date is
071 * set to 01\01\9999)
072 */
073 public void setSelectableDateRange(Date min, Date max) {
074 if (min == null) {
075 minSelectableDate = defaultMinSelectableDate;
076 } else {
077 minSelectableDate = min;
078 }
079 if (max == null) {
080 maxSelectableDate = defaultMaxSelectableDate;
081 } else {
082 maxSelectableDate = max;
083 }
084 if (maxSelectableDate.before(minSelectableDate)) {
085 minSelectableDate = defaultMinSelectableDate;
086 maxSelectableDate = defaultMaxSelectableDate;
087 }
088 }
089
090 /**
091 * Sets the maximum selectable date. If null, the date 01\01\9999 will be set instead.
092 *
093 * @param max the maximum selectable date
094 *
095 * @return the maximum selectable date
096 */
097 public Date setMaxSelectableDate(Date max) {
098 if (max == null) {
099 maxSelectableDate = defaultMaxSelectableDate;
100 } else {
101 maxSelectableDate = max;
102 }
103 return maxSelectableDate;
104 }
105
106 /**
107 * Sets the minimum selectable date. If null, the date 01\01\0001 will be set instead.
108 *
109 * @param min the minimum selectable date
110 *
111 * @return the minimum selectable date
112 */
113 public Date setMinSelectableDate(Date min) {
114 if (min == null) {
115 minSelectableDate = defaultMinSelectableDate;
116 } else {
117 minSelectableDate = min;
118 }
119 return minSelectableDate;
120 }
121
122 /**
123 * Gets the maximum selectable date.
124 *
125 * @return the maximum selectable date
126 */
127 public Date getMaxSelectableDate() {
128 return maxSelectableDate;
129 }
130
131 /**
132 * Gets the minimum selectable date.
133 *
134 * @return the minimum selectable date
135 */
136 public Date getMinSelectableDate() {
137 return minSelectableDate;
138 }
139
140 /**
141 * Checks a given date if it is in the formally specified date range.
142 *
143 * @param date
144 * the date to check
145 * @return true, if the date is within minSelectableDate and
146 * maxSelectableDate
147 */
148 public boolean checkDate(Date date) {
149 Calendar calendar = Calendar.getInstance();
150 calendar.setTime(date);
151 calendar.set(Calendar.HOUR_OF_DAY, 0);
152 calendar.set(Calendar.MINUTE, 0);
153 calendar.set(Calendar.SECOND, 0);
154 calendar.set(Calendar.MILLISECOND, 0);
155
156 Calendar minCal = Calendar.getInstance();
157 minCal.setTime(minSelectableDate);
158 minCal.set(Calendar.HOUR_OF_DAY, 0);
159 minCal.set(Calendar.MINUTE, 0);
160 minCal.set(Calendar.SECOND, 0);
161 minCal.set(Calendar.MILLISECOND, 0);
162
163 Calendar maxCal = Calendar.getInstance();
164 maxCal.setTime(maxSelectableDate);
165 maxCal.set(Calendar.HOUR_OF_DAY, 0);
166 maxCal.set(Calendar.MINUTE, 0);
167 maxCal.set(Calendar.SECOND, 0);
168 maxCal.set(Calendar.MILLISECOND, 0);
169
170 return !(calendar.before(minCal) || calendar.after(maxCal));
171 }
172
173 }