Java中对日期的常用处理
下面的DateUtil类为笔者在以前项目中用到的对日期处理的公用方法,请看到这个类的有兴趣的读者测试,如有BUG请留言,笔者也会在以后的项目中积累完善并更新到此类中去。
1
import
java.text.SimpleDateFormat;
2 import java.util.Calendar;
3 import java.util.Date;
4 import java.util.GregorianCalendar;
5
6 /** */ /**
7 *
8 * @author zzk
9 *
10 */
11 public class DateUtil {
12
13 /** *//**
14 * 取得某天相加(减)後的那一天
15 *
16 * @param date
17 * @param num(可正可负)
18 * @return
19 */
20 public static Date getAnotherDate(Date date, int num) {
21 Calendar c = Calendar.getInstance();
22 c.setTime(date);
23 c.add(Calendar.DAY_OF_YEAR, num);
24 return c.getTime();
25 }
26
27 /** *//**
28 * 取得某月的的最后一天
29 *
30 * @param year
31 * @param month
32 * @return
33 */
34 public static Date getLastDayOfMonth(int year, int month) {
35 Calendar cal = Calendar.getInstance();
36 cal.set(Calendar.YEAR, year);// 年
37 cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
38 cal.set(Calendar.DATE, 1);// 日,设为一号
39 cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号
40 cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
41 return cal.getTime();// 获得月末是几号
42 }
43
44 /** *//**
45 * 取得某天是一年中的多少周
46 *
47 * @param date
48 * @return
49 */
50 public static int getWeekOfYear(Date date) {
51 Calendar c = new GregorianCalendar();
52 c.setFirstDayOfWeek(Calendar.MONDAY);
53 c.setMinimalDaysInFirstWeek(7);
54 c.setTime(date);
55 return c.get(Calendar.WEEK_OF_YEAR);
56 }
57
58 /** *//**
59 * 取得某天所在周的第一天
60 *
61 * @param date
62 * @return
63 */
64 public static Date getFirstDayOfWeek(Date date) {
65 Calendar c = new GregorianCalendar();
66 c.setFirstDayOfWeek(Calendar.MONDAY);
67 c.setTime(date);
68 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
69 return c.getTime();
70 }
71
72 /** *//**
73 * 取得某天所在周的最后一天
74 *
75 * @param date
76 * @return
77 */
78 public static Date getLastDayOfWeek(Date date) {
79 Calendar c = new GregorianCalendar();
80 c.setFirstDayOfWeek(Calendar.MONDAY);
81 c.setTime(date);
82 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
83 return c.getTime();
84 }
85
86 /** *//**
87 * 取得某一年共有多少周
88 *
89 * @param year
90 * @return
91 */
92 public static int getMaxWeekNumOfYear(int year) {
93 Calendar c = new GregorianCalendar();
94 c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
95 return getWeekOfYear(c.getTime());
96 }
97
98 /** *//**
99 * 取得某年某周的第一天
100 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
101 * @param year
102 * @param week
103 * @return
104 */
105 public static Date getFirstDayOfWeek(int year, int week) {
106 Calendar calFirst = Calendar.getInstance();
107 calFirst.set(year, 0, 7);
108 Date firstDate = getFirstDayOfWeek(calFirst.getTime());
109
110 Calendar firstDateCal = Calendar.getInstance();
111 firstDateCal.setTime(firstDate);
112
113 Calendar c = new GregorianCalendar();
114 c.set(Calendar.YEAR, year);
115 c.set(Calendar.MONTH, Calendar.JANUARY);
116 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
117
118 Calendar cal = (GregorianCalendar) c.clone();
119 cal.add(Calendar.DATE, (week - 1) * 7);
120 firstDate = getFirstDayOfWeek(cal.getTime());
121
122 return firstDate;
123 }
124
125 /** *//**
126 * 取得某年某周的最后一天
127 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-04为2008年最后一周的最后一天
128 * @param year
129 * @param week
130 * @return
131 */
132 public static Date getLastDayOfWeek(int year, int week) {
133 Calendar calLast = Calendar.getInstance();
134 calLast.set(year, 0, 7);
135 Date firstDate = getLastDayOfWeek(calLast.getTime());
136
137 Calendar firstDateCal = Calendar.getInstance();
138 firstDateCal.setTime(firstDate);
139
140 Calendar c = new GregorianCalendar();
141 c.set(Calendar.YEAR, year);
142 c.set(Calendar.MONTH, Calendar.JANUARY);
143 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
144
145 Calendar cal = (GregorianCalendar) c.clone();
146 cal.add(Calendar.DATE, (week - 1) * 7);
147 Date lastDate = getLastDayOfWeek(cal.getTime());
148
149 return lastDate;
150 }
151}
152
2 import java.util.Calendar;
3 import java.util.Date;
4 import java.util.GregorianCalendar;
5
6 /** */ /**
7 *
8 * @author zzk
9 *
10 */
11 public class DateUtil {
12
13 /** *//**
14 * 取得某天相加(减)後的那一天
15 *
16 * @param date
17 * @param num(可正可负)
18 * @return
19 */
20 public static Date getAnotherDate(Date date, int num) {
21 Calendar c = Calendar.getInstance();
22 c.setTime(date);
23 c.add(Calendar.DAY_OF_YEAR, num);
24 return c.getTime();
25 }
26
27 /** *//**
28 * 取得某月的的最后一天
29 *
30 * @param year
31 * @param month
32 * @return
33 */
34 public static Date getLastDayOfMonth(int year, int month) {
35 Calendar cal = Calendar.getInstance();
36 cal.set(Calendar.YEAR, year);// 年
37 cal.set(Calendar.MONTH, month - 1);// 月,因为Calendar里的月是从0开始,所以要减1
38 cal.set(Calendar.DATE, 1);// 日,设为一号
39 cal.add(Calendar.MONTH, 1);// 月份加一,得到下个月的一号
40 cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
41 return cal.getTime();// 获得月末是几号
42 }
43
44 /** *//**
45 * 取得某天是一年中的多少周
46 *
47 * @param date
48 * @return
49 */
50 public static int getWeekOfYear(Date date) {
51 Calendar c = new GregorianCalendar();
52 c.setFirstDayOfWeek(Calendar.MONDAY);
53 c.setMinimalDaysInFirstWeek(7);
54 c.setTime(date);
55 return c.get(Calendar.WEEK_OF_YEAR);
56 }
57
58 /** *//**
59 * 取得某天所在周的第一天
60 *
61 * @param date
62 * @return
63 */
64 public static Date getFirstDayOfWeek(Date date) {
65 Calendar c = new GregorianCalendar();
66 c.setFirstDayOfWeek(Calendar.MONDAY);
67 c.setTime(date);
68 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
69 return c.getTime();
70 }
71
72 /** *//**
73 * 取得某天所在周的最后一天
74 *
75 * @param date
76 * @return
77 */
78 public static Date getLastDayOfWeek(Date date) {
79 Calendar c = new GregorianCalendar();
80 c.setFirstDayOfWeek(Calendar.MONDAY);
81 c.setTime(date);
82 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
83 return c.getTime();
84 }
85
86 /** *//**
87 * 取得某一年共有多少周
88 *
89 * @param year
90 * @return
91 */
92 public static int getMaxWeekNumOfYear(int year) {
93 Calendar c = new GregorianCalendar();
94 c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
95 return getWeekOfYear(c.getTime());
96 }
97
98 /** *//**
99 * 取得某年某周的第一天
100 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
101 * @param year
102 * @param week
103 * @return
104 */
105 public static Date getFirstDayOfWeek(int year, int week) {
106 Calendar calFirst = Calendar.getInstance();
107 calFirst.set(year, 0, 7);
108 Date firstDate = getFirstDayOfWeek(calFirst.getTime());
109
110 Calendar firstDateCal = Calendar.getInstance();
111 firstDateCal.setTime(firstDate);
112
113 Calendar c = new GregorianCalendar();
114 c.set(Calendar.YEAR, year);
115 c.set(Calendar.MONTH, Calendar.JANUARY);
116 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
117
118 Calendar cal = (GregorianCalendar) c.clone();
119 cal.add(Calendar.DATE, (week - 1) * 7);
120 firstDate = getFirstDayOfWeek(cal.getTime());
121
122 return firstDate;
123 }
124
125 /** *//**
126 * 取得某年某周的最后一天
127 * 对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-04为2008年最后一周的最后一天
128 * @param year
129 * @param week
130 * @return
131 */
132 public static Date getLastDayOfWeek(int year, int week) {
133 Calendar calLast = Calendar.getInstance();
134 calLast.set(year, 0, 7);
135 Date firstDate = getLastDayOfWeek(calLast.getTime());
136
137 Calendar firstDateCal = Calendar.getInstance();
138 firstDateCal.setTime(firstDate);
139
140 Calendar c = new GregorianCalendar();
141 c.set(Calendar.YEAR, year);
142 c.set(Calendar.MONTH, Calendar.JANUARY);
143 c.set(Calendar.DATE, firstDateCal.get(Calendar.DATE));
144
145 Calendar cal = (GregorianCalendar) c.clone();
146 cal.add(Calendar.DATE, (week - 1) * 7);
147 Date lastDate = getLastDayOfWeek(cal.getTime());
148
149 return lastDate;
150 }
151}
152