XcglDateUtils.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package com.xcgl.utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.List;
  8. import org.apache.log4j.Logger;
  9. import org.jeecgframework.core.util.DataUtils;
  10. import org.jeecgframework.core.util.DateUtils;
  11. public class XcglDateUtils {
  12. private static final Logger logger = Logger.getLogger(XcglDateUtils.class);
  13. // 获得当天0点时间
  14. public static Date getTimesmorning() {
  15. Calendar cal = Calendar.getInstance();
  16. cal.set(Calendar.HOUR_OF_DAY, 0);
  17. cal.set(Calendar.SECOND, 0);
  18. cal.set(Calendar.MINUTE, 0);
  19. cal.set(Calendar.MILLISECOND, 0);
  20. return cal.getTime();
  21. }
  22. // 获得昨天0点时间
  23. public static Date getYesterdaymorning() {
  24. Calendar cal = Calendar.getInstance();
  25. cal.setTimeInMillis(getTimesmorning().getTime()-3600*24*1000);
  26. return cal.getTime();
  27. }
  28. // 获得当天近7天时间
  29. public static Date getWeekFromNow() {
  30. Calendar cal = Calendar.getInstance();
  31. cal.setTimeInMillis( getTimesmorning().getTime()-3600*24*1000*7);
  32. return cal.getTime();
  33. }
  34. // 获得当天24点时间
  35. public static Date getTimesnight() {
  36. Calendar cal = Calendar.getInstance();
  37. cal.set(Calendar.HOUR_OF_DAY, 24);
  38. cal.set(Calendar.SECOND, 0);
  39. cal.set(Calendar.MINUTE, 0);
  40. cal.set(Calendar.MILLISECOND, 0);
  41. return cal.getTime();
  42. }
  43. // 获得本周一0点时间
  44. public static Date getTimesWeekmorning() {
  45. Calendar cal = Calendar.getInstance();
  46. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  47. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  48. return cal.getTime();
  49. }
  50. // 获得本周日24点时间
  51. public static Date getTimesWeeknight() {
  52. Calendar cal = Calendar.getInstance();
  53. cal.setTime(getTimesWeekmorning());
  54. cal.add(Calendar.DAY_OF_WEEK, 7);
  55. return cal.getTime();
  56. }
  57. // 获得本月第一天0点时间
  58. public static Date getTimesMonthmorning() {
  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.getActualMinimum(Calendar.DAY_OF_MONTH));
  62. return cal.getTime();
  63. }
  64. // 获得本月最后一天24点时间
  65. public static Date getTimesMonthnight() {
  66. Calendar cal = Calendar.getInstance();
  67. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  68. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  69. cal.set(Calendar.HOUR_OF_DAY, 24);
  70. return cal.getTime();
  71. }
  72. public static Date getLastMonthStartMorning() {
  73. Calendar cal = Calendar.getInstance();
  74. cal.setTime(getTimesMonthmorning());
  75. cal.add(Calendar.MONTH, -1);
  76. return cal.getTime();
  77. }
  78. public static Date getCurrentQuarterStartTime() {
  79. Calendar c = Calendar.getInstance();
  80. int currentMonth = c.get(Calendar.MONTH) + 1;
  81. SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  82. SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
  83. Date now = null;
  84. try {
  85. if (currentMonth >= 1 && currentMonth <= 3)
  86. c.set(Calendar.MONTH, 0);
  87. else if (currentMonth >= 4 && currentMonth <= 6)
  88. c.set(Calendar.MONTH, 3);
  89. else if (currentMonth >= 7 && currentMonth <= 9)
  90. c.set(Calendar.MONTH, 6);
  91. else if (currentMonth >= 10 && currentMonth <= 12)
  92. c.set(Calendar.MONTH, 9);
  93. c.set(Calendar.DATE, 1);
  94. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return now;
  99. }
  100. /**
  101. * 当前季度的结束时间,即2012-03-31 23:59:59
  102. *
  103. * @return
  104. */
  105. public static Date getCurrentQuarterEndTime() {
  106. Calendar cal = Calendar.getInstance();
  107. cal.setTime(getCurrentQuarterStartTime());
  108. cal.add(Calendar.MONTH, 3);
  109. return cal.getTime();
  110. }
  111. public static Date getCurrentYearStartTime() {
  112. Calendar cal = Calendar.getInstance();
  113. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  114. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
  115. return cal.getTime();
  116. }
  117. public static Date getCurrentYearEndTime() {
  118. Calendar cal = Calendar.getInstance();
  119. cal.setTime(getCurrentYearStartTime());
  120. cal.add(Calendar.YEAR, 1);
  121. return cal.getTime();
  122. }
  123. public static Date getLastYearStartTime() {
  124. Calendar cal = Calendar.getInstance();
  125. cal.setTime(getCurrentYearStartTime());
  126. cal.add(Calendar.YEAR, -1);
  127. return cal.getTime();
  128. }
  129. //返回的是字符串型的时间,输入的
  130. //是String day, int x
  131. public static String addDateMinut(String day, int x)
  132. {
  133. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  134. // 24小时制
  135. // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
  136. // 量day格式一致
  137. Date date = null;
  138. try {
  139. date = format.parse(day);
  140. } catch (Exception ex) {
  141. ex.printStackTrace();
  142. }
  143. if (date == null)
  144. return "";
  145. Calendar cal = Calendar.getInstance();
  146. cal.setTime(date);
  147. cal.add(Calendar.MINUTE, x);// 24小时制
  148. date = cal.getTime();
  149. cal = null;
  150. return format.format(date);
  151. }
  152. //返回的是字符串型的时间,输入的
  153. // 是String day, int x
  154. public static String addDateDay(String day, int x ,SimpleDateFormat format)
  155. {
  156. if(format == null) {
  157. format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  158. }
  159. // 24小时制
  160. // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
  161. // 量day格式一致
  162. Date date = null;
  163. try {
  164. date = format.parse(day.length() > 10 ? day : day+" 00:00:00");
  165. } catch (Exception ex) {
  166. ex.printStackTrace();
  167. }
  168. if (date == null)
  169. return "";
  170. Calendar cal = Calendar.getInstance();
  171. cal.setTime(date);
  172. cal.add(Calendar.DATE, x);// 24小时制
  173. date = cal.getTime();
  174. cal = null;
  175. return format.format(date);
  176. }
  177. // 返回的是字符串型的时间,输入的
  178. // 是String day, int x
  179. public static Date addDateDay(Date date, int x) {
  180. // 24小时制
  181. // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
  182. // 量day格式一致
  183. if (date == null)
  184. return null;
  185. Calendar cal = Calendar.getInstance();
  186. cal.setTime(date);
  187. cal.add(Calendar.DATE, x);// 24小时制
  188. date = cal.getTime();
  189. cal = null;
  190. return date;
  191. }
  192. public static String addDateMonth(Date date, int x ,SimpleDateFormat format)
  193. {
  194. if(format == null) {
  195. format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  196. }
  197. if (date == null)
  198. return "";
  199. Calendar cal = Calendar.getInstance();
  200. cal.setTime(date);
  201. cal.add(Calendar.MONTH, x);
  202. date = cal.getTime();
  203. cal = null;
  204. return format.format(date);
  205. }
  206. public static List<String> getWeekMonAndSun(String indexday)
  207. {
  208. List<String> retday = new ArrayList<String>();
  209. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
  210. Calendar cal = Calendar.getInstance();
  211. Date time;
  212. try {
  213. time = sdf.parse(indexday);
  214. cal.setTime(time);
  215. } catch (ParseException e) {
  216. e.printStackTrace();
  217. }
  218. //判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
  219. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
  220. if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); }
  221. cal.setFirstDayOfWeek(Calendar.MONDAY);
  222. //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  223. int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
  224. cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
  225. //根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 System.out.println("所在周星期一的日期:"+sdf.format(cal.getTime()));
  226. retday.add(sdf.format(cal.getTime()));
  227. // System.out.println(cal.getFirstDayOfWeek()+"-"+day+"+6="+(cal.getFirstDayOfWeek()-day+6));
  228. cal.add(Calendar.DATE, 6);
  229. // System.out.println("所在周星期日的日期:"+sdf.format(cal.getTime()));
  230. retday.add(sdf.format(cal.getTime()));
  231. return retday;
  232. }
  233. public static List<String> getWeekdays(String indexday)
  234. {
  235. List<String> retday = new ArrayList<String>();
  236. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
  237. Calendar cal = Calendar.getInstance();
  238. Date time;
  239. try {
  240. time = sdf.parse(indexday);
  241. cal.setTime(time);
  242. } catch (ParseException e) {
  243. e.printStackTrace();
  244. }
  245. //判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
  246. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
  247. if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); }
  248. cal.setFirstDayOfWeek(Calendar.MONDAY);
  249. //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  250. int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
  251. cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
  252. retday.add(sdf.format(cal.getTime()));
  253. //周二
  254. cal.add(Calendar.DATE, 1);
  255. retday.add(sdf.format(cal.getTime()));
  256. //周三
  257. cal.add(Calendar.DATE, 1);
  258. retday.add(sdf.format(cal.getTime()));
  259. //周四
  260. cal.add(Calendar.DATE, 1);
  261. retday.add(sdf.format(cal.getTime()));
  262. //周五
  263. cal.add(Calendar.DATE, 1);
  264. retday.add(sdf.format(cal.getTime()));
  265. //周六
  266. cal.add(Calendar.DATE, 1);
  267. retday.add(sdf.format(cal.getTime()));
  268. //周日
  269. cal.add(Calendar.DATE, 1);
  270. retday.add(sdf.format(cal.getTime()));
  271. return retday;
  272. }
  273. /**
  274. * 获取星期几
  275. * @param 日期字符串,”yyyy-MM-dd“格式 or ”yyyy-MM-dd HH:mm:ss“
  276. * */
  277. public static String getWeekOfDate(String dateStr) {
  278. // 24小时制
  279. // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
  280. // 量day格式一致
  281. Date date = null;
  282. try {
  283. date = DataUtils.date_sdf.parse(dateStr.length() > 10 ? dateStr : dateStr+" 00:00:00");
  284. } catch (Exception ex) {
  285. logger.error(ex);
  286. ex.printStackTrace();
  287. }
  288. String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  289. Calendar cal = Calendar.getInstance();
  290. cal.setTime(date);
  291. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  292. if (w < 0)
  293. w = 0;
  294. return weekDays[w];
  295. }
  296. /**
  297. * 通过时间秒毫秒数判断两个时间的间隔
  298. * @param date1
  299. * @param date2
  300. * @return
  301. */
  302. public static int differentDaysByMillisecond(Date date1,Date date2)
  303. {
  304. int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
  305. return days;
  306. }
  307. /**
  308. * date2比date1多的天数
  309. * @param date1
  310. * @param date2
  311. * @return
  312. */
  313. public static int differentDays(Date date1,Date date2)
  314. {
  315. Calendar cal1 = Calendar.getInstance();
  316. cal1.setTime(date1);
  317. Calendar cal2 = Calendar.getInstance();
  318. cal2.setTime(date2);
  319. int day1= cal1.get(Calendar.DAY_OF_YEAR);
  320. int day2 = cal2.get(Calendar.DAY_OF_YEAR);
  321. int year1 = cal1.get(Calendar.YEAR);
  322. int year2 = cal2.get(Calendar.YEAR);
  323. if(year1 != year2) //不同一年
  324. {
  325. int timeDistance = 0 ;
  326. if(year2 > year1) {
  327. for(int i = year1 ; i < year2 ; i ++)
  328. {
  329. if(i%4==0 && i%100!=0 || i%400==0) //闰年
  330. {
  331. timeDistance += 366;
  332. }
  333. else //不是闰年
  334. {
  335. timeDistance += 365;
  336. }
  337. }
  338. return timeDistance + (day2-day1) ;
  339. }else {
  340. for(int i = year2 ; i < year1 ; i ++)
  341. {
  342. if(i%4==0 && i%100!=0 || i%400==0) //闰年
  343. {
  344. timeDistance += 366;
  345. }
  346. else //不是闰年
  347. {
  348. timeDistance += 365;
  349. }
  350. }
  351. return 0 - (timeDistance + (day1-day2)) ;
  352. }
  353. }
  354. else //同年
  355. {
  356. return day2-day1;
  357. }
  358. }
  359. /**
  360. *
  361. * 计算当前日期离当年最后一天相差天数
  362. * */
  363. public static int differentDays2YearEnd(Date date) {
  364. Date yearEnd = new Date();
  365. try {
  366. yearEnd = DateUtils.date_sdf.parse(DateUtils.date_sdf.format(date).substring(0,4)+"-12-31");
  367. } catch (ParseException e) {
  368. logger.error(e.getMessage());
  369. }
  370. return differentDays(date,yearEnd);
  371. }
  372. /**
  373. *
  374. * 计算当前日期离当年最后一天相差天数
  375. * */
  376. public static int differentDays2YearStart(Date date) {
  377. Date yearStart = new Date();
  378. try {
  379. yearStart = DateUtils.date_sdf.parse(DateUtils.date_sdf.format(date).substring(0,4)+"-01-01");
  380. } catch (ParseException e) {
  381. logger.error(e.getMessage());
  382. }
  383. return differentDays(yearStart,date);
  384. }
  385. public static Date getLastDayByMonth(Date date) {
  386. //获取当前月最后一天
  387. Calendar ca = Calendar.getInstance();
  388. ca.setTime(date);
  389. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  390. return ca.getTime();
  391. }
  392. public static Date getFirstDayByMonth(Date date) {
  393. Calendar c = Calendar.getInstance();
  394. c.setTime(date);
  395. c.add(Calendar.MONTH, 0);
  396. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  397. return c.getTime();
  398. }
  399. }