DataUill.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.huimv.common.utils;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. public class DataUill {
  6. // 获得当天0点时间
  7. public static Date getTimesmorning() {
  8. Calendar cal = Calendar.getInstance();
  9. cal.set(Calendar.HOUR_OF_DAY, 0);
  10. cal.set(Calendar.SECOND, 0);
  11. cal.set(Calendar.MINUTE, 0);
  12. cal.set(Calendar.MILLISECOND, 0);
  13. return cal.getTime();
  14. }
  15. // 获得昨天0点时间
  16. public static Date getYesterdaymorning() {
  17. Calendar cal = Calendar.getInstance();
  18. cal.setTimeInMillis(getTimesmorning().getTime()-3600*24*1000);
  19. return cal.getTime();
  20. }
  21. // 获得当天近7天时间
  22. public static Date getWeekFromNow() {
  23. Calendar cal = Calendar.getInstance();
  24. cal.setTimeInMillis( getTimesmorning().getTime()-3600*24*1000*7);
  25. return cal.getTime();
  26. }
  27. // 获得当天24点时间
  28. public static Date getTimesnight() {
  29. Calendar cal = Calendar.getInstance();
  30. cal.set(Calendar.HOUR_OF_DAY, 24);
  31. cal.set(Calendar.SECOND, 0);
  32. cal.set(Calendar.MINUTE, 0);
  33. cal.set(Calendar.MILLISECOND, 0);
  34. return cal.getTime();
  35. }
  36. // 获得本周一0点时间
  37. public static Date getTimesWeekmorning() {
  38. Calendar cal = Calendar.getInstance();
  39. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  40. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  41. return cal.getTime();
  42. }
  43. // 获得本周日24点时间
  44. public static Date getTimesWeeknight() {
  45. Calendar cal = Calendar.getInstance();
  46. cal.setTime(getTimesWeekmorning());
  47. cal.add(Calendar.DAY_OF_WEEK, 7);
  48. return cal.getTime();
  49. }
  50. // 获得本月第一天0点时间
  51. public static Date getTimesMonthmorning() {
  52. Calendar cal = Calendar.getInstance();
  53. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  54. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  55. return cal.getTime();
  56. }
  57. // 获得本月最后一天24点时间
  58. public static Date getTimesMonthnight() {
  59. Calendar cal = Calendar.getInstance();
  60. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  61. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  62. cal.set(Calendar.HOUR_OF_DAY, 24);
  63. return cal.getTime();
  64. }
  65. public static Date getLastMonthStartMorning() {
  66. Calendar cal = Calendar.getInstance();
  67. cal.setTime(getTimesMonthmorning());
  68. cal.add(Calendar.MONTH, -1);
  69. return cal.getTime();
  70. }
  71. public static Date getTwoLastMonthStartMorning() {
  72. Calendar cal = Calendar.getInstance();
  73. cal.setTime(getTimesMonthmorning());
  74. cal.add(Calendar.MONTH, -2);
  75. return cal.getTime();
  76. }
  77. public static Date getCurrentQuarterStartTime() {
  78. Calendar c = Calendar.getInstance();
  79. int currentMonth = c.get(Calendar.MONTH) + 1;
  80. SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  81. SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
  82. Date now = null;
  83. try {
  84. if (currentMonth >= 1 && currentMonth <= 3)
  85. c.set(Calendar.MONTH, 0);
  86. else if (currentMonth >= 4 && currentMonth <= 6)
  87. c.set(Calendar.MONTH, 3);
  88. else if (currentMonth >= 7 && currentMonth <= 9)
  89. c.set(Calendar.MONTH, 4);
  90. else if (currentMonth >= 10 && currentMonth <= 12)
  91. c.set(Calendar.MONTH, 9);
  92. c.set(Calendar.DATE, 1);
  93. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. return now;
  98. }
  99. /**
  100. * 当前季度的结束时间,即2012-03-31 23:59:59
  101. *
  102. * @return
  103. */
  104. public static Date getCurrentQuarterEndTime() {
  105. Calendar cal = Calendar.getInstance();
  106. cal.setTime(getCurrentQuarterStartTime());
  107. cal.add(Calendar.MONTH, 3);
  108. return cal.getTime();
  109. }
  110. public static Date getCurrentYearStartTime() {
  111. Calendar cal = Calendar.getInstance();
  112. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  113. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
  114. return cal.getTime();
  115. }
  116. public static Date getCurrentYearEndTime() {
  117. Calendar cal = Calendar.getInstance();
  118. cal.setTime(getCurrentYearStartTime());
  119. cal.add(Calendar.YEAR, 1);
  120. return cal.getTime();
  121. }
  122. public static Date getLastYearStartTime() {
  123. Calendar cal = Calendar.getInstance();
  124. cal.setTime(getCurrentYearStartTime());
  125. cal.add(Calendar.YEAR, -1);
  126. return cal.getTime();
  127. }
  128. }