DateUtils.java 50 KB

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