DateUtils.java 45 KB

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