日历类

1 // 使用默认时区和语言环境获得一个日历
2     Calendar cal = Calendar.getInstance();
3 // 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1
4     System.out.println("年:" + cal.get(Calendar.YEAR));
5     System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));       
6     System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
7     System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));
8     System.out.println("分:" + cal.get(Calendar.MINUTE));
9     System.out.println("秒:" + cal.get(Calendar.SECOND));
cal.add(Calendar.SECOND, 1);//往后推一秒
cal.add(Calendar.DATE,-1);//往前推一天
cal.add(Calendar.MONTH, 1);//往后推一个月

常用方法:


  1 public class DatesUtil {
  2     // 获取当天的开始时间
  3     public static java.util.Date getDayBegin() {
  4         Calendar cal = new GregorianCalendar();
  5         cal.set(Calendar.HOUR_OF_DAY, 0);
  6         cal.set(Calendar.MINUTE, 0);
  7         cal.set(Calendar.SECOND, 0);
  8         cal.set(Calendar.MILLISECOND, 0);
  9         return cal.getTime();
 10     }
 11  
 12     // 获取当天的结束时间
 13     public static java.util.Date getDayEnd() {
 14         Calendar cal = new GregorianCalendar();
 15         cal.set(Calendar.HOUR_OF_DAY, 23);
 16         cal.set(Calendar.MINUTE, 59);
 17         cal.set(Calendar.SECOND, 59);
 18         return cal.getTime();
 19     }
 20  
 21     // 获取昨天的开始时间
 22     public static Date getBeginDayOfYesterday() {
 23         Calendar cal = new GregorianCalendar();
 24         cal.setTime(getDayBegin());
 25         cal.add(Calendar.DAY_OF_MONTH, -1);
 26         return cal.getTime();
 27     }
 28  
 29     // 获取昨天的结束时间
 30     public static Date getEndDayOfYesterDay() {
 31         Calendar cal = new GregorianCalendar();
 32         cal.setTime(getDayEnd());
 33         cal.add(Calendar.DAY_OF_MONTH, -1);
 34         return cal.getTime();
 35     }
 36  
 37     // 获取明天的开始时间
 38     public static Date getBeginDayOfTomorrow() {
 39         Calendar cal = new GregorianCalendar();
 40         cal.setTime(getDayBegin());
 41         cal.add(Calendar.DAY_OF_MONTH, 1);
 42         return cal.getTime();
 43     }
 44  
 45     // 获取明天的结束时间
 46     public static Date getEndDayOfTomorrow() {
 47         Calendar cal = new GregorianCalendar();
 48         cal.setTime(getDayEnd());
 49         cal.add(Calendar.DAY_OF_MONTH, 1);
 50         return cal.getTime();
 51     }
 52  
 53     // 获取本周的开始时间
 54     @SuppressWarnings("unused")
 55     public static Date getBeginDayOfWeek() {
 56         Date date = new Date();
 57         if (date == null) {
 58             return null;
 59         }
 60         Calendar cal = Calendar.getInstance();
 61         cal.setTime(date);
 62         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
 63         if (dayofweek == 1) {
 64             dayofweek += 7;
 65         }
 66         cal.add(Calendar.DATE, 2 - dayofweek);
 67         return getDayStartTime(cal.getTime());
 68     }
 69  
 70     // 获取本周的结束时间
 71     public static Date getEndDayOfWeek() {
 72         Calendar cal = Calendar.getInstance();
 73         cal.setTime(getBeginDayOfWeek());
 74         cal.add(Calendar.DAY_OF_WEEK, 6);
 75         Date weekEndSta = cal.getTime();
 76         return getDayEndTime(weekEndSta);
 77     }
 78  
 79     // 获取上周的开始时间
 80     @SuppressWarnings("unused")
 81     public static Date getBeginDayOfLastWeek() {
 82         Date date = new Date();
 83         if (date == null) {
 84             return null;
 85         }
 86         Calendar cal = Calendar.getInstance();
 87         cal.setTime(date);
 88         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
 89         if (dayofweek == 1) {
 90             dayofweek += 7;
 91         }
 92         cal.add(Calendar.DATE, 2 - dayofweek - 7);
 93         return getDayStartTime(cal.getTime());
 94     }
 95  
 96     // 获取上周的结束时间
 97     public static Date getEndDayOfLastWeek() {
 98         Calendar cal = Calendar.getInstance();
 99         cal.setTime(getBeginDayOfLastWeek());
100         cal.add(Calendar.DAY_OF_WEEK, 6);
101         Date weekEndSta = cal.getTime();
102         return getDayEndTime(weekEndSta);
103     }
104  
105     // 获取本月的开始时间
106     public static Date getBeginDayOfMonth() {
107         Calendar calendar = Calendar.getInstance();
108         calendar.set(getNowYear(), getNowMonth() - 1, 1);
109         return getDayStartTime(calendar.getTime());
110     }
111  
112     // 获取本月的结束时间
113     public static Date getEndDayOfMonth() {
114         Calendar calendar = Calendar.getInstance();
115         calendar.set(getNowYear(), getNowMonth() - 1, 1);
116         int day = calendar.getActualMaximum(5);
117         calendar.set(getNowYear(), getNowMonth() - 1, day);
118         return getDayEndTime(calendar.getTime());
119     }
120  
121     // 获取上月的开始时间
122     public static Date getBeginDayOfLastMonth() {
123         Calendar calendar = Calendar.getInstance();
124         calendar.set(getNowYear(), getNowMonth() - 2, 1);
125         return getDayStartTime(calendar.getTime());
126     }
127  
128     // 获取上月的结束时间
129     public static Date getEndDayOfLastMonth() {
130         Calendar calendar = Calendar.getInstance();
131         calendar.set(getNowYear(), getNowMonth() - 2, 1);
132         int day = calendar.getActualMaximum(5);
133         calendar.set(getNowYear(), getNowMonth() - 2, day);
134         return getDayEndTime(calendar.getTime());
135     }
136  
137     // 获取本年的开始时间
138     public static Date getBeginDayOfYear() {
139         Calendar cal = Calendar.getInstance();
140         cal.set(Calendar.YEAR, getNowYear());
141         cal.set(Calendar.MONTH, Calendar.JANUARY);
142         cal.set(Calendar.DATE, 1);
143         return getDayStartTime(cal.getTime());
144     }
145  
146     // 获取本年的结束时间
147     public static java.util.Date getEndDayOfYear() {
148         Calendar cal = Calendar.getInstance();
149         cal.set(Calendar.YEAR, getNowYear());
150         cal.set(Calendar.MONTH, Calendar.DECEMBER);
151         cal.set(Calendar.DATE, 31);
152         return getDayEndTime(cal.getTime());
153     }
154  
155     // 获取某个日期的开始时间
156     public static Timestamp getDayStartTime(Date d) {
157         Calendar calendar = Calendar.getInstance();
158         if (null != d)
159             calendar.setTime(d);
160         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
161                 calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
162         calendar.set(Calendar.MILLISECOND, 0);
163         return new Timestamp(calendar.getTimeInMillis());
164     }
165  
166     // 获取某个日期的结束时间
167     public static Timestamp getDayEndTime(Date d) {
168         Calendar calendar = Calendar.getInstance();
169         if (null != d)
170             calendar.setTime(d);
171         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
172                 calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
173         calendar.set(Calendar.MILLISECOND, 999);
174         return new Timestamp(calendar.getTimeInMillis());
175     }
176  
177     // 获取今年是哪一年
178     public static Integer getNowYear() {
179         Date date = new Date();
180         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
181         gc.setTime(date);
182         return Integer.valueOf(gc.get(1));
183     }
184  
185     // 获取本月是哪一月
186     public static int getNowMonth() {
187         Date date = new Date();
188         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
189         gc.setTime(date);
190         return gc.get(2) + 1;
191     }
192  
193     // 两个日期相减得到的天数
194     public static int getDiffDays(Date beginDate, Date endDate) {
195         if (beginDate == null || endDate == null) {
196             throw new IllegalArgumentException("getDiffDays param is null!");
197         }
198         long diff = (endDate.getTime() - beginDate.getTime())
199                 / (1000 * 60 * 60 * 24);
200         int days = new Long(diff).intValue();
201         return days;
202     }
203  
204     // 两个日期相减得到的毫秒数
205     public static long dateDiff(Date beginDate, Date endDate) {
206         long date1ms = beginDate.getTime();
207         long date2ms = endDate.getTime();
208         return date2ms - date1ms;
209     }
210  
211     // 获取两个日期中的最大日期
212     public static Date max(Date beginDate, Date endDate) {
213         if (beginDate == null) {
214             return endDate;
215         }
216         if (endDate == null) {
217             return beginDate;
218         }
219         if (beginDate.after(endDate)) {
220             return beginDate;
221         }
222         return endDate;
223     }
224  
225     // 获取两个日期中的最小日期
226     public static Date min(Date beginDate, Date endDate) {
227         if (beginDate == null) {
228             return endDate;
229         }
230         if (endDate == null) {
231             return beginDate;
232         }
233         if (beginDate.after(endDate)) {
234             return endDate;
235         }
236         return beginDate;
237     }
238  
239     // 返回某月该季度的第一个月
240     public static Date getFirstSeasonDate(Date date) {
241         final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
242         Calendar cal = Calendar.getInstance();
243         cal.setTime(date);
244         int sean = SEASON[cal.get(Calendar.MONTH)];
245         cal.set(Calendar.MONTH, sean * 3 - 3);
246         return cal.getTime();
247     }
248  
249     // 返回某个日期下几天的日期
250     public static Date getNextDay(Date date, int i) {
251         Calendar cal = new GregorianCalendar();
252         cal.setTime(date);
253         cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
254         return cal.getTime();
255     }
256  
257     // 返回某个日期前几天的日期
258     public static Date getFrontDay(Date date, int i) {
259         Calendar cal = new GregorianCalendar();
260         cal.setTime(date);
261         cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
262         return cal.getTime();
263     }
264  
265     // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
266     @SuppressWarnings({ "rawtypes", "unchecked" })
267     public static List getTimeList(int beginYear, int beginMonth, int endYear,
268             int endMonth, int k) {
269         List list = new ArrayList();
270         if (beginYear == endYear) {
271             for (int j = beginMonth; j <= endMonth; j++) {
272                 list.add(getTimeList(beginYear, j, k));
273             }
274         } else {
275             {
276                 for (int j = beginMonth; j < 12; j++) {
277                     list.add(getTimeList(beginYear, j, k));
278                 }
279                 for (int i = beginYear + 1; i < endYear; i++) {
280                     for (int j = 0; j < 12; j++) {
281                         list.add(getTimeList(i, j, k));
282                     }
283                 }
284                 for (int j = 0; j <= endMonth; j++) {
285                     list.add(getTimeList(endYear, j, k));
286                 }
287             }
288         }
289         return list;
290     }
291  
292     // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
293     @SuppressWarnings({ "unchecked", "rawtypes" })
294     public static List getTimeList(int beginYear, int beginMonth, int k) {
295         List list = new ArrayList();
296         Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
297         int max = begincal.getActualMaximum(Calendar.DATE);
298         for (int i = 1; i < max; i = i + k) {
299             list.add(begincal.getTime());
300             begincal.add(Calendar.DATE, k);
301         }
302         begincal = new GregorianCalendar(beginYear, beginMonth, max);
303         list.add(begincal.getTime());
304         return list;
305     }
306  
307 }

 

 
posted @ 2021-05-31 17:43  小拐  阅读(67)  评论(0)    收藏  举报