Java日期操作


  1. /**
  2. *java日期操作(月末、周末等的日期操作)
  3. *
  4. *@author
  5. *
  6. */
  7. publicclassDateUtil{
  8. /***/
  9. /**
  10. *取得某天相加(减)後的那一天
  11. *
  12. *@paramdate
  13. *@paramnum
  14. *(可正可负)
  15. *@return
  16. */
  17. publicstaticDategetAnotherDate(Datedate,intnum){
  18. Calendarc=Calendar.getInstance();
  19. c.setTime(date);
  20. c.add(Calendar.DAY_OF_YEAR,num);
  21. returnc.getTime();
  22. }
  23. /***/
  24. /**
  25. *取得某月的的最后一天
  26. *
  27. *@paramyear
  28. *@parammonth
  29. *@return
  30. */
  31. publicstaticDategetLastDayOfMonth(intyear,intmonth){
  32. Calendarcal=Calendar.getInstance();
  33. cal.set(Calendar.YEAR,year);//年
  34. cal.set(Calendar.MONTH,month-1);//月,因为Calendar里的月是从0开始,所以要减1
  35. cal.set(Calendar.DATE,1);//日,设为一号
  36. cal.add(Calendar.MONTH,1);//月份加一,得到下个月的一号
  37. cal.add(Calendar.DATE,-1);//下一个月减一为本月最后一天
  38. returncal.getTime();//获得月末是几号
  39. }
  40. /***/
  41. /**
  42. *取得某天是一年中的多少周
  43. *
  44. *@paramdate
  45. *@return
  46. */
  47. publicstaticintgetWeekOfYear(Datedate){
  48. Calendarc=newGregorianCalendar();
  49. c.setFirstDayOfWeek(Calendar.MONDAY);
  50. c.setMinimalDaysInFirstWeek(7);
  51. c.setTime(date);
  52. returnc.get(Calendar.WEEK_OF_YEAR);
  53. }
  54. /***/
  55. /**
  56. *取得某天所在周的第一天
  57. *
  58. *@paramdate
  59. *@return
  60. */
  61. publicstaticDategetFirstDayOfWeek(Datedate){
  62. Calendarc=newGregorianCalendar();
  63. c.setFirstDayOfWeek(Calendar.MONDAY);
  64. c.setTime(date);
  65. c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek());
  66. returnc.getTime();
  67. }
  68. /***/
  69. /**
  70. *取得某天所在周的最后一天
  71. *
  72. *@paramdate
  73. *@return
  74. */
  75. publicstaticDategetLastDayOfWeek(Datedate){
  76. Calendarc=newGregorianCalendar();
  77. c.setFirstDayOfWeek(Calendar.MONDAY);
  78. c.setTime(date);
  79. c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek()+6);
  80. returnc.getTime();
  81. }
  82. /***/
  83. /**http://ini.iteye.com/
  84. *取得某一年共有多少周
  85. *
  86. *@paramyear
  87. *@return
  88. */
  89. publicstaticintgetMaxWeekNumOfYear(intyear){
  90. Calendarc=newGregorianCalendar();
  91. c.set(year,Calendar.DECEMBER,31,23,59,59);
  92. returngetWeekOfYear(c.getTime());
  93. }
  94. /**
  95. *http://ini.iteye.com/
  96. *获取某一年某一周的日期
  97. *@description
  98. *@paramyear
  99. *@paramweek
  100. *@return
  101. */
  102. publicstaticList<String>getWeekDays(intyear,intweek){
  103. List<String>list=newArrayList<String>();
  104. Datedate=getFirstDayOfWeek(year,week);
  105. SimpleDateFormatd=newSimpleDateFormat("yyyy-MM-dd");
  106. for(inti=0;i<7;i++){
  107. list.add(d.format(date));
  108. System.out.println(d.format(date));
  109. date.setDate(date.getDate()+1);
  110. }
  111. returnlist;
  112. }
  113. /***/
  114. /**
  115. *取得某年某周的第一天对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-05为2009年第一周的第一天
  116. *
  117. *@paramyear
  118. *@paramweek
  119. *@return
  120. */
  121. publicstaticDategetFirstDayOfWeek(intyear,intweek){
  122. CalendarcalFirst=Calendar.getInstance();
  123. calFirst.set(year,0,7);
  124. DatefirstDate=getFirstDayOfWeek(calFirst.getTime());
  125. CalendarfirstDateCal=Calendar.getInstance();
  126. firstDateCal.setTime(firstDate);
  127. Calendarc=newGregorianCalendar();
  128. c.set(Calendar.YEAR,year);
  129. c.set(Calendar.MONTH,Calendar.JANUARY);
  130. c.set(Calendar.DATE,firstDateCal.get(Calendar.DATE));
  131. Calendarcal=(GregorianCalendar)c.clone();
  132. cal.add(Calendar.DATE,(week-1)*7);
  133. firstDate=getFirstDayOfWeek(cal.getTime());
  134. returnfirstDate;
  135. }
  136. /***/
  137. /**
  138. *取得某年某周的最后一天对于交叉:2008-12-29到2009-01-04属于2008年的最后一周,2009-01-04为
  139. *2008年最后一周的最后一天
  140. *
  141. *@paramyear
  142. *@paramweek
  143. *@return
  144. */
  145. publicstaticDategetLastDayOfWeek(intyear,intweek){
  146. CalendarcalLast=Calendar.getInstance();
  147. calLast.set(year,0,7);
  148. DatefirstDate=getLastDayOfWeek(calLast.getTime());
  149. CalendarfirstDateCal=Calendar.getInstance();
  150. firstDateCal.setTime(firstDate);
  151. Calendarc=newGregorianCalendar();
  152. c.set(Calendar.YEAR,year);
  153. c.set(Calendar.MONTH,Calendar.JANUARY);
  154. c.set(Calendar.DATE,firstDateCal.get(Calendar.DATE));
  155. Calendarcal=(GregorianCalendar)c.clone();
  156. cal.add(Calendar.DATE,(week-1)*7);
  157. DatelastDate=getLastDayOfWeek(cal.getTime());
  158. returnlastDate;
  159. }
  160. /**
  161. *获取当前日期的年、月、日
  162. */
  163. publicvoiddisplay(){
  164. Calendarcal=Calendar.getInstance();
  165. //年
  166. intyear=cal.get(cal.YEAR);
  167. //月
  168. intmonth=cal.get(cal.MONTH)+1;
  169. //日
  170. intdate=cal.get(cal.DATE);
  171. //星期
  172. inttoday=cal.get(cal.DAY_OF_WEEK)-1;
  173. }
  174. publicstaticvoidmain(Stringargs[]){
  175. List<String>listWeekDate=DateUtil.getWeekDays(2010,52);
  176. for(Stringweeks:listWeekDate){
  177. System.out.println("weeks:"+weeks);
  178. }
  179. }
  180. }

你可能感兴趣的:(java)