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