DateUtils.java 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. package com.kcim.common.util;
  2. import com.kcim.common.enums.DateStyleEnum;
  3. import com.kcim.common.enums.WeekEnum;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.sql.Timestamp;
  6. import java.text.DateFormat;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.*;
  10. @Slf4j
  11. public class DateUtils {
  12. public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13. public static final SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM");
  14. public static final SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy");
  15. public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
  16. /**
  17. * 通用的一个DateFormat
  18. */
  19. public final static SimpleDateFormat commonDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  20. /**
  21. * 处理时分秒的DateFormat
  22. */
  23. public final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  24. /**
  25. * 不显示秒的DateFormat
  26. */
  27. public final static SimpleDateFormat noSecondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  28. /**
  29. * 获取SimpleDateFormat
  30. *
  31. * @param parttern 日期格式
  32. * @return SimpleDateFormat对象
  33. * @throws RuntimeException 异常:非法日期格式
  34. */
  35. private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
  36. return new SimpleDateFormat(parttern);
  37. }
  38. /**
  39. * 获取日期中的某数值。如获取月份
  40. *
  41. * @param date 日期
  42. * @param dateType 日期格式
  43. * @return 数值
  44. */
  45. public static int getInteger(Date date, int dateType) {
  46. Calendar calendar = Calendar.getInstance();
  47. calendar.setTime(date);
  48. return calendar.get(dateType);
  49. }
  50. /**
  51. * 当前时间转换.
  52. *
  53. * @return 时间字符串
  54. */
  55. public static String getDateString() {
  56. Date date = new Date();
  57. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  58. String strDate = sdf.format(date);
  59. return strDate;
  60. }
  61. /**
  62. * 获取时间的字符串
  63. *
  64. * @param date
  65. * @param simpleDateFormat
  66. * @return 时间字符串
  67. */
  68. public static String getDateString(Date date, SimpleDateFormat simpleDateFormat) {
  69. String strDate = simpleDateFormat.format(date);
  70. return strDate;
  71. }
  72. /**
  73. * 增加日期中某类型的某数值。如增加日期
  74. *
  75. * @param date 日期字符串
  76. * @param dateType 类型
  77. * @param amount 数值
  78. * @return 计算后日期字符串
  79. */
  80. private static String addInteger(String date, int dateType, int amount) {
  81. String dateString = null;
  82. DateStyleEnum dateStyle = getDateStyle(date);
  83. if (dateStyle != null) {
  84. Date myDate = StringToDate(date, dateStyle);
  85. myDate = addInteger(myDate, dateType, amount);
  86. dateString = DateToString(myDate, dateStyle);
  87. }
  88. return dateString;
  89. }
  90. /**
  91. * 增加日期中某类型的某数值。如增加日期
  92. *
  93. * @param date 日期字符串
  94. * @param dateType 类型
  95. * @param amount 数值
  96. * @return 计算后日期字符串
  97. */
  98. private static String addInteger(String date, DateStyleEnum dateStyle, int dateType, int amount) {
  99. String dateString = null;
  100. if (dateStyle != null) {
  101. Date myDate = StringToDate(date, dateStyle);
  102. myDate = addInteger(myDate, dateType, amount);
  103. dateString = DateToString(myDate, dateStyle);
  104. }
  105. return dateString;
  106. }
  107. /**
  108. * 增加日期中某类型的某数值。如增加日期
  109. *
  110. * @param date 日期
  111. * @param dateType 类型
  112. * @param amount 数值
  113. * @return 计算后日期
  114. */
  115. private static Date addInteger(Date date, int dateType, int amount) {
  116. Date myDate = null;
  117. if (date != null) {
  118. Calendar calendar = Calendar.getInstance();
  119. calendar.setTime(date);
  120. calendar.add(dateType, amount);
  121. myDate = calendar.getTime();
  122. }
  123. return myDate;
  124. }
  125. /**
  126. * 获取本周开始时间
  127. *
  128. * @param date
  129. *
  130. * @return
  131. */
  132. public static Date getBeginDayOfWeek(Date date) {
  133. Calendar cal = Calendar.getInstance();
  134. cal.setTime(date);
  135. int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
  136. if (dayofweek == 1) {
  137. dayofweek += 7;
  138. }
  139. cal.add(Calendar.DATE, 2 - dayofweek);
  140. return getDayStartTime(cal.getTime());
  141. }
  142. /**
  143. * 获取某个日期的开始时间
  144. *
  145. * @param date
  146. *
  147. * @return
  148. */
  149. public static Timestamp getDayStartTime(Date date) {
  150. Calendar calendar = Calendar.getInstance();
  151. if (null != date) {
  152. calendar.setTime(date);
  153. }
  154. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  155. calendar.set(Calendar.MILLISECOND, 0);
  156. return new Timestamp(calendar.getTimeInMillis());
  157. }
  158. /**
  159. * 获取本周结束时间
  160. *
  161. * @param date
  162. *
  163. * @return
  164. */
  165. public static Date getEndDayOfWeek(Date date) {
  166. Calendar cal = Calendar.getInstance();
  167. cal.setTime(getBeginDayOfWeek(date));
  168. cal.add(Calendar.DAY_OF_WEEK, 6);
  169. Date weekEndSta = cal.getTime();
  170. return getDayEndTime(weekEndSta);
  171. }
  172. /**
  173. * 获取某个日期的结束时间
  174. *
  175. * @param date
  176. *
  177. * @return
  178. */
  179. private static Date getDayEndTime(Date date) {
  180. Calendar calendar = Calendar.getInstance();
  181. if(null != date){
  182. calendar.setTime(date);
  183. }
  184. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
  185. calendar.set(Calendar.MILLISECOND, 999);
  186. return new Timestamp(calendar.getTimeInMillis());
  187. }
  188. /**
  189. * 获取精确的日期
  190. *
  191. * @param timestamps 时间long集合
  192. * @return 日期
  193. */
  194. private static Date getAccurateDate(List<Long> timestamps) {
  195. Date date = null;
  196. long timestamp = 0;
  197. Map<Long, long[]> map = new HashMap<Long, long[]>();
  198. List<Long> absoluteValues = new ArrayList<Long>();
  199. if (timestamps != null && timestamps.size() > 0) {
  200. if (timestamps.size() > 1) {
  201. for (int i = 0; i < timestamps.size(); i++) {
  202. for (int j = i + 1; j < timestamps.size(); j++) {
  203. long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
  204. absoluteValues.add(absoluteValue);
  205. long[] timestampTmp = {timestamps.get(i), timestamps.get(j)};
  206. map.put(absoluteValue, timestampTmp);
  207. }
  208. }
  209. // 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的
  210. long minAbsoluteValue = -1;
  211. if (!absoluteValues.isEmpty()) {
  212. // 如果timestamps的size为2,这是差值只有一个,因此要给默认值
  213. minAbsoluteValue = absoluteValues.get(0);
  214. }
  215. for (int i = 0; i < absoluteValues.size(); i++) {
  216. for (int j = i + 1; j < absoluteValues.size(); j++) {
  217. if (minAbsoluteValue > absoluteValues.get(j)) {
  218. minAbsoluteValue = absoluteValues.get(j);
  219. }
  220. }
  221. }
  222. if (minAbsoluteValue != -1) {
  223. long[] timestampsLastTmp = map.get(minAbsoluteValue);
  224. if (absoluteValues.size() > 1) {
  225. timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
  226. } else if (absoluteValues.size() == 1) {
  227. // 当timestamps的size为2,需要与当前时间作为参照
  228. long dateOne = timestampsLastTmp[0];
  229. long dateTwo = timestampsLastTmp[1];
  230. if ((Math.abs(dateOne - dateTwo)) < 100000000000L) {
  231. timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
  232. } else {
  233. long now = new Date().getTime();
  234. if (Math.abs(dateOne - now) <= Math.abs(dateTwo - now)) {
  235. timestamp = dateOne;
  236. } else {
  237. timestamp = dateTwo;
  238. }
  239. }
  240. }
  241. }
  242. } else {
  243. timestamp = timestamps.get(0);
  244. }
  245. }
  246. if (timestamp != 0) {
  247. date = new Date(timestamp);
  248. }
  249. return date;
  250. }
  251. /**
  252. * 判断字符串是否为日期字符串
  253. *
  254. * @param date 日期字符串
  255. * @return true or false
  256. */
  257. public static boolean isDate(String date) {
  258. boolean isDate = false;
  259. if (date != null) {
  260. if (StringToDate(date) != null) {
  261. isDate = true;
  262. }
  263. }
  264. return isDate;
  265. }
  266. /**
  267. * 获取日期字符串的日期风格。失敗返回null。
  268. *
  269. * @param date 日期字符串
  270. * @return 日期风格
  271. */
  272. public static DateStyleEnum getDateStyle(String date) {
  273. DateStyleEnum dateStyle = null;
  274. Map<Long, DateStyleEnum> map = new HashMap<Long, DateStyleEnum>();
  275. List<Long> timestamps = new ArrayList<Long>();
  276. for (DateStyleEnum style : DateStyleEnum.values()) {
  277. Date dateTmp = StringToDate(date, style.getValue());
  278. if (dateTmp != null) {
  279. timestamps.add(dateTmp.getTime());
  280. map.put(dateTmp.getTime(), style);
  281. }
  282. }
  283. dateStyle = map.get(getAccurateDate(timestamps).getTime());
  284. return dateStyle;
  285. }
  286. /**
  287. * 将日期字符串转化为日期。失败返回null。
  288. *
  289. * @param date 日期字符串
  290. * @return 日期
  291. */
  292. public static Date StringToDate(String date) {
  293. DateStyleEnum dateStyle = null;
  294. return StringToDate(date, dateStyle);
  295. }
  296. /**
  297. * 将日期字符串转化为日期。失败返回null。
  298. *
  299. * @param date 日期字符串
  300. * @param parttern 日期格式
  301. * @return 日期
  302. */
  303. public static Date StringToDate(String date, String parttern) {
  304. Date myDate = null;
  305. if (date != null) {
  306. try {
  307. myDate = getDateFormat(parttern).parse(date);
  308. } catch (Exception e) {
  309. }
  310. }
  311. return myDate;
  312. }
  313. /**
  314. * 将日期字符串转化为日期。失败返回null。
  315. *
  316. * @param date 日期字符串
  317. * @param dateStyle 日期风格
  318. * @return 日期
  319. */
  320. public static Date StringToDate(String date, DateStyleEnum dateStyle) {
  321. Date myDate = null;
  322. if (dateStyle == null) {
  323. List<Long> timestamps = new ArrayList<Long>();
  324. for (DateStyleEnum style : DateStyleEnum.values()) {
  325. Date dateTmp = StringToDate(date, style.getValue());
  326. if (dateTmp != null) {
  327. timestamps.add(dateTmp.getTime());
  328. }
  329. }
  330. myDate = getAccurateDate(timestamps);
  331. } else {
  332. myDate = StringToDate(date, dateStyle.getValue());
  333. }
  334. return myDate;
  335. }
  336. /**
  337. * 将日期转化为日期字符串。失败返回null。
  338. *
  339. * @param date 日期
  340. * @param parttern 日期格式
  341. * @return 日期字符串
  342. */
  343. public static String DateToString(Date date, String parttern) {
  344. String dateString = null;
  345. if (date != null) {
  346. try {
  347. dateString = getDateFormat(parttern).format(date);
  348. } catch (Exception e) {
  349. }
  350. }
  351. return dateString;
  352. }
  353. /**
  354. * 将日期转化为日期字符串。失败返回null。
  355. *
  356. * @param date 日期
  357. * @param dateStyle 日期风格
  358. * @return 日期字符串
  359. */
  360. public static String DateToString(Date date, DateStyleEnum dateStyle) {
  361. String dateString = null;
  362. if (dateStyle != null) {
  363. dateString = DateToString(date, dateStyle.getValue());
  364. }
  365. return dateString;
  366. }
  367. /**
  368. * 将日期字符串转化为另一日期字符串。失败返回null。
  369. *
  370. * @param date 旧日期字符串
  371. * @param parttern 新日期格式
  372. * @return 新日期字符串
  373. */
  374. public static String StringToString(String date, String parttern) {
  375. return StringToString(date, null, parttern);
  376. }
  377. /**
  378. * 将日期字符串转化为另一日期字符串。失败返回null。
  379. *
  380. * @param date 旧日期字符串
  381. * @param dateStyle 新日期风格
  382. * @return 新日期字符串
  383. */
  384. public static String StringToString(String date, DateStyleEnum dateStyle) {
  385. return StringToString(date, null, dateStyle);
  386. }
  387. /**
  388. * 将日期字符串转化为另一日期字符串。失败返回null。
  389. *
  390. * @param date 旧日期字符串
  391. * @param olddParttern 旧日期格式
  392. * @param newParttern 新日期格式
  393. * @return 新日期字符串
  394. */
  395. public static String StringToString(String date, String olddParttern, String newParttern) {
  396. String dateString = null;
  397. if (olddParttern == null) {
  398. DateStyleEnum style = getDateStyle(date);
  399. if (style != null) {
  400. Date myDate = StringToDate(date, style.getValue());
  401. dateString = DateToString(myDate, newParttern);
  402. }
  403. } else {
  404. Date myDate = StringToDate(date, olddParttern);
  405. dateString = DateToString(myDate, newParttern);
  406. }
  407. return dateString;
  408. }
  409. /**
  410. * 将日期字符串转化为另一日期字符串。失败返回null。
  411. *
  412. * @param date 旧日期字符串
  413. * @param olddDteStyle 旧日期风格
  414. * @param newDateStyle 新日期风格
  415. * @return 新日期字符串
  416. */
  417. public static String StringToString(String date, DateStyleEnum olddDteStyle, DateStyleEnum newDateStyle) {
  418. String dateString = null;
  419. if (olddDteStyle == null) {
  420. DateStyleEnum style = getDateStyle(date);
  421. dateString = StringToString(date, style.getValue(), newDateStyle.getValue());
  422. } else {
  423. dateString = StringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
  424. }
  425. return dateString;
  426. }
  427. /**
  428. * 增加日期的年份。失败返回null。
  429. *
  430. * @param date 日期
  431. * @param yearAmount 增加数量。可为负数
  432. * @return 增加年份后的日期字符串
  433. */
  434. public static String addYear(String date, int yearAmount) {
  435. return addInteger(date, Calendar.YEAR, yearAmount);
  436. }
  437. /**
  438. * 增加日期的年份。失败返回null。
  439. *
  440. * @param date 日期
  441. * @param yearAmount 增加数量。可为负数
  442. * @return 增加年份后的日期
  443. */
  444. public static Date addYear(Date date, int yearAmount) {
  445. return addInteger(date, Calendar.YEAR, yearAmount);
  446. }
  447. /**
  448. * 增加日期的月份。失败返回null。
  449. *
  450. * @param date 日期
  451. * @param yearAmount 增加数量。可为负数
  452. * @return 增加月份后的日期字符串
  453. */
  454. public static String addMonth(String date, int yearAmount) {
  455. return addInteger(date, Calendar.MONTH, yearAmount);
  456. }
  457. /**
  458. * 增加日期的月份。失败返回null。
  459. *
  460. * @param date 日期
  461. * @param yearAmount 增加数量。可为负数
  462. * @return 增加月份后的日期
  463. */
  464. public static Date addMonth(Date date, int yearAmount) {
  465. return addInteger(date, Calendar.MONTH, yearAmount);
  466. }
  467. /**
  468. * 增加日期的天数。失败返回null。
  469. *
  470. * @param date 日期字符串
  471. * @param dayAmount 增加数量。可为负数
  472. * @return 增加天数后的日期字符串
  473. * @deprecated use {@link #addDay(String, int, DateStyleEnum)}
  474. */
  475. @Deprecated
  476. public static String addDay(String date, int dayAmount) {
  477. return addInteger(date, Calendar.DATE, dayAmount);
  478. }
  479. /**
  480. * 增加日期的天数。失败返回null。
  481. *
  482. * @param date 日期字符串
  483. * @param dayAmount 增加数量。可为负数
  484. * @param style 日期样式
  485. * @return 增加天数后的日期字符串
  486. */
  487. public static String addDay(String date, int dayAmount, DateStyleEnum style) {
  488. return addInteger(date, style, Calendar.DATE, dayAmount);
  489. }
  490. /**
  491. * 增加日期的天数。失败返回null。
  492. *
  493. * @param date 日期
  494. * @param dayAmount 增加数量。可为负数
  495. * @return 增加天数后的日期
  496. */
  497. public static Date addDay(Date date, int dayAmount) {
  498. return addInteger(date, Calendar.DATE, dayAmount);
  499. }
  500. /**
  501. * 增加日期的小时。失败返回null。
  502. *
  503. * @param date 日期字符串
  504. * @param hourAmount 增加数量。可为负数
  505. * @return 增加小时后的日期字符串
  506. */
  507. public static String addHour(String date, int hourAmount) {
  508. return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
  509. }
  510. /**
  511. * 增加日期的小时。失败返回null。
  512. *
  513. * @param date 日期
  514. * @param hourAmount 增加数量。可为负数
  515. * @return 增加小时后的日期
  516. */
  517. public static Date addHour(Date date, int hourAmount) {
  518. return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
  519. }
  520. /**
  521. * 增加日期的分钟。失败返回null。
  522. *
  523. * @param date 日期字符串
  524. * @param hourAmount 增加数量。可为负数
  525. * @return 增加分钟后的日期字符串
  526. */
  527. public static String addMinute(String date, int hourAmount) {
  528. return addInteger(date, Calendar.MINUTE, hourAmount);
  529. }
  530. /**
  531. * 增加日期的分钟。失败返回null。
  532. *
  533. * @param date 日期
  534. * @param hourAmount 增加数量。可为负数
  535. * @return 增加分钟后的日期
  536. */
  537. public static Date addMinute(Date date, int hourAmount) {
  538. return addInteger(date, Calendar.MINUTE, hourAmount);
  539. }
  540. /**
  541. * 增加日期的秒钟。失败返回null。
  542. *
  543. * @param date 日期字符串
  544. * @param hourAmount 增加数量。可为负数
  545. * @return 增加秒钟后的日期字符串
  546. */
  547. public static String addSecond(String date, int hourAmount) {
  548. return addInteger(date, Calendar.SECOND, hourAmount);
  549. }
  550. /**
  551. * 增加日期的秒钟。失败返回null。
  552. *
  553. * @param date 日期
  554. * @param hourAmount 增加数量。可为负数
  555. * @return 增加秒钟后的日期
  556. */
  557. public static Date addSecond(Date date, int hourAmount) {
  558. return addInteger(date, Calendar.SECOND, hourAmount);
  559. }
  560. /**
  561. * 获取日期的年份。失败返回0。
  562. *
  563. * @param date 日期字符串
  564. * @return 年份
  565. */
  566. public static int getYear(String date) {
  567. return getYear(StringToDate(date));
  568. }
  569. /**
  570. * 获取日期的年份。失败返回0。
  571. *
  572. * @param date 日期
  573. * @return 年份
  574. */
  575. public static int getYear(Date date) {
  576. return getInteger(date, Calendar.YEAR);
  577. }
  578. /**
  579. * 获取日期的月份。失败返回0。
  580. *
  581. * @param date 日期字符串
  582. * @return 月份
  583. */
  584. public static int getMonth(String date) {
  585. return getMonth(StringToDate(date));
  586. }
  587. /**
  588. * 获取日期的月份
  589. *
  590. * @param date 日期字符串
  591. * @return 月份
  592. */
  593. public static String getMonthStr(Date date) {
  594. return monthSdf.format(date);
  595. }
  596. /**
  597. * 获取日期的月份。失败返回0。
  598. *
  599. * @param date 日期
  600. * @return 月份
  601. */
  602. public static int getMonth(Date date) {
  603. return getInteger(date, Calendar.MONTH);
  604. }
  605. /**
  606. * 获取日期的天数。失败返回0。
  607. *
  608. * @param date 日期字符串
  609. * @return 天
  610. */
  611. public static int getDay(String date) {
  612. return getDay(StringToDate(date));
  613. }
  614. /**
  615. * 获取日期的天数。失败返回0。
  616. *
  617. * @param date 日期
  618. * @return 天
  619. */
  620. public static int getDay(Date date) {
  621. return getInteger(date, Calendar.DATE);
  622. }
  623. /**
  624. * 获取日期的小时。失败返回0。
  625. *
  626. * @param date 日期字符串
  627. * @return 小时
  628. */
  629. public static int getHour(String date) {
  630. return getHour(StringToDate(date));
  631. }
  632. /**
  633. * 获取日期的小时。失败返回0。
  634. *
  635. * @param date 日期
  636. * @return 小时
  637. */
  638. public static int getHour(Date date) {
  639. return getInteger(date, Calendar.HOUR_OF_DAY);
  640. }
  641. /**
  642. * 获取日期的分钟。失败返回0。
  643. *
  644. * @param date 日期字符串
  645. * @return 分钟
  646. */
  647. public static int getMinute(String date) {
  648. return getMinute(StringToDate(date));
  649. }
  650. /**
  651. * 获取日期的分钟。失败返回0。
  652. *
  653. * @param date 日期
  654. * @return 分钟
  655. */
  656. public static int getMinute(Date date) {
  657. return getInteger(date, Calendar.MINUTE);
  658. }
  659. /**
  660. * 获取日期的秒钟。失败返回0。
  661. *
  662. * @param date 日期字符串
  663. * @return 秒钟
  664. */
  665. public static int getSecond(String date) {
  666. return getSecond(StringToDate(date));
  667. }
  668. /**
  669. * 获取日期的秒钟。失败返回0。
  670. *
  671. * @param date 日期
  672. * @return 秒钟
  673. */
  674. public static int getSecond(Date date) {
  675. return getInteger(date, Calendar.SECOND);
  676. }
  677. /**
  678. * 获取日期 。默认yyyy-MM-dd格式。失败返回null。
  679. *
  680. * @param date 日期字符串
  681. * @return 日期
  682. */
  683. public static String getDate(String date) {
  684. return StringToString(date, DateStyleEnum.YYYY_MM_DD);
  685. }
  686. /**
  687. * 获取日期。默认yyyy-MM-dd格式。失败返回null。
  688. *
  689. * @param date 日期
  690. * @return 日期
  691. */
  692. public static String getDate(Date date) {
  693. return DateToString(date, DateStyleEnum.YYYY_MM_DD);
  694. }
  695. /**
  696. * 获取日期。默认yyyy-MM-dd HH:mm:ss格式。失败返回null。
  697. *
  698. * @param date 日期
  699. * @return 日期
  700. */
  701. public static String getDateTime(Date date) {
  702. return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM_SS);
  703. }
  704. /**
  705. * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
  706. *
  707. * @param date 日期
  708. * @return 日期
  709. */
  710. public static String getDateTimeExcludeSec(Date date) {
  711. return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
  712. }
  713. /**
  714. * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
  715. *
  716. * @param date 日期
  717. * @return 日期
  718. */
  719. public static Date getDateTimeExcludeSec(String date) {
  720. return StringToDate(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
  721. }
  722. /**
  723. * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
  724. *
  725. * @param date 日期字符串
  726. * @return 时间
  727. */
  728. public static String getTime(String date) {
  729. return StringToString(date, DateStyleEnum.HH_MM_SS);
  730. }
  731. /**
  732. * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
  733. *
  734. * @param date 日期
  735. * @return 时间
  736. */
  737. public static String getTime(Date date) {
  738. return DateToString(date, DateStyleEnum.HH_MM_SS);
  739. }
  740. /**
  741. * 获取日期的星期。失败返回null。
  742. *
  743. * @param date 日期字符串
  744. * @return 星期
  745. */
  746. public static WeekEnum getWeek(String date) {
  747. WeekEnum week = null;
  748. DateStyleEnum dateStyle = getDateStyle(date);
  749. if (dateStyle != null) {
  750. Date myDate = StringToDate(date, dateStyle);
  751. week = getWeek(myDate);
  752. }
  753. return week;
  754. }
  755. /**
  756. * 获取日期的星期。失败返回null。
  757. *
  758. * @param date 日期
  759. * @return 星期
  760. */
  761. public static WeekEnum getWeek(Date date) {
  762. WeekEnum week = null;
  763. Calendar calendar = Calendar.getInstance();
  764. calendar.setTime(date);
  765. int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  766. switch (weekNumber) {
  767. case 0:
  768. week = WeekEnum.SUNDAY;
  769. break;
  770. case 1:
  771. week = WeekEnum.MONDAY;
  772. break;
  773. case 2:
  774. week = WeekEnum.TUESDAY;
  775. break;
  776. case 3:
  777. week = WeekEnum.WEDNESDAY;
  778. break;
  779. case 4:
  780. week = WeekEnum.THURSDAY;
  781. break;
  782. case 5:
  783. week = WeekEnum.FRIDAY;
  784. break;
  785. case 6:
  786. week = WeekEnum.SATURDAY;
  787. break;
  788. }
  789. return week;
  790. }
  791. /**
  792. * 获取两个日期相差的天数
  793. *
  794. * @param date 日期字符串
  795. * @param otherDate 另一个日期字符串
  796. * @return 相差天数
  797. */
  798. public static int getIntervalDays(String date, String otherDate) {
  799. return getIntervalDays(StringToDate(date), StringToDate(otherDate));
  800. }
  801. /**
  802. * @param date 日期
  803. * @param otherDate 另一个日期mingt
  804. * @return 相差天数
  805. */
  806. public static int getIntervalDays(Date date, Date otherDate) {
  807. //date = DateUtil.StringToDate(DateUtil.getDate(date));
  808. long time = Math.abs(date.getTime() - otherDate.getTime());
  809. long day = time / (24 * 60 * 60 * 1000);
  810. return (int) day;
  811. }
  812. /**
  813. * @param date
  814. * @param otherDate
  815. * @return 相差年数
  816. */
  817. public static long getIntervalYear(Date date, Date otherDate) {
  818. Calendar cal = Calendar.getInstance();
  819. int yearNow = cal.get(Calendar.YEAR);
  820. cal.setTime(otherDate);
  821. int yearOther = cal.get(Calendar.YEAR);
  822. long year = yearNow - yearOther;
  823. return year;
  824. }
  825. /**
  826. * 获取月份差
  827. *
  828. * @param date
  829. * @param otherDate
  830. * @return
  831. */
  832. public static long getIntervalMonths(Date date, Date otherDate) {
  833. long months;
  834. Calendar cal = Calendar.getInstance();
  835. int yearNow = cal.get(Calendar.YEAR);
  836. int monthNow = cal.get(Calendar.MONTH);
  837. cal.setTime(otherDate);
  838. int yearOther = cal.get(Calendar.YEAR);
  839. int monthOther = cal.get(Calendar.MONTH);
  840. long year = yearNow - yearOther;
  841. if (year >= 1) {
  842. months = year * 12 + monthNow - monthOther;
  843. } else {
  844. months = monthNow - monthOther;
  845. }
  846. return months;
  847. }
  848. public static String getWeekFromDate(Date date) {
  849. Calendar c = Calendar.getInstance();
  850. c.setTime(date);
  851. int week = c.get(Calendar.DAY_OF_WEEK);
  852. switch (week) {
  853. case 1:
  854. return "星期日";
  855. case 2:
  856. return "星期一";
  857. case 3:
  858. return "星期二";
  859. case 4:
  860. return "星期三";
  861. case 5:
  862. return "星期四";
  863. case 6:
  864. return "星期五";
  865. case 7:
  866. return "星期六";
  867. default:
  868. return "";
  869. }
  870. }
  871. /**
  872. * 获取指定天数之前的时间(相对于当前天数)
  873. *
  874. * @param currentDate 当前时间
  875. * @param dayNo 当前时间的几天之前
  876. * @return
  877. * @Title: getLastDays
  878. * @date 2014-3-26 下午11:23:57
  879. * @author Lawrence
  880. */
  881. public static Date getLastTimeForDays(Date currentDate, Integer dayNo) {
  882. if (null == currentDate || null == dayNo) {
  883. return null;
  884. }
  885. Calendar c = Calendar.getInstance();
  886. c.setTime(currentDate);
  887. c.add(Calendar.DATE, -dayNo);
  888. return c.getTime();
  889. }
  890. /**
  891. * 获取指定月数之前的时间(相对于当前天数)
  892. *
  893. * @param currentDate 当前时间
  894. * @param monthNo 当前时间的几月之前
  895. * @return
  896. * @Title: getLastDays
  897. * @date 2014-3-26 下午11:23:57
  898. * @author Lawrence
  899. */
  900. public static Date getLastTimeForMonths(Date currentDate, Integer monthNo) {
  901. if (null == currentDate || null == monthNo) {
  902. return null;
  903. }
  904. Calendar c = Calendar.getInstance();
  905. c.setTime(currentDate);
  906. c.add(Calendar.MONTH, -monthNo);
  907. return c.getTime();
  908. }
  909. /**
  910. * 获取指定月数之前的时间(相对于当前天数)
  911. *
  912. * @param currentDate 当前时间
  913. * @param yearNo 当前时间的几年之前
  914. * @return
  915. * @Title: getLastDays
  916. * @date 2014-3-26 下午11:23:57
  917. * @author Lawrence
  918. */
  919. public static Date getLastTimeForYears(Date currentDate, Integer yearNo) {
  920. Calendar c = Calendar.getInstance();
  921. c.setTime(currentDate);
  922. c.add(Calendar.YEAR, -yearNo);
  923. return c.getTime();
  924. }
  925. /**
  926. * 获取当天00:00:00
  927. *
  928. * @param currentDate
  929. * @return
  930. * @Title: getZeroPoint
  931. * @Description: (这里用一句话描述这个方法的作用)
  932. * @date 2014年10月10日 上午11:09:35
  933. * @author Administrator
  934. */
  935. public static Date getZeroPoint(Date currentDate) {
  936. Calendar cal = Calendar.getInstance();
  937. cal.setTime(currentDate);
  938. cal.set(Calendar.HOUR_OF_DAY, 0); // 把当前时间小时变成0
  939. cal.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
  940. cal.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
  941. cal.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
  942. return cal.getTime();
  943. }
  944. /**
  945. * 获取当天00:00:00
  946. *
  947. * @param currentDate
  948. * @return
  949. * @Title: getZeroPoint
  950. * @Description: (这里用一句话描述这个方法的作用)
  951. * @date 2014年10月10日 上午11:09:35
  952. * @author Administrator
  953. */
  954. public static Date getZeroPoint(String currentDate) {
  955. return getZeroPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
  956. }
  957. /**
  958. * 获取日期的最后一秒时间
  959. *
  960. * @param currentDate
  961. * @return
  962. */
  963. public static Date getLastPoint(String currentDate) {
  964. return getLastPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
  965. }
  966. /**
  967. * 获取日期的最后一秒时间
  968. *
  969. * @param currentDate
  970. * @return
  971. */
  972. public static Date getLastPoint(Date currentDate) {
  973. Calendar cal = Calendar.getInstance();
  974. cal.setTime(currentDate);
  975. cal.set(Calendar.HOUR_OF_DAY, 23); // 把当前时间小时变成
  976. cal.set(Calendar.MINUTE, 59); // 把当前时间分钟变成
  977. cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
  978. return cal.getTime();
  979. }
  980. /**
  981. * 获取指定分钟最后一秒时间
  982. *
  983. * @param currentDate
  984. * @return
  985. */
  986. public static Date getDateEndTime(Date currentDate) {
  987. Calendar cal = Calendar.getInstance();
  988. cal.setTime(currentDate);
  989. cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
  990. return cal.getTime();
  991. }
  992. /**
  993. * 获取指定分钟最后一秒时间
  994. *
  995. * @param currentDate
  996. * @return
  997. */
  998. public static Date getDateBegTime(Date currentDate) {
  999. Calendar cal = Calendar.getInstance();
  1000. cal.setTime(currentDate);
  1001. cal.set(Calendar.SECOND, 00); // 把当前时间秒数变成
  1002. return cal.getTime();
  1003. }
  1004. /**
  1005. * 时间比较大小工具类
  1006. *
  1007. * @param date1
  1008. * @param date2
  1009. * @return
  1010. */
  1011. public static Boolean compareDate(Date date1, Date date2) {
  1012. try {
  1013. if (date1.getTime() >= date2.getTime()) {
  1014. return true;
  1015. } else if (date1.getTime() < date2.getTime()) {
  1016. return false;
  1017. }
  1018. } catch (Exception e) {
  1019. log.error(e.toString(), e);
  1020. }
  1021. return false;
  1022. }
  1023. /**
  1024. * 把毫秒转化成日期
  1025. *
  1026. * @param millSec(毫秒数)
  1027. * @return
  1028. */
  1029. public static Date LongToDate(Long millSec) {
  1030. return LongToDate(millSec, "yyyy-MM-dd HH:mm:ss");
  1031. }
  1032. /**
  1033. * 把毫秒转化成日期
  1034. *
  1035. * @param parttern(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
  1036. * @param millSec(毫秒数)
  1037. * @return
  1038. */
  1039. public static Date LongToDate(Long millSec, String parttern) {
  1040. return new Date(millSec);
  1041. }
  1042. /* public static void main(String[] args){
  1043. // Long currentTime = System.currentTimeMillis();
  1044. // System.out.println(LongToDate(currentTime));
  1045. // Date currentDate = new Date();
  1046. // currentDate = getZeroPoint(currentDate);
  1047. // //得到一天前
  1048. // Date dd = getLastTimeForDays(currentDate, 1);
  1049. // System.out.println(dd.toLocaleString());
  1050. // //得到前一周
  1051. // Date dw = getLastTimeForDays(currentDate, 7);
  1052. // System.out.println(dw.toLocaleString());
  1053. // //得到前一个月
  1054. // Date dm = getLastTimeForMonths(currentDate, 1);
  1055. // System.out.println(dm.toLocaleString());
  1056. // //得到前一年
  1057. // Date dy = getLastTimeForYears(currentDate, 1);
  1058. // System.out.println(dy.toLocaleString());
  1059. String date = DateToString(new Date(), "yyyyMMddHHmmss");
  1060. System.out.println(date);
  1061. System.out.println(getZeroPoint(new Date()));
  1062. }*/
  1063. /**
  1064. * 截取时间getTime的后N位
  1065. *
  1066. * @param time 时间
  1067. * @return
  1068. * @Title: getTimeToSix
  1069. * @date 2016年9月21日 下午5:14:00
  1070. * @author Chris_He
  1071. */
  1072. public static String getTimeToNumber(Date time, Integer n) {
  1073. String s = String.valueOf(time.getTime());
  1074. return s.substring(s.length() - n, s.length());
  1075. }
  1076. /**
  1077. * 获取当前时间上下午
  1078. *
  1079. * @return
  1080. * @Title: getNowDateAmPm
  1081. * @date 2017年8月29日 下午5:08:37
  1082. * @author Chris_He
  1083. */
  1084. public static DateStyleEnum getNowDateAmPm() {
  1085. GregorianCalendar ca = new GregorianCalendar();
  1086. int i = ca.get(GregorianCalendar.AM_PM);
  1087. if (i == 0) {
  1088. return DateStyleEnum.AM;
  1089. } else {
  1090. return DateStyleEnum.PM;
  1091. }
  1092. }
  1093. public static Date getAfterMinuteDate(Date date, int amount) {
  1094. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1095. Calendar cal = Calendar.getInstance();
  1096. cal.setTime(date);
  1097. cal.add(Calendar.MINUTE, amount);
  1098. date = cal.getTime();
  1099. // String theDate = sdf.format(date);
  1100. return date;
  1101. }
  1102. public static Date getAfterSecondDate(Date date, int amount) {
  1103. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1104. Calendar cal = Calendar.getInstance();
  1105. cal.setTime(date);
  1106. cal.add(Calendar.SECOND, amount);
  1107. date = cal.getTime();
  1108. // String theDate = sdf.format(date);
  1109. return date;
  1110. }
  1111. public static String formatFull(Date date) {
  1112. if (date != null) {
  1113. return DATE_FORMAT_YYYY_MM_DD_HH_MM_SS.format(date);
  1114. }
  1115. return null;
  1116. }
  1117. /**
  1118. * 获取下一天00:00:00
  1119. *
  1120. * @return
  1121. * @Title: getNextDay
  1122. * @date 2018年12月22日 下午5:08:37
  1123. * @author Chris_He
  1124. */
  1125. public static Date getNextDay(Date date) {
  1126. return addDay(StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD), DateStyleEnum.YYYY_MM_DD), 1);
  1127. }
  1128. public static void main(String[] args) {
  1129. // System.out.println(DateUtil.getYear(new Date()));
  1130. System.out.println(getHour("7:00"));
  1131. }
  1132. /**
  1133. * 获取下一天指定时间
  1134. *
  1135. * @return
  1136. * @Title: getNextDay
  1137. * @date 2018年12月22日 下午5:08:37
  1138. * @author Chris_He
  1139. */
  1140. public static Date getNextDayHour(Date date, int hour, int amount) {
  1141. Date myDate = null;
  1142. if (date != null) {
  1143. Calendar calendar = Calendar.getInstance();
  1144. calendar.setTime(date);
  1145. calendar.add(Calendar.DATE, amount);
  1146. calendar.set(Calendar.HOUR_OF_DAY, hour); // 把当前时间小时变成0
  1147. calendar.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
  1148. calendar.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
  1149. calendar.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
  1150. myDate = calendar.getTime();
  1151. }
  1152. return myDate;
  1153. }
  1154. /**
  1155. * 获取yyyy-MM
  1156. *
  1157. * @return
  1158. */
  1159. public static String getMonthStr() {
  1160. return monthSdf.format(new Date());
  1161. }
  1162. /**
  1163. * 获取时间范围内所有天数 yyyy-MM-dd
  1164. *
  1165. * @param startDate
  1166. * @param endDate
  1167. * @return
  1168. */
  1169. public static List<Date> getDayList(String startDate, String endDate) {
  1170. Date dBegin = getZeroPoint(startDate);
  1171. Date dEnd = getZeroPoint(endDate);
  1172. List<Date> dateList = new ArrayList<>();
  1173. Calendar calBegin = Calendar.getInstance();
  1174. // 使用给定的 Date 设置此 Calendar 的时间
  1175. calBegin.setTime(dBegin);
  1176. Calendar calEnd = Calendar.getInstance();
  1177. // 使用给定的 Date 设置此 Calendar 的时间
  1178. calEnd.setTime(dEnd);
  1179. // 此日期是否在指定日期之后
  1180. dateList.add(dBegin);
  1181. while (dEnd.after(calBegin.getTime())) {
  1182. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1183. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  1184. dateList.add(calBegin.getTime());
  1185. }
  1186. return dateList;
  1187. }
  1188. /**
  1189. * 获取时间范围内所有天 yyyy-MM-dd
  1190. *
  1191. * @param startDate
  1192. * @param endDate
  1193. * @return
  1194. */
  1195. public static List<Date> getDayList(Date startDate, Date endDate) {
  1196. Date dBegin = getZeroPoint(startDate);
  1197. Date dEnd = getZeroPoint(endDate);
  1198. List<Date> dateList = new ArrayList<>();
  1199. Calendar calBegin = Calendar.getInstance();
  1200. // 使用给定的 Date 设置此 Calendar 的时间
  1201. calBegin.setTime(dBegin);
  1202. Calendar calEnd = Calendar.getInstance();
  1203. // 使用给定的 Date 设置此 Calendar 的时间
  1204. calEnd.setTime(dEnd);
  1205. // 此日期是否在指定日期之后
  1206. dateList.add(dBegin);
  1207. while (dEnd.after(calBegin.getTime())) {
  1208. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1209. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  1210. dateList.add(calBegin.getTime());
  1211. }
  1212. return dateList;
  1213. }
  1214. /**
  1215. * 获取时间范围内所有天 yyyy-MM-dd
  1216. *
  1217. * @param startDate
  1218. * @param endDate
  1219. * @return
  1220. */
  1221. public static List<String> getDayStrList(Date startDate, Date endDate) {
  1222. Date dBegin = getZeroPoint(startDate);
  1223. Date dEnd = getZeroPoint(endDate);
  1224. List<String> dateList = new ArrayList<>();
  1225. Calendar calBegin = Calendar.getInstance();
  1226. // 使用给定的 Date 设置此 Calendar 的时间
  1227. calBegin.setTime(dBegin);
  1228. Calendar calEnd = Calendar.getInstance();
  1229. // 使用给定的 Date 设置此 Calendar 的时间
  1230. calEnd.setTime(dEnd);
  1231. // 此日期是否在指定日期之后
  1232. dateList.add(getDate(dBegin));
  1233. while (dEnd.after(calBegin.getTime())) {
  1234. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1235. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  1236. dateList.add(getDate(calBegin.getTime()));
  1237. }
  1238. return dateList;
  1239. }
  1240. /**
  1241. * 获取某个时间段内所有月份
  1242. *
  1243. * @param startDate
  1244. * @param endDate
  1245. * @return
  1246. */
  1247. public static List<Date> getMonthList(String startDate, String endDate) {
  1248. List<Date> result = new ArrayList<>();
  1249. Calendar min = Calendar.getInstance();
  1250. Calendar max = Calendar.getInstance();
  1251. min.setTime(StringToDate(startDate, DateStyleEnum.YYYY_MM));
  1252. min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  1253. max.setTime(StringToDate(endDate, DateStyleEnum.YYYY_MM));
  1254. max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  1255. Calendar curr = min;
  1256. while (curr.before(max)) {
  1257. result.add(curr.getTime());
  1258. curr.add(Calendar.MONTH, 1);
  1259. }
  1260. return result;
  1261. }
  1262. /**
  1263. * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
  1264. *
  1265. * @param date
  1266. * @return
  1267. */
  1268. public static int getSeason(Date date) {
  1269. int season = 0;
  1270. Calendar c = Calendar.getInstance();
  1271. c.setTime(date);
  1272. int month = c.get(Calendar.MONTH);
  1273. switch (month) {
  1274. case Calendar.JANUARY:
  1275. case Calendar.FEBRUARY:
  1276. case Calendar.MARCH:
  1277. season = 1;
  1278. break;
  1279. case Calendar.APRIL:
  1280. case Calendar.MAY:
  1281. case Calendar.JUNE:
  1282. season = 2;
  1283. break;
  1284. case Calendar.JULY:
  1285. case Calendar.AUGUST:
  1286. case Calendar.SEPTEMBER:
  1287. season = 3;
  1288. break;
  1289. case Calendar.OCTOBER:
  1290. case Calendar.NOVEMBER:
  1291. case Calendar.DECEMBER:
  1292. season = 4;
  1293. break;
  1294. default:
  1295. break;
  1296. }
  1297. return season;
  1298. }
  1299. /**
  1300. * 取得季度月
  1301. *
  1302. * @param date
  1303. * @return
  1304. */
  1305. public static Date[] getSeasonDate(Date date) {
  1306. Date[] season = new Date[3];
  1307. Calendar c = Calendar.getInstance();
  1308. c.setTime(date);
  1309. int nSeason = getSeason(date);
  1310. if (nSeason == 1) {// 第一季度
  1311. c.set(Calendar.MONTH, Calendar.JANUARY);
  1312. season[0] = c.getTime();
  1313. c.set(Calendar.MONTH, Calendar.FEBRUARY);
  1314. season[1] = c.getTime();
  1315. c.set(Calendar.MONTH, Calendar.MARCH);
  1316. season[2] = c.getTime();
  1317. } else if (nSeason == 2) {// 第二季度
  1318. c.set(Calendar.MONTH, Calendar.APRIL);
  1319. season[0] = c.getTime();
  1320. c.set(Calendar.MONTH, Calendar.MAY);
  1321. season[1] = c.getTime();
  1322. c.set(Calendar.MONTH, Calendar.JUNE);
  1323. season[2] = c.getTime();
  1324. } else if (nSeason == 3) {// 第三季度
  1325. c.set(Calendar.MONTH, Calendar.JULY);
  1326. season[0] = c.getTime();
  1327. c.set(Calendar.MONTH, Calendar.AUGUST);
  1328. season[1] = c.getTime();
  1329. c.set(Calendar.MONTH, Calendar.SEPTEMBER);
  1330. season[2] = c.getTime();
  1331. } else if (nSeason == 4) {// 第四季度
  1332. c.set(Calendar.MONTH, Calendar.OCTOBER);
  1333. season[0] = c.getTime();
  1334. c.set(Calendar.MONTH, Calendar.NOVEMBER);
  1335. season[1] = c.getTime();
  1336. c.set(Calendar.MONTH, Calendar.DECEMBER);
  1337. season[2] = c.getTime();
  1338. }
  1339. return season;
  1340. }
  1341. /**
  1342. * 取得季度第一天
  1343. *
  1344. * @param date
  1345. * @return
  1346. */
  1347. public static Date getFirstDateOfSeason(Date date) {
  1348. return getFirstDateOfMonth(getSeasonDate(date)[0]);
  1349. }
  1350. /**
  1351. * 取得季度最后一天
  1352. *
  1353. * @param date
  1354. * @return
  1355. */
  1356. public static Date getLastDateOfSeason(Date date) {
  1357. return getLastDateOfMonth(getSeasonDate(date)[2]);
  1358. }
  1359. /**
  1360. * 取得月第一天
  1361. *
  1362. * @param date
  1363. * @return
  1364. */
  1365. public static Date getFirstDateOfMonth(Date date) {
  1366. Calendar c = Calendar.getInstance();
  1367. c.setTime(date);
  1368. c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
  1369. return c.getTime();
  1370. }
  1371. /**
  1372. * 取得月最后一天
  1373. *
  1374. * @param date
  1375. * @return
  1376. */
  1377. public static Date getLastDateOfMonth(Date date) {
  1378. Calendar c = Calendar.getInstance();
  1379. c.setTime(date);
  1380. c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
  1381. return c.getTime();
  1382. }
  1383. /**
  1384. * 时间范围内的每个季度开始时间
  1385. *
  1386. * @param startDate
  1387. * @param endDate
  1388. * @return
  1389. */
  1390. public static List<Date> getSeasonDateList(Date startDate, Date endDate) {
  1391. List<Date> list = new ArrayList<>();
  1392. Date firstDateOfSeason = getFirstDateOfSeason(startDate);
  1393. list.add(firstDateOfSeason);
  1394. Date lastDateOfSeason = getFirstDateOfSeason(endDate);
  1395. Calendar c = Calendar.getInstance();
  1396. c.setTime(firstDateOfSeason);
  1397. while (c.getTime().before(lastDateOfSeason)) {
  1398. c.add(Calendar.MONTH, 3);
  1399. list.add(c.getTime());
  1400. }
  1401. return list;
  1402. }
  1403. /**
  1404. * 获取所在年第一天
  1405. *
  1406. * @param date
  1407. * @return
  1408. */
  1409. public static Date getFirstDayOfYear(Date date) {
  1410. final Calendar cal = Calendar.getInstance();
  1411. cal.setTime(date);
  1412. cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
  1413. return cal.getTime();
  1414. }
  1415. /**
  1416. * 获取所在年第一天
  1417. *
  1418. * @param
  1419. * @return
  1420. */
  1421. public static Date getFirstDayOfYear(String year) {
  1422. final Calendar cal = Calendar.getInstance();
  1423. cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
  1424. cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
  1425. return cal.getTime();
  1426. }
  1427. /**
  1428. * 获取所在年最后一天
  1429. *
  1430. * @param date
  1431. * @return
  1432. */
  1433. public static Date getLastDayOfYear(Date date) {
  1434. final Calendar cal = Calendar.getInstance();
  1435. cal.setTime(date);
  1436. cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
  1437. return cal.getTime();
  1438. }
  1439. /**
  1440. * 获取所在年最后一天
  1441. *
  1442. * @param year
  1443. * @return
  1444. */
  1445. public static Date getLastDayOfYear(String year) {
  1446. final Calendar cal = Calendar.getInstance();
  1447. cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
  1448. cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
  1449. return cal.getTime();
  1450. }
  1451. /**
  1452. * 时间范围内的每个年开始时间列表
  1453. *
  1454. * @param startDate
  1455. * @param endDate
  1456. * @return
  1457. */
  1458. public static List<Date> getYearDateList(Date startDate, Date endDate) {
  1459. List<Date> list = new ArrayList<>();
  1460. Date firstDate = getFirstDayOfYear(startDate);
  1461. list.add(firstDate);
  1462. Date lastDate = getFirstDayOfYear(endDate);
  1463. Calendar c = Calendar.getInstance();
  1464. c.setTime(firstDate);
  1465. while (c.getTime().before(lastDate)) {
  1466. c.add(Calendar.YEAR, 1);
  1467. list.add(c.getTime());
  1468. }
  1469. return list;
  1470. }
  1471. public static String getChineseDate(Date date) {
  1472. String chineseDate = null;
  1473. SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
  1474. return chineseDate;
  1475. }
  1476. public static StringBuilder getChineseTime(Date date) {
  1477. String sign;
  1478. StringBuilder chineseTime = new StringBuilder();
  1479. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  1480. String time = sdf.format(date);
  1481. String[] split = time.split(":");
  1482. if(split[0].equals("00")){
  1483. chineseTime.append("零时");
  1484. }else{
  1485. String s = formatDigit(split[0]);
  1486. String substring1 = s.substring(0, 1);
  1487. String substring2 = s.substring(1, 2);
  1488. if(substring1.equals("零")){
  1489. }else if(substring1.equals("一")){
  1490. chineseTime.append("十");
  1491. }else{
  1492. chineseTime.append(substring1).append("十");
  1493. }
  1494. if(!substring2.equals("零")){
  1495. chineseTime.append(substring2);
  1496. }
  1497. chineseTime.append("时");
  1498. }
  1499. if(split[1].equals("00")){
  1500. chineseTime.append("整");
  1501. }else {
  1502. String s = formatDigit(split[1]);
  1503. String substring1 = s.substring(0, 1);
  1504. String substring2 = s.substring(1, 2);
  1505. if(substring1.equals("零")){
  1506. }else if(substring1.equals("一")){
  1507. chineseTime.append("十");
  1508. }else{
  1509. chineseTime.append(substring1).append("十");
  1510. }
  1511. if(!substring2.equals("零")){
  1512. chineseTime.append(substring2);
  1513. }
  1514. chineseTime.append("分");
  1515. }
  1516. //
  1517. return chineseTime;
  1518. }
  1519. private static String formatDigit(String sign) {
  1520. StringBuilder sb = new StringBuilder();
  1521. for(int i=0;i<sign.length();i++){
  1522. String substring = sign.substring(i, i + 1);
  1523. switch (substring){
  1524. case "0":
  1525. sb.append("零");
  1526. break;
  1527. case "1":
  1528. sb.append("一");
  1529. break;
  1530. case "2":
  1531. sb.append("二");
  1532. break;
  1533. case "3":
  1534. sb.append("三");
  1535. break;
  1536. case "4":
  1537. sb.append("四");
  1538. break;
  1539. case "5":
  1540. sb.append("五");
  1541. break;
  1542. case "6":
  1543. sb.append("六");
  1544. break;
  1545. case "7":
  1546. sb.append("七");
  1547. break;
  1548. case "8":
  1549. sb.append("八");
  1550. break;
  1551. case "9":
  1552. sb.append("九");
  1553. break;
  1554. }
  1555. }
  1556. sign = sb.toString();
  1557. return sign;
  1558. }
  1559. /**
  1560. * 取出当前日期到指定日期的周数据差值
  1561. *
  1562. * @param date 指日 定日期
  1563. * @param otherDate 当前日期
  1564. * @return 周数差
  1565. */
  1566. public static long intervalWeeks(Date date, Date otherDate) {
  1567. int intervalDays = getIntervalDays(date, otherDate) + 1;
  1568. int i = intervalDays % 7;
  1569. long weekNum = Math.abs((date.getTime() - otherDate.getTime()) / (7 * 60 * 60 * 24 * 1000));
  1570. weekNum += 1;
  1571. return weekNum;
  1572. }
  1573. public static long intervalWeeks(String date, String otherDate) {
  1574. Date date1 = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  1575. Date date2 = DateUtils.StringToDate(otherDate, DateStyleEnum.YYYY_MM_DD);
  1576. return intervalWeeks(date1, date2);
  1577. }
  1578. /**
  1579. * 获取两个时间相差多少小时多少分钟
  1580. *
  1581. * @param startTime
  1582. * @param endTime
  1583. * @return
  1584. */
  1585. public static String getDistanceHourMin(Date startTime, Date endTime) {
  1586. long hour = 0;
  1587. long min = 0;
  1588. try {
  1589. long time1 = startTime.getTime();
  1590. long time2 = endTime.getTime();
  1591. long diff;
  1592. if (time1 < time2) {
  1593. diff = time2 - time1;
  1594. } else {
  1595. diff = time1 - time2;
  1596. }
  1597. hour = (diff / (60 * 60 * 1000));
  1598. min = ((diff / (60 * 1000)) - hour * 60);
  1599. } catch (Exception e) {
  1600. e.printStackTrace();
  1601. }
  1602. return hour + "小时" + min + "分";
  1603. }
  1604. public static Date getDateHourMin(Date date) {
  1605. if (date == null) {
  1606. return null;
  1607. }
  1608. return StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM), DateStyleEnum.YYYY_MM_DD_HH_MM);
  1609. }
  1610. /**
  1611. * 判断一个日期是否是周六、周日
  1612. * @param date
  1613. * @return
  1614. * @throws ParseException
  1615. */
  1616. public static int isWeekend(String date) throws ParseException {
  1617. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
  1618. Date bdate = format1.parse(date);
  1619. Calendar cal = Calendar.getInstance();
  1620. cal.setTime(bdate);
  1621. if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  1622. return 1;
  1623. } else {
  1624. return 0;
  1625. }
  1626. }
  1627. }