1. package
2.   
3.   
4. import
5. import
6. import
7. import
8. import
9. import
10.   
11. public class
12. //用来全局控制 上一周,本周,下一周的周数变化 
13. private  int weeks = 0;   
14. private int MaxDate;//一月最大天数 
15. private int MaxYear;//一年最大天数 
16.        
17.        
18. /** 
19.      * @param args 
20.      */
21. public static void
22. new
23. "获取当天日期:"+tt.getNowTime("yyyy-MM-dd"));   
24. "获取本周一日期:"+tt.getMondayOFWeek());   
25. "获取本周日的日期~:"+tt.getCurrentWeekday());   
26. "获取上周一日期:"+tt.getPreviousWeekday());   
27. "获取上周日日期:"+tt.getPreviousWeekSunday());   
28. "获取下周一日期:"+tt.getNextMonday());   
29. "获取下周日日期:"+tt.getNextSunday());   
30. "获得相应周的周六的日期:"+tt.getNowTime("yyyy-MM-dd"));   
31. "获取本月第一天日期:"+tt.getFirstDayOfMonth());   
32. "获取本月最后一天日期:"+tt.getDefaultDay());   
33. "获取上月第一天日期:"+tt.getPreviousMonthFirst());   
34. "获取上月最后一天的日期:"+tt.getPreviousMonthEnd());   
35. "获取下月第一天日期:"+tt.getNextMonthFirst());   
36. "获取下月最后一天日期:"+tt.getNextMonthEnd());   
37. "获取本年的第一天日期:"+tt.getCurrentYearFirst());   
38. "获取本年最后一天日期:"+tt.getCurrentYearEnd());   
39. "获取去年的第一天日期:"+tt.getPreviousYearFirst());   
40. "获取去年的最后一天日期:"+tt.getPreviousYearEnd());   
41. "获取明年第一天日期:"+tt.getNextYearFirst());   
42. "获取明年最后一天日期:"+tt.getNextYearEnd());   
43. "获取本季度第一天到最后一天:"+tt.getThisSeasonTime(11));   
44. "获取两个日期之间间隔天数2008-12-1~2008-9.29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29"));   
45.     }   
46.        
47.        
48. /** 
49.         * 得到二个日期间的间隔天数 
50.         */
51. public static
52. new SimpleDateFormat("yyyy-MM-dd");   
53. long day = 0;   
54. try
55.          java.util.Date date = myFormatter.parse(sj1);   
56.          java.util.Date mydate = myFormatter.parse(sj2);   
57. 24 * 60 * 60 * 1000);   
58. catch
59. return "";   
60.         }   
61. return day + "";   
62.     }   
63.   
64.   
65. /** 
66.         * 根据一个日期,返回是星期几的字符串 
67.         *  
68.         * @param sdate 
69.         * @return 
70.         */
71. public static
72. // 再转换为时间 
73.         Date date = TimeTest.strToDate(sdate);   
74.         Calendar c = Calendar.getInstance();   
75.         c.setTime(date);   
76. // int hour=c.get(Calendar.DAY_OF_WEEK); 
77. // hour中存的就是星期几了,其范围 1~7 
78. // 1=星期日 7=星期六,其他类推 
79. return new SimpleDateFormat("EEEE").format(c.getTime());   
80.     }   
81.   
82. /** 
83.         * 将短时间格式字符串转换为时间 yyyy-MM-dd  
84.         *  
85.         * @param strDate 
86.         * @return 
87.         */
88. public static
89. new SimpleDateFormat("yyyy-MM-dd");   
90. new ParsePosition(0);   
91.         Date strtodate = formatter.parse(strDate, pos);   
92. return
93.     }   
94.   
95. /** 
96.         * 两个时间之间的天数 
97.         *  
98.         * @param date1 
99.         * @param date2 
100.         * @return 
101.         */
102. public static long
103. if (date1 == null || date1.equals(""))   
104. return 0;   
105. if (date2 == null || date2.equals(""))   
106. return 0;   
107. // 转换为标准时间 
108. new SimpleDateFormat("yyyy-MM-dd");   
109. null;   
110. null;   
111. try
112.          date = myFormatter.parse(date1);   
113.          mydate = myFormatter.parse(date2);   
114. catch
115.         }   
116. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);   
117. return
118.     }   
119.   
120.   
121.   
122.        
123. // 计算当月最后一天,返回字符串 
124. public
125. "";   
126. new SimpleDateFormat("yyyy-MM-dd");       
127.   
128.        Calendar lastDate = Calendar.getInstance();   
129. 1);//设为当前月的1号 
130. 1);//加一个月,变为下月的1号 
131. 1);//减去一天,变为当月最后一天 
132.           
133.        str=sdf.format(lastDate.getTime());   
134. return
135.     }   
136.        
137. // 上月第一天 
138. public
139. "";   
140. new SimpleDateFormat("yyyy-MM-dd");       
141.   
142.        Calendar lastDate = Calendar.getInstance();   
143. 1);//设为当前月的1号 
144. 1);//减一个月,变为下月的1号 
145. //lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天 
146.           
147.        str=sdf.format(lastDate.getTime());   
148. return
149.     }   
150.        
151. //获取当月第一天 
152. public
153. "";   
154. new SimpleDateFormat("yyyy-MM-dd");       
155.   
156.        Calendar lastDate = Calendar.getInstance();   
157. 1);//设为当前月的1号 
158.        str=sdf.format(lastDate.getTime());   
159. return
160.     }   
161.        
162. // 获得本周星期日的日期   
163. public
164. 0;   
165. int mondayPlus = this.getMondayPlus();   
166. new
167. 6);   
168.         Date monday = currentDate.getTime();   
169.            
170.         DateFormat df = DateFormat.getDateInstance();   
171.         String preMonday = df.format(monday);   
172. return
173.     }   
174.        
175.        
176. //获取当天时间  
177. public
178. new
179. new   SimpleDateFormat(dateformat);//可以方便地修改日期格式    
180.         String  hehe  = dateFormat.format(now);      
181. return
182.     }   
183.        
184. // 获得当前日期与本周日相差的天数 
185. private int
186.         Calendar cd = Calendar.getInstance();   
187. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天...... 
188. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因为按中国礼拜一作为第一天所以这里减1 
189. if (dayOfWeek == 1) {   
190. return 0;   
191. else
192. return 1
193.         }   
194.     }   
195.        
196. //获得本周一的日期 
197. public
198. 0;   
199. int mondayPlus = this.getMondayPlus();   
200. new
201.          currentDate.add(GregorianCalendar.DATE, mondayPlus);   
202.          Date monday = currentDate.getTime();   
203.             
204.          DateFormat df = DateFormat.getDateInstance();   
205.          String preMonday = df.format(monday);   
206. return
207.     }   
208.        
209. //获得相应周的周六的日期 
210. public
211. int mondayPlus = this.getMondayPlus();   
212. new
213. 7 * weeks + 6);   
214.         Date monday = currentDate.getTime();   
215.         DateFormat df = DateFormat.getDateInstance();   
216.         String preMonday = df.format(monday);   
217. return
218.     }   
219.        
220. // 获得上周星期日的日期 
221. public
222. 0;   
223.         weeks--;   
224. int mondayPlus = this.getMondayPlus();   
225. new
226.         currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);   
227.         Date monday = currentDate.getTime();   
228.         DateFormat df = DateFormat.getDateInstance();   
229.         String preMonday = df.format(monday);   
230. return
231.     }   
232.   
233. // 获得上周星期一的日期 
234. public
235.         weeks--;   
236. int mondayPlus = this.getMondayPlus();   
237. new
238. 7
239.         Date monday = currentDate.getTime();   
240.         DateFormat df = DateFormat.getDateInstance();   
241.         String preMonday = df.format(monday);   
242. return
243.     }   
244.        
245. // 获得下周星期一的日期 
246. public
247.         weeks++;   
248. int mondayPlus = this.getMondayPlus();   
249. new
250. 7);   
251.         Date monday = currentDate.getTime();   
252.         DateFormat df = DateFormat.getDateInstance();   
253.         String preMonday = df.format(monday);   
254. return
255.     }   
256.        
257. // 获得下周星期日的日期 
258. public
259.            
260. int mondayPlus = this.getMondayPlus();   
261. new
262. 7+6);   
263.         Date monday = currentDate.getTime();   
264.         DateFormat df = DateFormat.getDateInstance();   
265.         String preMonday = df.format(monday);   
266. return
267.     }   
268.        
269.        
270.        
271. private int
272.         Calendar cd = Calendar.getInstance();   
273. int
274. 1);//把日期设置为当月第一天  
275. 1);//日期回滚一天,也就是最后一天  
276.         MaxDate=cd.get(Calendar.DATE);    
277. if(monthOfNumber == 1){   
278. return
279. else{   
280. return 1-monthOfNumber;   
281.         }   
282.     }   
283.        
284. //获得上月最后一天的日期 
285. public
286. "";   
287. new SimpleDateFormat("yyyy-MM-dd");       
288.   
289.        Calendar lastDate = Calendar.getInstance();   
290. 1);//减一个月 
291. 1);//把日期设置为当月第一天  
292. 1);//日期回滚一天,也就是本月最后一天  
293.        str=sdf.format(lastDate.getTime());   
294. return
295.     }   
296.        
297. //获得下个月第一天的日期 
298. public
299. "";   
300. new SimpleDateFormat("yyyy-MM-dd");       
301.   
302.        Calendar lastDate = Calendar.getInstance();   
303. 1);//减一个月 
304. 1);//把日期设置为当月第一天  
305.        str=sdf.format(lastDate.getTime());   
306. return
307.     }   
308.        
309. //获得下个月最后一天的日期 
310. public
311. "";   
312. new SimpleDateFormat("yyyy-MM-dd");       
313.   
314.        Calendar lastDate = Calendar.getInstance();   
315. 1);//加一个月 
316. 1);//把日期设置为当月第一天  
317. 1);//日期回滚一天,也就是本月最后一天  
318.        str=sdf.format(lastDate.getTime());   
319. return
320.     }   
321.        
322. //获得明年最后一天的日期 
323. public
324. "";   
325. new SimpleDateFormat("yyyy-MM-dd");       
326.   
327.       Calendar lastDate = Calendar.getInstance();   
328. 1);//加一个年 
329. 1);   
330. 1);   
331.        str=sdf.format(lastDate.getTime());   
332. return
333.     }   
334.        
335. //获得明年第一天的日期 
336. public
337. "";   
338. new SimpleDateFormat("yyyy-MM-dd");       
339.   
340.       Calendar lastDate = Calendar.getInstance();   
341. 1);//加一个年 
342. 1);   
343.            str=sdf.format(lastDate.getTime());   
344. return
345.            
346.     }   
347.        
348. //获得本年有多少天 
349. private int
350.         Calendar cd = Calendar.getInstance();   
351. 1);//把日期设为当年第一天 
352. 1);//把日期回滚一天。 
353. int
354. return
355.     }   
356.        
357. private int
358.         Calendar cd = Calendar.getInstance();   
359. int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天 
360. 1);//把日期设为当年第一天 
361. 1);//把日期回滚一天。 
362. int
363. if(yearOfNumber == 1){   
364. return
365. else{   
366. return 1-yearOfNumber;   
367.         }   
368.     }   
369. //获得本年第一天的日期 
370. public
371. int yearPlus = this.getYearPlus();   
372. new
373.         currentDate.add(GregorianCalendar.DATE,yearPlus);   
374.         Date yearDay = currentDate.getTime();   
375.         DateFormat df = DateFormat.getDateInstance();   
376.         String preYearDay = df.format(yearDay);   
377. return
378.     }   
379.        
380.        
381. //获得本年最后一天的日期 * 
382. public
383. new
384. new   SimpleDateFormat("yyyy");//可以方便地修改日期格式    
385.         String  years  = dateFormat.format(date);      
386. return years+"-12-31";   
387.     }   
388.        
389.        
390. //获得上年第一天的日期 * 
391. public
392. new
393. new   SimpleDateFormat("yyyy");//可以方便地修改日期格式    
394. int
395.         years_value--;   
396. return years_value+"-1-1";   
397.     }   
398.        
399. //获得上年最后一天的日期 
400. public
401.         weeks--;   
402. int yearPlus = this.getYearPlus();   
403. new
404. 1));   
405.         Date yearDay = currentDate.getTime();   
406.         DateFormat df = DateFormat.getDateInstance();   
407.         String preYearDay = df.format(yearDay);   
408. 11);   
409. return
410.     }   
411.        
412. //获得本季度 
413. public String getThisSeasonTime(int
414. int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};   
415. int season = 1;   
416. if(month>=1&&month<=3){   
417. 1;   
418.         }   
419. if(month>=4&&month<=6){   
420. 2;   
421.         }   
422. if(month>=7&&month<=9){   
423. 3;   
424.         }   
425. if(month>=10&&month<=12){   
426. 4;   
427.         }   
428. int start_month = array[season-1][0];   
429. int end_month = array[season-1][2];   
430.            
431. new
432. new   SimpleDateFormat("yyyy");//可以方便地修改日期格式    
433.         String  years  = dateFormat.format(date);      
434. int
435.            
436. int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month); 
437. int
438. "-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;   
439. return
440.            
441.     }   
442.        
443. /** 
444.      * 获取某年某月的最后一天 
445.      * @param year 年 
446.      * @param month 月 
447.      * @return 最后一天 
448.      */
449. private int getLastDayOfMonth(int year, int
450. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
451. 10 || month == 12) {   
452. return 31;   
453.          }   
454. if (month == 4 || month == 6 || month == 9 || month == 11) {   
455. return 30;   
456.          }   
457. if (month == 2) {   
458. if
459. return 29;   
460. else
461. return 28;   
462.                }   
463.          }   
464. return 0;   
465.    }   
466. /** 
467.     * 是否闰年 
468.     * @param year 年 
469.     * @return  
470.     */
471. public boolean isLeapYear(int
472. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);   
473.   }   
474. }