68d9af7342a660106f61ba65f20559136558d8ae.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package com.sinosoft.lz.common;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Hashtable;
  6. /**
  7. * 功能:今天,昨天,本周,上周,本月,上月 范围:以周一作为一周的第一天,每周天数7天
  8. *
  9. * @author wzj
  10. *
  11. */
  12. public class CalendarUtil{
  13. private Date begin;
  14. private Date end;
  15. private Date now;
  16. // private Calendar now ;
  17. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18. public CalendarUtil() {
  19. now = new Date();
  20. // now = Calendar.getInstance();
  21. }
  22. // 获取今天
  23. public Hashtable<String, Date> getToday() {
  24. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  25. Calendar cal = Calendar.getInstance();
  26. cal.set(Calendar.MILLISECOND, 0);
  27. cal.set(Calendar.SECOND, 0);
  28. cal.set(Calendar.MINUTE, 0);
  29. cal.set(Calendar.HOUR_OF_DAY, 0);
  30. begin = cal.getTime();
  31. end = now;
  32. temp.put("begin", begin);
  33. temp.put("end", end);
  34. return temp;
  35. }
  36. // 获取昨天
  37. @SuppressWarnings("static-access")
  38. public Hashtable<String, Date> getYesterday() {
  39. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  40. Calendar cal = Calendar.getInstance();
  41. cal.add(cal.DATE, -1);
  42. cal.set(Calendar.MILLISECOND, 0);
  43. cal.set(Calendar.SECOND, 0);
  44. cal.set(Calendar.MINUTE, 0);
  45. cal.set(Calendar.HOUR_OF_DAY, 0);
  46. begin = cal.getTime();
  47. temp.put("begin", begin);
  48. cal.set(Calendar.MILLISECOND, 0);
  49. cal.set(Calendar.SECOND, 59);
  50. cal.set(Calendar.MINUTE, 59);
  51. cal.set(Calendar.HOUR_OF_DAY, 23);
  52. end = cal.getTime();
  53. temp.put("end", end);
  54. return temp;
  55. }
  56. // 获取本周
  57. public Hashtable<String, Date> getThisWeek() {
  58. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  59. Calendar cal = Calendar.getInstance();
  60. end = cal.getTime();
  61. int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
  62. cal.add(Calendar.DATE, -day_of_week);
  63. cal.set(Calendar.MILLISECOND, 0);
  64. cal.set(Calendar.SECOND, 0);
  65. cal.set(Calendar.MINUTE, 0);
  66. cal.set(Calendar.HOUR_OF_DAY, 0);
  67. begin = cal.getTime();
  68. temp.put("begin", begin);
  69. // cal.add(Calendar.DATE, 6);
  70. // cal.set(Calendar.MILLISECOND, 0);
  71. // cal.set(Calendar.SECOND, 59);
  72. // cal.set(Calendar.MINUTE, 59);
  73. // cal.set(Calendar.HOUR_OF_DAY, 23);
  74. temp.put("end", end);
  75. return temp;
  76. }
  77. // 获取上周
  78. @SuppressWarnings("static-access")
  79. public Hashtable<String, Date> getLastWeek() {
  80. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  81. Calendar cal = Calendar.getInstance();
  82. cal.add(cal.WEEK_OF_YEAR, -1);
  83. int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
  84. cal.add(Calendar.DATE, -day_of_week);
  85. cal.set(Calendar.MILLISECOND, 0);
  86. cal.set(Calendar.SECOND, 0);
  87. cal.set(Calendar.MINUTE, 0);
  88. cal.set(Calendar.HOUR_OF_DAY, 0);
  89. begin = cal.getTime();
  90. temp.put("begin", begin);
  91. cal.add(Calendar.DATE, 6);
  92. cal.set(Calendar.MILLISECOND, 0);
  93. cal.set(Calendar.SECOND, 59);
  94. cal.set(Calendar.MINUTE, 59);
  95. cal.set(Calendar.HOUR_OF_DAY, 23);
  96. end = cal.getTime();
  97. temp.put("end", end);
  98. return temp;
  99. }
  100. // 获取本月
  101. @SuppressWarnings("static-access")
  102. public Hashtable<String, Date> getThisMonth() {
  103. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  104. Calendar cal = Calendar.getInstance();
  105. end = cal.getTime();
  106. // cal.add(field, amount)\
  107. cal.set(cal.DAY_OF_MONTH, 1);
  108. cal.set(Calendar.MILLISECOND, 0);
  109. cal.set(Calendar.SECOND, 0);
  110. cal.set(Calendar.MINUTE, 0);
  111. cal.set(Calendar.HOUR_OF_DAY, 0);
  112. begin = cal.getTime();
  113. temp.put("begin", begin);
  114. temp.put("end", end);
  115. return temp;
  116. }
  117. // 获取上月
  118. @SuppressWarnings("static-access")
  119. public Hashtable<String, Date> getLastMonth() {
  120. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  121. Calendar cal = Calendar.getInstance();
  122. cal.set(cal.DAY_OF_MONTH, 0);
  123. cal.set(Calendar.MILLISECOND, 0);
  124. cal.set(Calendar.SECOND, 59);
  125. cal.set(Calendar.MINUTE, 59);
  126. cal.set(Calendar.HOUR_OF_DAY, 23);
  127. // cal.add(cal., amount)
  128. end = cal.getTime();
  129. cal.set(cal.DAY_OF_MONTH, 1);
  130. cal.set(Calendar.MILLISECOND, 0);
  131. cal.set(Calendar.SECOND, 0);
  132. cal.set(Calendar.MINUTE, 0);
  133. cal.set(Calendar.HOUR_OF_DAY, 0);
  134. begin = cal.getTime();
  135. temp.put("begin", begin);
  136. temp.put("end", end);
  137. return temp;
  138. }
  139. // 获取本年
  140. @SuppressWarnings("static-access")
  141. public Hashtable<String, Date> getThisYear() {
  142. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  143. Calendar cal = Calendar.getInstance();
  144. end = cal.getTime();
  145. cal.set(cal.DAY_OF_YEAR, 1);
  146. cal.set(Calendar.MILLISECOND, 0);
  147. cal.set(Calendar.SECOND, 0);
  148. cal.set(Calendar.MINUTE, 0);
  149. cal.set(Calendar.HOUR_OF_DAY, 0);
  150. begin = cal.getTime();
  151. temp.put("begin", begin);
  152. temp.put("end", end);
  153. return temp;
  154. }
  155. // 获取上一年
  156. @SuppressWarnings("static-access")
  157. public Hashtable<String, Date> getLastYear() {
  158. Hashtable<String, Date> temp = new Hashtable<String, Date>();
  159. Calendar cal = Calendar.getInstance();
  160. cal.set(cal.DAY_OF_YEAR, 0);
  161. cal.set(Calendar.MILLISECOND, 0);
  162. cal.set(Calendar.SECOND, 59);
  163. cal.set(Calendar.MINUTE, 59);
  164. cal.set(Calendar.HOUR_OF_DAY, 23);
  165. // cal.add(cal., amount)
  166. end = cal.getTime();
  167. cal.set(cal.DAY_OF_YEAR, 1);
  168. cal.set(Calendar.MILLISECOND, 0);
  169. cal.set(Calendar.SECOND, 0);
  170. cal.set(Calendar.MINUTE, 0);
  171. cal.set(Calendar.HOUR_OF_DAY, 0);
  172. begin = cal.getTime();
  173. temp.put("begin", begin);
  174. temp.put("end", end);
  175. return temp;
  176. }
  177. /**
  178. * @param args
  179. */
  180. public static void main(String[] args) {
  181. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  182. // Calendar cal = Calendar.getInstance();
  183. // cal.add(cal.WEEK_OF_YEAR, -1);
  184. // cal.set(cal.DAY_OF_MONTH, 1);
  185. // cal.set(cal.DAY_OF_YEAR, 0);
  186. // sdf.format(date)
  187. // cal.set(Calendar.HOUR_OF_DAY, 0);
  188. // cal.set(Calendar.MILLISECOND, 0);
  189. // cal.set(Calendar.SECOND, 0);
  190. // cal.set(Calendar.MINUTE, 0);
  191. // int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
  192. // cal.add(Calendar.DATE, -day_of_week);
  193. //
  194. // cal.add(Calendar.DATE, 6);
  195. CalendarUtil cu = new CalendarUtil();
  196. cu.getToday();
  197. cu.getYesterday();
  198. cu.getThisWeek();
  199. cu.getLastWeek();
  200. cu.getThisMonth();
  201. cu.getLastMonth();
  202. cu.getThisYear();
  203. cu.getLastYear();
  204. }
  205. }