DateUtils.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. package org.jeecg.common.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.util.StringUtils;
  5. import java.beans.PropertyEditorSupport;
  6. import java.sql.Timestamp;
  7. import java.text.DateFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. /**
  12. * 类描述:时间操作定义类
  13. *
  14. * @Author: 张代浩
  15. * @Date:2012-12-8 12:15:03
  16. * @Version 1.0
  17. */
  18. public class DateUtils extends PropertyEditorSupport {
  19. private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
  20. /**
  21. * 以毫秒表示的时间
  22. */
  23. private static final long DAY_IN_MILLIS = 24 * 3600 * 1000L;
  24. private static final long HOUR_IN_MILLIS = 3600 * 1000L;
  25. private static final long MINUTE_IN_MILLIS = 60 * 1000L;
  26. private static final long SECOND_IN_MILLIS = 1000L;
  27. public static boolean compare(String beginDate, String nowDate) throws ParseException {
  28. //如果想比较日期则写成"yyyy-MM-dd"就可以了
  29. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  30. //将字符串形式的时间转化为Date类型的时间
  31. Date a = sdf.parse(beginDate);
  32. Date b = sdf.parse(nowDate);
  33. //Date类的一个方法,如果a早于b返回true,否则返回false
  34. if (a.before(b)) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. /*
  40. * 如果你不喜欢用上面这个太流氓的方法,也可以根据将Date转换成毫秒
  41. if(a.getTime()-b.getTime()<0)
  42. return true;
  43. else
  44. return false;
  45. */
  46. }
  47. public static String getDateByDateStr(String dateStr) throws ParseException {
  48. SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
  49. Date parse = dsdf.parse(dateStr);
  50. String day = dsdf.format(parse);
  51. return day;
  52. }
  53. public static Integer getHour(String dateStr) throws ParseException {
  54. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  55. Calendar cal = Calendar.getInstance();
  56. Date date = sdf.parse(dateStr);
  57. cal.setTime(date);
  58. Integer hour = cal.get(Calendar.HOUR_OF_DAY);
  59. return hour;
  60. }
  61. /**
  62. * 指定模式的时间格式
  63. *
  64. * @param pattern
  65. * @return
  66. */
  67. private static SimpleDateFormat getDateFormat(String pattern) {
  68. return new SimpleDateFormat(pattern);
  69. }
  70. /**
  71. * 当前日历,这里用中国时间表示
  72. *
  73. * @return 以当地时区表示的系统当前日历
  74. */
  75. public static Calendar getCalendar() {
  76. return Calendar.getInstance();
  77. }
  78. /**
  79. * 指定毫秒数表示的日历
  80. *
  81. * @param millis 毫秒数
  82. * @return 指定毫秒数表示的日历
  83. */
  84. public static Calendar getCalendar(long millis) {
  85. Calendar cal = Calendar.getInstance();
  86. cal.setTime(new Date(millis));
  87. return cal;
  88. }
  89. // ////////////////////////////////////////////////////////////////////////////
  90. // getDate
  91. // 各种方式获取的Date
  92. // ////////////////////////////////////////////////////////////////////////////
  93. /**
  94. * 当前日期
  95. *
  96. * @return 系统当前时间
  97. */
  98. public static Date getDate() {
  99. return new Date();
  100. }
  101. /**
  102. * 字符串转换成日期
  103. *
  104. * @param str
  105. * @param sdf
  106. * @return
  107. */
  108. public static Date str2Date(String str, SimpleDateFormat sdf) {
  109. if (null == str || "".equals(str)) {
  110. return null;
  111. }
  112. Date date = null;
  113. try {
  114. date = sdf.parse(str);
  115. return date;
  116. } catch (ParseException e) {
  117. e.printStackTrace();
  118. }
  119. return null;
  120. }
  121. /**
  122. * 日期转换为字符串
  123. *
  124. * @return 字符串
  125. */
  126. public static String date2Str() {
  127. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  128. return format.format(new Date());
  129. }
  130. /**
  131. * 格式化时间
  132. *
  133. * @param date
  134. * @param format
  135. * @return
  136. */
  137. public static String dateformat(String date, String format) {
  138. SimpleDateFormat sformat = new SimpleDateFormat(format);
  139. Date getDate = null;
  140. try {
  141. getDate = sformat.parse(date);
  142. } catch (ParseException e) {
  143. e.printStackTrace();
  144. }
  145. return sformat.format(getDate);
  146. }
  147. /**
  148. * 日期转换为字符串
  149. *
  150. * @param format 日期格式
  151. * @return 字符串
  152. */
  153. public static String getDate(String format) {
  154. Date date = new Date();
  155. SimpleDateFormat sdf = new SimpleDateFormat(format);
  156. return sdf.format(date);
  157. }
  158. /**
  159. * 指定毫秒数的时间戳
  160. *
  161. * @param millis 毫秒数
  162. * @return 指定毫秒数的时间戳
  163. */
  164. public static Timestamp getTimestamp(long millis) {
  165. return new Timestamp(millis);
  166. }
  167. /**
  168. * 以字符形式表示的时间戳
  169. *
  170. * @param time 毫秒数
  171. * @return 以字符形式表示的时间戳
  172. */
  173. public static Timestamp getTimestamp(String time) {
  174. return new Timestamp(Long.parseLong(time));
  175. }
  176. /**
  177. * 系统当前的时间戳
  178. *
  179. * @return 系统当前的时间戳
  180. */
  181. public static Timestamp getTimestamp() {
  182. return new Timestamp(System.currentTimeMillis());
  183. }
  184. /**
  185. * 当前时间,格式 yyyy-MM-dd HH:mm:ss
  186. *
  187. * @return 当前时间的标准形式字符串
  188. */
  189. public static String now() {
  190. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  191. return datetimeFormat.format(getCalendar().getTime());
  192. }
  193. /**
  194. * 指定日期的时间戳
  195. *
  196. * @param date 指定日期
  197. * @return 指定日期的时间戳
  198. */
  199. public static Timestamp getTimestamp(Date date) {
  200. return new Timestamp(date.getTime());
  201. }
  202. /**
  203. * 指定日历的时间戳
  204. *
  205. * @param cal 指定日历
  206. * @return 指定日历的时间戳
  207. */
  208. public static Timestamp getCalendarTimestamp(Calendar cal) {
  209. return new Timestamp(cal.getTime().getTime());
  210. }
  211. public static Timestamp gettimestamp() {
  212. Date dt = new Date();
  213. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  214. String nowTime = df.format(dt);
  215. Timestamp buydate = Timestamp.valueOf(nowTime);
  216. return buydate;
  217. }
  218. // ////////////////////////////////////////////////////////////////////////////
  219. // getMillis
  220. // 各种方式获取的Millis
  221. // ////////////////////////////////////////////////////////////////////////////
  222. /**
  223. * 系统时间的毫秒数
  224. *
  225. * @return 系统时间的毫秒数
  226. */
  227. public static long getMillis() {
  228. return System.currentTimeMillis();
  229. }
  230. /**
  231. * 指定日历的毫秒数
  232. *
  233. * @param cal 指定日历
  234. * @return 指定日历的毫秒数
  235. */
  236. public static long getMillis(Calendar cal) {
  237. return cal.getTime().getTime();
  238. }
  239. /**
  240. * 指定日期的毫秒数
  241. *
  242. * @param date 指定日期
  243. * @return 指定日期的毫秒数
  244. */
  245. public static long getMillis(Date date) {
  246. return date.getTime();
  247. }
  248. /**
  249. * 指定时间戳的毫秒数
  250. *
  251. * @param ts 指定时间戳
  252. * @return 指定时间戳的毫秒数
  253. */
  254. public static long getMillis(Timestamp ts) {
  255. return ts.getTime();
  256. }
  257. // ////////////////////////////////////////////////////////////////////////////
  258. // formatDate
  259. // 将日期按照一定的格式转化为字符串
  260. // ////////////////////////////////////////////////////////////////////////////
  261. /**
  262. * 默认方式表示的系统当前日期,具体格式:年-月-日
  263. *
  264. * @return 默认日期按“年-月-日“格式显示
  265. */
  266. public static String formatDate() {
  267. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  268. return format.format(getCalendar().getTime());
  269. }
  270. /**
  271. * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
  272. *
  273. * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
  274. */
  275. public static String formatDateTime() {
  276. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  277. return datetimeFormat.format(getCalendar().getTime());
  278. }
  279. /**
  280. * 指定日期的默认显示,具体格式:年-月-日
  281. *
  282. * @param cal 指定的日期
  283. * @return 指定日期按“年-月-日“格式显示
  284. */
  285. public static String formatDate(Calendar cal) {
  286. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  287. return format.format(cal.getTime());
  288. }
  289. /**
  290. * 指定日期的默认显示,具体格式:年-月-日
  291. *
  292. * @param date 指定的日期
  293. * @return 指定日期按“年-月-日“格式显示
  294. */
  295. public static String formatDate(Date date) {
  296. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  297. return format.format(date);
  298. }
  299. /**
  300. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
  301. *
  302. * @param millis 指定的毫秒数
  303. * @return 指定毫秒数表示日期按“年-月-日“格式显示
  304. */
  305. public static String formatDate(long millis) {
  306. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  307. return format.format(new Date(millis));
  308. }
  309. /**
  310. * 默认日期按指定格式显示
  311. *
  312. * @param pattern 指定的格式
  313. * @return 默认日期按指定格式显示
  314. */
  315. public static String formatDate(String pattern) {
  316. return getDateFormat(pattern).format(getCalendar().getTime());
  317. }
  318. /**
  319. * 指定日期按指定格式显示
  320. *
  321. * @param cal 指定的日期
  322. * @param pattern 指定的格式
  323. * @return 指定日期按指定格式显示
  324. */
  325. public static String formatDate(Calendar cal, String pattern) {
  326. return getDateFormat(pattern).format(cal.getTime());
  327. }
  328. /**
  329. * 指定日期按指定格式显示
  330. *
  331. * @param date 指定的日期
  332. * @param pattern 指定的格式
  333. * @return 指定日期按指定格式显示
  334. */
  335. public static String formatDate(Date date, String pattern) {
  336. return getDateFormat(pattern).format(date);
  337. }
  338. // ////////////////////////////////////////////////////////////////////////////
  339. // formatTime
  340. // 将日期按照一定的格式转化为字符串
  341. // ////////////////////////////////////////////////////////////////////////////
  342. /**
  343. * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
  344. *
  345. * @return 默认日期按“年-月-日 时:分“格式显示
  346. */
  347. public static String formatTime() {
  348. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  349. return timeSdf.format(getCalendar().getTime());
  350. }
  351. /**
  352. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
  353. *
  354. * @param millis 指定的毫秒数
  355. * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
  356. */
  357. public static String formatTime(long millis) {
  358. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  359. return timeSdf.format(new Date(millis));
  360. }
  361. /**
  362. * 指定日期的默认显示,具体格式:年-月-日 时:分
  363. *
  364. * @param cal 指定的日期
  365. * @return 指定日期按“年-月-日 时:分“格式显示
  366. */
  367. public static String formatTime(Calendar cal) {
  368. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  369. return timeSdf.format(cal.getTime());
  370. }
  371. /**
  372. * 指定日期的默认显示,具体格式:年-月-日 时:分
  373. *
  374. * @param date 指定的日期
  375. * @return 指定日期按“年-月-日 时:分“格式显示
  376. */
  377. public static String formatTime(Date date) {
  378. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  379. return timeSdf.format(date);
  380. }
  381. // ////////////////////////////////////////////////////////////////////////////
  382. // formatShortTime
  383. // 将日期按照一定的格式转化为字符串
  384. // ////////////////////////////////////////////////////////////////////////////
  385. /**
  386. * 默认方式表示的系统当前日期,具体格式:时:分
  387. *
  388. * @return 默认日期按“时:分“格式显示
  389. */
  390. public static String formatShortTime() {
  391. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  392. return shortTimeSdf.format(getCalendar().getTime());
  393. }
  394. /**
  395. * 指定毫秒数表示日期的默认显示,具体格式:时:分
  396. *
  397. * @param millis 指定的毫秒数
  398. * @return 指定毫秒数表示日期按“时:分“格式显示
  399. */
  400. public static String formatShortTime(long millis) {
  401. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  402. return shortTimeSdf.format(new Date(millis));
  403. }
  404. /**
  405. * 指定日期的默认显示,具体格式:时:分
  406. *
  407. * @param cal 指定的日期
  408. * @return 指定日期按“时:分“格式显示
  409. */
  410. public static String formatShortTime(Calendar cal) {
  411. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  412. return shortTimeSdf.format(cal.getTime());
  413. }
  414. /**
  415. * 指定日期的默认显示,具体格式:时:分
  416. *
  417. * @param date 指定的日期
  418. * @return 指定日期按“时:分“格式显示
  419. */
  420. public static String formatShortTime(Date date) {
  421. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  422. return shortTimeSdf.format(date);
  423. }
  424. // ////////////////////////////////////////////////////////////////////////////
  425. // parseDate
  426. // parseCalendar
  427. // parseTimestamp
  428. // 将字符串按照一定的格式转化为日期或时间
  429. // ////////////////////////////////////////////////////////////////////////////
  430. /**
  431. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  432. *
  433. * @param src 将要转换的原始字符窜
  434. * @param pattern 转换的匹配格式
  435. * @return 如果转换成功则返回转换后的日期
  436. * @throws ParseException
  437. * @throws
  438. */
  439. public static Date parseDate(String src, String pattern) {
  440. try {
  441. return getDateFormat(pattern).parse(src);
  442. } catch (ParseException e) {
  443. e.printStackTrace();
  444. return null;
  445. }
  446. }
  447. /**
  448. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  449. *
  450. * @param src 将要转换的原始字符窜
  451. * @param pattern 转换的匹配格式
  452. * @return 如果转换成功则返回转换后的日期
  453. * @throws ParseException
  454. * @throws
  455. */
  456. public static Calendar parseCalendar(String src, String pattern) throws ParseException {
  457. Date date = parseDate(src, pattern);
  458. Calendar cal = Calendar.getInstance();
  459. cal.setTime(date);
  460. return cal;
  461. }
  462. public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
  463. Calendar cal;
  464. cal = parseCalendar(src, pattern);
  465. cal.add(Calendar.DATE, amount);
  466. return formatDate(cal);
  467. }
  468. /**
  469. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  470. *
  471. * @param src 将要转换的原始字符窜
  472. * @param pattern 转换的匹配格式
  473. * @return 如果转换成功则返回转换后的时间戳
  474. * @throws ParseException
  475. * @throws
  476. */
  477. public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
  478. Date date = parseDate(src, pattern);
  479. return new Timestamp(date.getTime());
  480. }
  481. // ////////////////////////////////////////////////////////////////////////////
  482. // dateDiff
  483. // 计算两个日期之间的差值
  484. // ////////////////////////////////////////////////////////////////////////////
  485. /**
  486. * 计算两个时间之间的差值,根据标志的不同而不同
  487. *
  488. * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
  489. * @param calSrc 减数
  490. * @param calDes 被减数
  491. * @return 两个日期之间的差值
  492. */
  493. public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
  494. long millisDiff = getMillis(calSrc) - getMillis(calDes);
  495. if (flag == 'y') {
  496. return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR));
  497. }
  498. if (flag == 'd') {
  499. return (int) (millisDiff / DAY_IN_MILLIS);
  500. }
  501. if (flag == 'h') {
  502. return (int) (millisDiff / HOUR_IN_MILLIS);
  503. }
  504. if (flag == 'm') {
  505. return (int) (millisDiff / MINUTE_IN_MILLIS);
  506. }
  507. if (flag == 's') {
  508. return (int) (millisDiff / SECOND_IN_MILLIS);
  509. }
  510. return 0;
  511. }
  512. public static String getDateString(String createTime) throws ParseException {
  513. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
  514. Date date = sdf1.parse(createTime);
  515. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  516. String str = sdf2.format(date);
  517. return str;
  518. }
  519. public static Map<String, Object> getStartEndTime(String createTime) throws ParseException {
  520. SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  521. Date parse = smf.parse(createTime);
  522. Calendar calendar = Calendar.getInstance();
  523. calendar.setTime(parse);
  524. calendar.set(Calendar.HOUR_OF_DAY, 0);
  525. calendar.set(Calendar.MINUTE, 0);
  526. calendar.set(Calendar.SECOND, 0);
  527. Date start = calendar.getTime();
  528. calendar.add(Calendar.DAY_OF_MONTH, 1);
  529. calendar.add(Calendar.SECOND, -1);
  530. Date end = calendar.getTime();
  531. Map<String, Object> map = new HashMap<>();
  532. map.put("start", smf.format(start));
  533. map.put("end", smf.format(end));
  534. return map;
  535. }
  536. public static String getQuarterStartDate(Date date) {
  537. Calendar calendar = new GregorianCalendar();
  538. calendar.setTime(date);
  539. Integer month = calendar.get(Calendar.MONTH);
  540. Integer compare = month % 3;
  541. calendar.add(Calendar.MONTH, -compare);
  542. calendar.set(Calendar.DAY_OF_MONTH, 1);
  543. Date getDate = calendar.getTime();
  544. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  545. return dateFormat.format(getDate);
  546. }
  547. public static String getQuarterEndDate(Date date) {
  548. Calendar calendar = new GregorianCalendar();
  549. calendar.setTime(date);
  550. Integer month = calendar.get(Calendar.MONTH);
  551. Integer compare = month % 3;
  552. calendar.add(Calendar.MONTH, (3 - compare));
  553. calendar.set(Calendar.DAY_OF_MONTH, 1);
  554. Date getDate = calendar.getTime();
  555. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  556. return dateFormat.format(getDate);
  557. }
  558. /**
  559. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  560. * HH:mm:ss“ * @param text String类型的时间值
  561. */
  562. @Override
  563. public void setAsText(String text) throws IllegalArgumentException {
  564. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  565. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  566. if (StringUtils.hasText(text)) {
  567. try {
  568. if (text.indexOf(":") == -1 && text.length() == 10) {
  569. setValue(format.parse(text));
  570. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  571. setValue(datetimeFormat.parse(text));
  572. } else {
  573. throw new IllegalArgumentException("Could not parse date, date format is error ");
  574. }
  575. } catch (ParseException ex) {
  576. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  577. iae.initCause(ex);
  578. throw iae;
  579. }
  580. } else {
  581. setValue(null);
  582. }
  583. }
  584. public static int getYear(Date date) {
  585. GregorianCalendar calendar = new GregorianCalendar();
  586. calendar.setTime(date);
  587. return calendar.get(Calendar.YEAR);
  588. }
  589. public static Date timeStampToDate(Long time) {
  590. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  591. Date date;
  592. try {
  593. date = sdf.parse(sdf.format(time));
  594. return date;
  595. } catch (ParseException e) {
  596. e.printStackTrace();
  597. return null;
  598. }
  599. }
  600. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  601. List<Map<String, String>> list = new ArrayList<>();
  602. String splitDate = DateUtils.addDay(endDate, -1);
  603. splitDate = DateUtils.addDay(splitDate, 1);
  604. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  605. Map<String, String> map = new HashMap<String, String>();
  606. map.put("startDate", startDate);
  607. map.put("endDate", endDate);
  608. list.add(map);
  609. } else {
  610. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  611. Map<String, String> map = new HashMap<String, String>();
  612. map.put("startDate", splitDate);
  613. map.put("endDate", endDate);
  614. list.add(map);
  615. splitDate = DateUtils.addDay(splitDate, -1);
  616. endDate = DateUtils.addDay(endDate, -1);
  617. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  618. splitDate = startDate;
  619. }
  620. }
  621. }
  622. return list;
  623. }
  624. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  625. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  626. String splitDate = DateUtils.addWeek(endDate, -1);
  627. splitDate = DateUtils.addDay(splitDate, 1);
  628. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  629. Map<String, String> map = new HashMap<String, String>();
  630. map.put("startDate", startDate);
  631. map.put("endDate", endDate);
  632. list.add(map);
  633. } else {
  634. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  635. Map<String, String> map = new HashMap<String, String>();
  636. map.put("startDate", splitDate);
  637. map.put("endDate", endDate);
  638. list.add(map);
  639. splitDate = DateUtils.addWeek(splitDate, -1);
  640. endDate = DateUtils.addWeek(endDate, -1);
  641. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  642. splitDate = startDate;
  643. }
  644. }
  645. }
  646. return list;
  647. }
  648. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  649. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  650. String splitDate = DateUtils.addMonth(endDate, -1);
  651. splitDate = DateUtils.addDay(splitDate, 1);
  652. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  653. Map<String, String> map = new HashMap<String, String>();
  654. map.put("startDate", startDate);
  655. map.put("endDate", endDate);
  656. list.add(map);
  657. } else {
  658. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  659. Map<String, String> map = new HashMap<String, String>();
  660. map.put("startDate", splitDate);
  661. map.put("endDate", endDate);
  662. list.add(map);
  663. splitDate = DateUtils.addMonth(splitDate, -1);
  664. endDate = DateUtils.addMonth(endDate, -1);
  665. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  666. splitDate = startDate;
  667. }
  668. }
  669. }
  670. return list;
  671. }
  672. public static String addMonth(String dateString, int month) throws ParseException {
  673. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  674. Date date = sdf.parse(dateString);
  675. Calendar calendar = Calendar.getInstance();
  676. calendar.setTime(date);
  677. calendar.add(Calendar.MONTH, month);
  678. return sdf.format(calendar.getTime());
  679. }
  680. public static String addWeek(String dateString, int week) throws ParseException {
  681. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  682. Date date = sdf.parse(dateString);
  683. Calendar calendar = Calendar.getInstance();
  684. calendar.setTime(date);
  685. calendar.add(Calendar.WEEK_OF_YEAR, week);
  686. return sdf.format(calendar.getTime());
  687. }
  688. public static String addDay(String dateString, int day) {
  689. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  690. Date date = null;
  691. try {
  692. date = sdf.parse(dateString);
  693. } catch (ParseException e) {
  694. e.printStackTrace();
  695. }
  696. Calendar calendar = Calendar.getInstance();
  697. calendar.setTime(date);
  698. calendar.add(Calendar.DAY_OF_YEAR, day);
  699. return sdf.format(calendar.getTime());
  700. }
  701. public static Date addDay(Date date, int day) {
  702. Calendar calendar = Calendar.getInstance();
  703. calendar.setTime(date);
  704. calendar.add(Calendar.DAY_OF_YEAR, day);
  705. return calendar.getTime();
  706. }
  707. public static Date addSecond(Date date, int seconds) {
  708. Calendar calendar = Calendar.getInstance();
  709. calendar.setTime(date);
  710. calendar.add(Calendar.SECOND, seconds);
  711. return calendar.getTime();
  712. }
  713. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  714. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  715. Date firstDate = sdf.parse(firstDateString);
  716. Date secondDate = sdf.parse(secondDateString);
  717. long first = firstDate.getTime();
  718. long second = secondDate.getTime();
  719. return first == second ? 0 : (first > second ? 1 : -1);
  720. }
  721. public static String timeStamp2Date(Timestamp timeLong) {
  722. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  723. Date date;
  724. try {
  725. date = sdf.parse(sdf.format(timeLong));
  726. return sdf.format(date);
  727. } catch (ParseException e) {
  728. e.printStackTrace();
  729. return null;
  730. }
  731. }
  732. public static String timeStamp2Date(long currentTimeMillis) {
  733. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  734. Date date;
  735. try {
  736. date = sdf.parse(sdf.format(currentTimeMillis));
  737. return sdf.format(date);
  738. } catch (ParseException e) {
  739. e.printStackTrace();
  740. return null;
  741. }
  742. }
  743. public static Map<String, Object> getBeforeDate() {
  744. Date dNow = new Date();
  745. Date dBefore = new Date();
  746. Calendar calendar = Calendar.getInstance();
  747. calendar.setTime(dNow);
  748. calendar.add(Calendar.DAY_OF_MONTH, -1);
  749. dBefore = calendar.getTime();
  750. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  751. String defaultStartDate = sdf.format(dBefore);
  752. defaultStartDate = defaultStartDate + " 00:00:00";
  753. String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
  754. Map<String, Object> map = new HashMap<>();
  755. map.put("startDate", defaultStartDate);
  756. map.put("endDate", defaultEndDate);
  757. return map;
  758. }
  759. /**
  760. * @param format 返回日期格式
  761. * @param date 传入的初始日期
  762. * @param num 天数
  763. * @return
  764. * @throws ParseException
  765. */
  766. public static String getAnotherDay(String format, String date, Integer num) {
  767. SimpleDateFormat sdf = new SimpleDateFormat(format);
  768. Date getDate = null;
  769. try {
  770. getDate = sdf.parse(date);
  771. } catch (ParseException e) {
  772. e.printStackTrace();
  773. }
  774. Calendar calendar = Calendar.getInstance();
  775. calendar.setTime(getDate);
  776. calendar.add(Calendar.DAY_OF_MONTH, num);
  777. Date resultDate = calendar.getTime();
  778. return sdf.format(resultDate);
  779. }
  780. public static String getNowDate(String format) {
  781. SimpleDateFormat sdf = new SimpleDateFormat(format);
  782. Date date = new Date();
  783. return sdf.format(date);
  784. }
  785. public static String getMonthBefore(String format, String nowTime, int amount) {
  786. // 获取当前时间
  787. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  788. Date date = null;
  789. try {
  790. date = dateFormat.parse(nowTime);
  791. } catch (ParseException e) {
  792. logger.error(e.getMessage(), e);
  793. }
  794. //得到日历
  795. Calendar calendar = Calendar.getInstance();
  796. //把当前时间赋给日历
  797. calendar.setTime(date);
  798. //设置为前2月,可根据需求进行修改
  799. calendar.add(Calendar.MONTH, amount);
  800. //获取2个月前的时间
  801. date = calendar.getTime();
  802. return dateFormat.format(date);
  803. }
  804. public static Map<String, Object> compareDate(String format, String date1, String date2) {
  805. DateFormat df = new SimpleDateFormat(format);
  806. Map<String, Object> map = new HashMap<>();
  807. try {
  808. Date dt1 = df.parse(date1);
  809. Date dt2 = df.parse(date2);
  810. if (dt1.getTime() > dt2.getTime()) {
  811. map.put("bigDate", date1);
  812. map.put("smallDate", date2);
  813. return map;
  814. } else if (dt1.getTime() < dt2.getTime()) {
  815. map.put("bigDate", date2);
  816. map.put("smallDate", date1);
  817. return map;
  818. }
  819. } catch (Exception exception) {
  820. exception.printStackTrace();
  821. }
  822. return null;
  823. }
  824. /**
  825. * 日期中获取年份
  826. *
  827. * @param format
  828. * @param date
  829. * @return
  830. * @throws ParseException
  831. */
  832. public static String getYear(String format, String date) throws ParseException {
  833. SimpleDateFormat df = new SimpleDateFormat(format);
  834. Date parse = df.parse(date);
  835. return String.format("%tY", parse);
  836. }
  837. /**
  838. * 日期中获取月份
  839. *
  840. * @param format
  841. * @param date
  842. * @return
  843. * @throws ParseException
  844. */
  845. public static String getMonth(String format, String date) throws ParseException {
  846. SimpleDateFormat df = new SimpleDateFormat(format);
  847. Date parse = df.parse(date);
  848. return String.format("%tm", parse);
  849. }
  850. /**
  851. * 日期中获取天
  852. *
  853. * @param format
  854. * @param date
  855. * @return
  856. * @throws ParseException
  857. */
  858. public static String getDay(String format, String date) throws ParseException {
  859. SimpleDateFormat df = new SimpleDateFormat(format);
  860. Date parse = df.parse(date);
  861. return String.format("%td", parse);
  862. }
  863. /**
  864. * 根据时间获取季度
  865. * 1即Q1(1,2,3月),以此类推
  866. *
  867. * @param date
  868. * @return
  869. */
  870. public static int getQuarter(Date date) {
  871. Calendar cal = Calendar.getInstance();
  872. cal.setTime(date);
  873. //获取当前月份
  874. int month = cal.get(Calendar.MONTH) + 1;
  875. if (month <= 3) {
  876. return 1;
  877. } else if (month <= 6) {
  878. return 2;
  879. } else if (month <= 9) {
  880. return 3;
  881. } else if (month <= 12) {
  882. return 4;
  883. }
  884. return 0;
  885. }
  886. /**
  887. * 获取【所在周】周五的时间(周五为一周第一天,周四为一周最后一天)
  888. *
  889. * @param date
  890. */
  891. public static Date getFriday(Date date) {
  892. Calendar calendar = new GregorianCalendar();
  893. calendar.setTime(date);
  894. calendar.setFirstDayOfWeek(Calendar.FRIDAY);
  895. calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  896. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  897. return calendar.getTime();
  898. }
  899. /**
  900. * 获取传入时间的年、月份和季度(周五为每周第一天,周四为每周最后一天)
  901. * 获取的是所属年、月、季度,因为很多月份的第一周或最后 一周会包括上一个月或者下一个月的部分日期
  902. */
  903. public static Map<String, Integer> getYearQuarter(Date date) {
  904. Map<String, Integer> dateMap = new HashMap<>();
  905. Calendar calendar = new GregorianCalendar();
  906. calendar.setTime(date);
  907. int originYear = calendar.get(Calendar.YEAR);
  908. int originMonth = calendar.get(Calendar.MONTH);
  909. int originQuarter = DateUtils.getQuarter(date); //本月所在季度(按照日期来的)
  910. Calendar calendar1 = new GregorianCalendar();
  911. calendar1.setTime(date);
  912. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  913. //1.判断当天所在周第一天是否跨月
  914. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  915. int fridayYear = calendar1.get(Calendar.YEAR);
  916. int fridayMonth = calendar1.get(Calendar.MONTH);
  917. int fridayQuarter = DateUtils.getQuarter(calendar1.getTime());
  918. //2.判断当天所在周的最后一天是否跨月
  919. Calendar calendar2 = new GregorianCalendar();
  920. calendar2.setTime(date);
  921. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  922. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  923. int thursdayYear = calendar2.get(Calendar.YEAR);
  924. int thursdayMonth = calendar2.get(Calendar.MONTH);
  925. int thursdayQuarter = DateUtils.getQuarter(calendar2.getTime());
  926. //如果本周第一天(周五)和当前天不在一个月内,说明周跨月了
  927. if (fridayMonth == originMonth && thursdayMonth == originMonth) {
  928. dateMap.put("year", originYear);
  929. dateMap.put("month", originMonth + 1); //最后一个月是11,应该+1
  930. dateMap.put("quarter", originQuarter);
  931. return dateMap;
  932. } else if (fridayYear < originYear || (fridayYear == originYear && fridayMonth < originMonth)) {
  933. //判断本周有几天在本月,如果是(5,6,7)则就是本月本季度
  934. //int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  935. calendar1.set(Calendar.DATE, calendar1.getActualMaximum(Calendar.DATE));
  936. int lastDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  937. //判断如果本周所在上个月的最后一天是周五、周六、周日的话,说明本周归属于本月(周五是第一天);否则本周归属于上个季度
  938. if (lastDateWeek == 6 || lastDateWeek == 7 || lastDateWeek == 1) {
  939. dateMap.put("year", originYear);
  940. dateMap.put("month", originMonth + 1);
  941. dateMap.put("quarter", originQuarter);
  942. } else {
  943. //判断如果本周属于上个月,判断是否跨年
  944. if (originYear == fridayYear) {
  945. dateMap.put("year", originYear);
  946. dateMap.put("month", originMonth);
  947. //判断是否跨季
  948. if (originQuarter == fridayQuarter) {
  949. dateMap.put("quarter", originQuarter);
  950. } else {
  951. dateMap.put("quarter", fridayQuarter);
  952. }
  953. } else {
  954. //跨年的话说明这周上年的12月(第四季度)
  955. dateMap.put("year", originYear - 1);
  956. dateMap.put("month", 12); //??查看是11,还是12
  957. dateMap.put("quarter", 4);
  958. }
  959. }
  960. return dateMap;
  961. } else if (originYear < thursdayYear || (originYear == thursdayYear && originMonth < thursdayMonth)) {
  962. calendar2.set(Calendar.DATE, 1);
  963. int firstDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  964. //如果跨月的第一天是周二周三周四的话说明这周不跨月,否在进入下个月(季度、年需要再判断)
  965. if (firstDateWeek == 3 || firstDateWeek == 4 || firstDateWeek == 5) {
  966. dateMap.put("year", originYear);
  967. dateMap.put("month", originMonth + 1);
  968. dateMap.put("quarter", originQuarter);
  969. } else {
  970. if (originYear == thursdayYear) {
  971. dateMap.put("year", originYear);
  972. dateMap.put("month", originMonth + 1 + 1);
  973. //判断是否跨季
  974. if (originQuarter == thursdayQuarter) {
  975. dateMap.put("quarter", originQuarter);
  976. } else {
  977. dateMap.put("quarter", thursdayQuarter);
  978. }
  979. } else {
  980. //跨年的话说明这周属于明年的1月(第一季度)
  981. dateMap.put("year", originYear + 1);
  982. dateMap.put("month", 1);
  983. dateMap.put("quarter", 1);
  984. }
  985. }
  986. return dateMap;
  987. }
  988. return dateMap;
  989. }
  990. /**
  991. * 计算季度开始、结束时间
  992. * 原则:周五为本周的第一天,下一周的周四为本周的最后一天
  993. *
  994. * @param year
  995. * @param quarter
  996. */
  997. public static Map<String, String> quarterStartEndDate(int year, int quarter) {
  998. Map<String, String> quarterStartEndDateMap = new HashMap<>();
  999. Date startDate = null;
  1000. Date endDate = null;
  1001. int startMonth = 0;
  1002. int endMonth = 0;
  1003. if (quarter == 1) {
  1004. startMonth = 1;
  1005. endMonth = 3;
  1006. } else if (quarter == 2) {
  1007. startMonth = 4;
  1008. endMonth = 6;
  1009. } else if (quarter == 3) {
  1010. startMonth = 7;
  1011. endMonth = 9;
  1012. } else {
  1013. startMonth = 10;
  1014. endMonth = 12;
  1015. }
  1016. //判断季度第一天的时间(以周为单位,有可能是上个月,也有可能是本月的非1号)
  1017. Calendar calendar1 = new GregorianCalendar();
  1018. calendar1.set(Calendar.YEAR, year);
  1019. calendar1.set(Calendar.MONTH, startMonth - 1);
  1020. calendar1.set(Calendar.DATE, 1);
  1021. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  1022. //如果季度第一个月的第一天是周二周三周四,那么这周应该归属于上个季度,否则这周(包括上个月的时间)归属于这个季度
  1023. int firstDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  1024. if (firstDateWeek == 5 || firstDateWeek == 3 || firstDateWeek == 4) {
  1025. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  1026. startDate = DateUtils.addDay(calendar1.getTime(), 1);
  1027. } else {
  1028. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  1029. startDate = calendar1.getTime();
  1030. }
  1031. //判断季度最后一天的时间(以周为单位,有可能是下个月,也有可能是本月的非最后一天)
  1032. Calendar calendar2 = new GregorianCalendar();
  1033. calendar2.set(Calendar.YEAR, year);
  1034. calendar2.set(Calendar.MONTH, endMonth - 1);
  1035. calendar2.set(Calendar.DATE, 1);
  1036. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  1037. //如果一个季度最后一个月最后一天是周五、周六、周日,则这个月最后一周属于下个季度
  1038. calendar2.set(Calendar.DATE, calendar2.getActualMaximum(Calendar.DATE));
  1039. int lastDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  1040. if (lastDateWeek == 1 || lastDateWeek == 7 || lastDateWeek == 6) {
  1041. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  1042. endDate = DateUtils.addDay(calendar2.getTime(), -1);
  1043. } else {
  1044. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  1045. endDate = calendar2.getTime();
  1046. }
  1047. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1048. quarterStartEndDateMap.put("startDate", sdf.format(startDate));
  1049. quarterStartEndDateMap.put("endDate", sdf.format(endDate));
  1050. return quarterStartEndDateMap;
  1051. }
  1052. /**
  1053. * 获取上个季度的年度和季度数
  1054. *
  1055. * @param date
  1056. * @return
  1057. */
  1058. public static Map<String, Integer> lastQuarter(Date date) {
  1059. Map<String, Integer> resultMap = new HashMap<>();
  1060. //获取当天所在
  1061. Map<String, Integer> yearQuarter = getYearQuarter(date);
  1062. Integer year = yearQuarter.get("year");
  1063. Integer quarter = yearQuarter.get("quarter");
  1064. Integer lastyear = 0;
  1065. Integer lastQuarter = 0;
  1066. if (quarter == 4 || quarter == 3 || quarter == 2) {
  1067. lastyear = year;
  1068. lastQuarter = quarter - 1;
  1069. } else {
  1070. lastyear = year - 1;
  1071. lastQuarter = 4;
  1072. }
  1073. resultMap.put("year", lastyear);
  1074. resultMap.put("quarter", lastQuarter);
  1075. return resultMap;
  1076. }
  1077. /***
  1078. * 两日期之间相差天数
  1079. * @param dateStart
  1080. * @param dateEnd
  1081. * @return
  1082. */
  1083. public static long getDiscrepantDays(String dateStart, String dateEnd) {
  1084. //设置转换的日期格式
  1085. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1086. //结束时间
  1087. Date startDate = null;
  1088. Date endDate = null;
  1089. try {
  1090. //开始时间
  1091. startDate = sdf.parse(dateStart);
  1092. endDate = sdf.parse(dateEnd);
  1093. } catch (ParseException e) {
  1094. e.printStackTrace();
  1095. }
  1096. //得到相差的天数 betweenDate
  1097. return (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
  1098. }
  1099. /***
  1100. * 两个日期之间间隔的月数(不考虑日期的情况)
  1101. */
  1102. public static int calDiffMonth(Date start, Date end) {
  1103. GregorianCalendar startCalendar = new GregorianCalendar();
  1104. startCalendar.setTime(start);
  1105. GregorianCalendar endCalendar = new GregorianCalendar();
  1106. endCalendar.setTime(end);
  1107. int startYear = startCalendar.get(Calendar.YEAR);
  1108. int startMonth = startCalendar.get(Calendar.MONTH) + 1;
  1109. int startDay = startCalendar.get(Calendar.DAY_OF_MONTH);
  1110. int endYear = endCalendar.get(Calendar.YEAR);
  1111. int endMonth = endCalendar.get(Calendar.MONTH) + 1;
  1112. int endDay = endCalendar.get(Calendar.DAY_OF_MONTH);
  1113. return (endYear - startYear) * 12 + (endMonth - startMonth);
  1114. }
  1115. public static int getDaysOfMonth(int year, int month) {
  1116. Calendar calendar = Calendar.getInstance();
  1117. calendar.set(year, month - 1, 1);
  1118. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  1119. }
  1120. public static int getFirstDateOfMonth(int year, int month) {
  1121. Calendar calendar = Calendar.getInstance();
  1122. calendar.set(year, month - 1, 1);
  1123. return calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
  1124. }
  1125. /**
  1126. * 获取指定日期下个月的第一天
  1127. *
  1128. * @param dateStr
  1129. * @param format
  1130. * @return
  1131. */
  1132. public static Map<String, String> getMaxMinDaysOfAddMonth(String dateStr, String format, int addMonth) {
  1133. SimpleDateFormat sdf = new SimpleDateFormat(format);
  1134. Map<String, String> dateMap = new HashMap<>();
  1135. try {
  1136. Date date = sdf.parse(dateStr);
  1137. Calendar calendar = Calendar.getInstance();
  1138. calendar.setTime(date);
  1139. calendar.add(Calendar.MONTH, addMonth);
  1140. calendar.set(Calendar.DAY_OF_MONTH, 1);
  1141. dateMap.put("startDate", sdf.format(calendar.getTime()));
  1142. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
  1143. dateMap.put("endDate", sdf.format(calendar.getTime()));
  1144. return dateMap;
  1145. } catch (ParseException e) {
  1146. e.printStackTrace();
  1147. }
  1148. return null;
  1149. }
  1150. public static int dateDiff(String start, String end) {
  1151. //设置转换的日期格式
  1152. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1153. //开始时间
  1154. Date startDate = null;
  1155. Date endDate = null;
  1156. try {
  1157. startDate = sdf.parse(start);
  1158. endDate = sdf.parse(end);
  1159. } catch (ParseException e) {
  1160. e.printStackTrace();
  1161. }
  1162. //得到相差的天数 betweenDate
  1163. long betweenDate = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
  1164. return (int) betweenDate;
  1165. }
  1166. public static Date addMonth(Date date) {
  1167. Calendar calendar = Calendar.getInstance();//日历对象
  1168. calendar.add(Calendar.MONTH, -1);//月份减一
  1169. return calendar.getTime();
  1170. }
  1171. public static List<Date> findDates(Date dBegin, Date dEnd) {
  1172. List<Date> lDate = new ArrayList<Date>();
  1173. lDate.add(dBegin);
  1174. Calendar calBegin = Calendar.getInstance();
  1175. // 使用给定的 Date 设置此 Calendar 的时间
  1176. calBegin.setTime(dBegin);
  1177. Calendar calEnd = Calendar.getInstance();
  1178. // 使用给定的 Date 设置此 Calendar 的时间
  1179. calEnd.setTime(dEnd);
  1180. // 测试此日期是否在指定日期之后
  1181. while (dEnd.after(calBegin.getTime())) {
  1182. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1183. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  1184. lDate.add(calBegin.getTime());
  1185. }
  1186. return lDate;
  1187. }
  1188. public static void main(String[] args) throws ParseException {
  1189. String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
  1190. String s = DateUtils.addDay(nowDate, -180);
  1191. System.err.println(nowDate);
  1192. System.err.println(s);
  1193. }
  1194. }