613bdc59e1118baf44a58cdb57f41b81d8638e7f.svn-base 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. package cn.com.goldenwater.dcproj.utils;
  2. import java.lang.management.ManagementFactory;
  3. import java.sql.Timestamp;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. /**
  8. * @author ben.chen
  9. * @version 创建时间:2012-8-21
  10. * 时间处理
  11. */
  12. public class DateUtils {
  13. public static Date getAddDay(Date stTm, int size) {
  14. Calendar c = Calendar.getInstance();
  15. c.setTime(stTm);
  16. c.add(Calendar.DAY_OF_MONTH, size);
  17. return c.getTime();
  18. }
  19. public static TimeZone zone = TimeZone.getTimeZone("GMT+8");
  20. /**
  21. * 获取服务器启动时间
  22. */
  23. public static Date getServerStartDate()
  24. {
  25. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  26. return new Date(time);
  27. }
  28. /**
  29. * 计算两个时间差
  30. */
  31. public static String getDatePoor(Date endDate, Date nowDate)
  32. {
  33. long nd = 1000 * 24 * 60 * 60;
  34. long nh = 1000 * 60 * 60;
  35. long nm = 1000 * 60;
  36. // long ns = 1000;
  37. // 获得两个时间的毫秒时间差异
  38. long diff = endDate.getTime() - nowDate.getTime();
  39. // 计算差多少天
  40. long day = diff / nd;
  41. // 计算差多少小时
  42. long hour = diff % nd / nh;
  43. // 计算差多少分钟
  44. long min = diff % nd % nh / nm;
  45. // 计算差多少秒//输出结果
  46. // long sec = diff % nd % nh % nm / ns;
  47. return day + "天" + hour + "小时" + min + "分钟";
  48. }
  49. /**
  50. * change the number to Calendar
  51. *
  52. * @param numDate YYYYMMDD
  53. * @return Calendar
  54. */
  55. public static Calendar toCalendar(int numDate) {
  56. int n = numDate;
  57. int day = n % 100;
  58. n /= 100;
  59. int month = n % 100;
  60. n /= 100;
  61. int year = n;
  62. Calendar calendar = new GregorianCalendar(year, month - 1, day);
  63. return calendar;
  64. }
  65. /**
  66. * add specified months to the specified date
  67. *
  68. * @param numDate (YYYYMMDD) of date
  69. * @param month number of months to be added
  70. * @return added date(a number) or -1 whenever error
  71. */
  72. public static int addMonth(int numDate, int month) {
  73. try {
  74. // change number to Calendar
  75. Calendar calendar = toCalendar(numDate);
  76. // add specified months
  77. calendar.add(Calendar.MONTH, month);
  78. // to check if it is a valid date
  79. int returnY = calendar.get(Calendar.YEAR);
  80. int returnM = calendar.get(Calendar.MONTH) + 1;
  81. int returnD = calendar.get(Calendar.DATE);
  82. return (returnY * 10000 + returnM * 100 + returnD);
  83. } catch (NumberFormatException ex) {
  84. return -1;
  85. }
  86. }
  87. /**
  88. * add specified days to the specified date
  89. *
  90. * @param numDate (YYYYMMDD) of date
  91. * @param day number of days to be added
  92. * @return added date(a number) or -1 whenever error
  93. */
  94. public static int addDay(int numDate, int day) {
  95. try {
  96. // change number to Calendar
  97. Calendar calendar = toCalendar(numDate);
  98. // add specified days
  99. calendar.add(Calendar.DATE, day);
  100. int returnY = calendar.get(Calendar.YEAR);
  101. int returnM = calendar.get(Calendar.MONTH) + 1;
  102. int returnD = calendar.get(Calendar.DATE);
  103. return (returnY * 10000 + returnM * 100 + returnD);
  104. } catch (NumberFormatException ex) {
  105. return -1;
  106. }
  107. }
  108. /**
  109. * subtract of two dates
  110. *
  111. * @param minDate (YYYYMMDD) of smaller date
  112. * @param maxDate (YYYYMMDD) of bigger date
  113. * @return days between the two dates(int)<BR>
  114. * or -1 whenever error occurs
  115. */
  116. public static int subDate(int minDate, int maxDate) {
  117. // smaller date > bigger date => error
  118. if (minDate > maxDate) {
  119. return -1;
  120. }
  121. // to check if the input date is a valid date
  122. if (!isValidDate(minDate)) {
  123. return -1;
  124. }
  125. if (!isValidDate(maxDate)) {
  126. return -1;
  127. }
  128. try {
  129. // change number to Calendar
  130. Calendar calcMin = toCalendar(minDate);
  131. Calendar calcMax = toCalendar(maxDate);
  132. long lngMinMilSec;
  133. long lngMaxMilSec;
  134. // miliseconds
  135. lngMinMilSec = (calcMin.getTime()).getTime();
  136. lngMaxMilSec = (calcMax.getTime()).getTime();
  137. // substract by miliseconds
  138. return (int) ((lngMaxMilSec - lngMinMilSec) / (1000 * 60 * 60 * 24));
  139. } catch (NumberFormatException ex) {
  140. return -1;
  141. }
  142. }
  143. /**
  144. * to deside if the input number is a valid date
  145. *
  146. * @param numDate (YYYYMMDD)
  147. * @return true(if valid), false(if invalid)
  148. */
  149. public static boolean isValidDate(int numDate) {
  150. String strDate = Integer.toString(numDate);
  151. if (strDate.length() != 8) {
  152. return false;
  153. }
  154. int fullYear = Integer.parseInt(strDate.substring(0, 4));
  155. int month = Integer.parseInt(strDate.substring(4, 6));
  156. int day = Integer.parseInt(strDate.substring(6, 8));
  157. if (isValid(fullYear, month, day) == false) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * to deside if the inputs(seperated year,month,day) is a valid date
  164. *
  165. * @param intYear year
  166. * @param intMonth month (1~12)
  167. * @param intDay day
  168. * @return true(if valid), false(if invalid)
  169. */
  170. public static boolean isValid(int intYear, int intMonth, int intDay) {
  171. Calendar calendar = new GregorianCalendar(intYear, intMonth - 1, intDay);
  172. if ((calendar.get(Calendar.YEAR) != intYear) ||
  173. (calendar.get(Calendar.MONTH) != (intMonth - 1)) ||
  174. (calendar.get(Calendar.DAY_OF_MONTH) != intDay)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. /* ************************** 转换函数 ****************************** */
  180. /**
  181. * switch a String to a Calendar
  182. *
  183. * @param input the String to be switched
  184. * @param pattern the specified pattern
  185. * @return output the Calendar has been switched
  186. */
  187. public static Calendar Str2Calendar(String input, String pattern) {
  188. Calendar output = null;
  189. if (input != null) {
  190. try {
  191. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  192. sdf.setTimeZone(DateUtils.zone);
  193. Date date = sdf.parse(input);
  194. output = Date2Calendar(date);
  195. } catch (ParseException e) {
  196. System.err.println(e);
  197. return null;
  198. }
  199. return output;
  200. } else {
  201. return null;
  202. }
  203. }
  204. /**
  205. * 转换(yyyy-mm-dd) to Calendar
  206. *
  207. * @param input a String with format of yyyy-mm-dd to be switched to Date
  208. * @return cal the Calendar has been switched
  209. */
  210. public static Calendar Str2CalendarYMD(String input) {
  211. if (input == null || "".equals(input.trim())) {
  212. return null;
  213. }
  214. GregorianCalendar cal = new GregorianCalendar();
  215. cal.set(Integer.parseInt(input.substring(0, 4)), Integer.parseInt(input
  216. .substring(5, 7)) - 1, Integer.parseInt(input.substring(8, 10)));
  217. return cal;
  218. }
  219. /**
  220. * 转换字符串格式(yyyy-mm-dd hh) to Calendar
  221. * 如:
  222. * 2007-05-01 08:00
  223. * 2007-06-01 08:00
  224. *
  225. * @param input a String with format of yyyy-mm-dd hh to be switched to Date
  226. * @return cal the Calendar has been switched
  227. */
  228. public static Calendar Str2CalendarYMDH(String input) {
  229. if (input == null || "".equals(input.trim())) {
  230. return null;
  231. }
  232. return Str2Calendar(input, "yyyy-MM-dd HH");
  233. }
  234. /**
  235. * 转换(yyyy-mm-dd hh:mm) to Calendar
  236. *
  237. * @param input a String with format of yyyy-mm-dd hh:mm to be switched to Date
  238. * @return cal the Calendar has been switched
  239. */
  240. public static Calendar Str2CalendarYMDHM(String input) {
  241. if (input == null || "".equals(input.trim())) {
  242. return null;
  243. }
  244. return Str2Calendar(input, "yyyy-MM-dd HH:mm");
  245. }
  246. /**
  247. * 转换(yyyy-mm-dd hh:mm:ss) to Calendar
  248. *
  249. * @param input a String with format of yyyy-mm-dd hh:mm:ss to be switched to Date
  250. * @return cal the Calendar has been switched
  251. */
  252. public static Calendar Str2CalendarYMDHMS(String input) {
  253. if (input == null || "".equals(input.trim())) {
  254. return null;
  255. }
  256. GregorianCalendar cal = new GregorianCalendar();
  257. cal.set(Integer.parseInt(input.substring(0, 4)),
  258. Integer.parseInt(input.substring(5, 7)) - 1,
  259. Integer.parseInt(input.substring(8, 10)),
  260. Integer.parseInt(input.substring(11, 13)),
  261. Integer.parseInt(input.substring(14, 16)),
  262. Integer.parseInt(input.substring(17, 19)));
  263. return cal;
  264. }
  265. /**
  266. * 转换(hh:mm) to Calendar
  267. *
  268. * @param input a String with format of hh:mm to be switched to Date
  269. * @return cal the Calendar has been switched
  270. */
  271. public static Calendar Str2CalendarHM(String input) {
  272. if (input == null || "".equals(input.trim())) {
  273. return null;
  274. }
  275. Calendar now = new GregorianCalendar();
  276. GregorianCalendar cal = new GregorianCalendar();
  277. cal.set(now.get(Calendar.YEAR),
  278. now.get(Calendar.MONTH),
  279. now.get(Calendar.DAY_OF_MONTH),
  280. Integer.parseInt(input.substring(0, 2)),
  281. Integer.parseInt(input.substring(3, 5)),
  282. 0);
  283. return cal;
  284. }
  285. /**
  286. * 转换(hh:mm:ss) to Calendar
  287. *
  288. * @param input a String with format of hh:mm:ss to be switched to Date
  289. * @return cal the Calendar has been switched
  290. */
  291. public static Calendar Str2CalendarHMS(String input) {
  292. if (input == null || "".equals(input.trim())) {
  293. return null;
  294. }
  295. Calendar now = new GregorianCalendar();
  296. GregorianCalendar cal = new GregorianCalendar();
  297. cal.set(now.get(Calendar.YEAR),
  298. now.get(Calendar.MONTH),
  299. now.get(Calendar.DAY_OF_MONTH),
  300. Integer.parseInt(input.substring(0, 2)),
  301. Integer.parseInt(input.substring(3, 5)),
  302. Integer.parseInt(input.substring(6, 8)));
  303. return cal;
  304. }
  305. /**
  306. * 转换字符串 to a Date
  307. *
  308. * @param input the String to be switched
  309. * @param pattern the specified pattern
  310. * @return output the Date has been switched
  311. */
  312. public static Date Str2Date(String input, String pattern) {
  313. Date output = null;
  314. if (input != null) {
  315. try {
  316. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  317. sdf.setTimeZone(DateUtils.zone);
  318. output = sdf.parse(input);
  319. } catch (ParseException e) {
  320. System.err.println(e);
  321. return null;
  322. }
  323. return output;
  324. } else {
  325. return null;
  326. }
  327. }
  328. /**
  329. * 转换字符串(yyyy-mm-dd) to a Date
  330. *
  331. * @param input a String with format of yyyy-mm-dd to be switched to Date
  332. * @return output the Date has been switched
  333. */
  334. public static Date Str2Date(String input) {
  335. Date output = null;
  336. if (input != null) {
  337. Calendar cal = Str2CalendarYMD(input);
  338. output = cal.getTime();
  339. return output;
  340. } else {
  341. return null;
  342. }
  343. }
  344. /**
  345. * 转换字符串(yyyy-mm-dd hh:mm) to a Date
  346. *
  347. * @param input a String with format of yyyy-mm-dd hh:mm to be switched to Date
  348. * @return output the Date has been switched
  349. */
  350. public static Date Str2DateYMDHM(String input) {
  351. Date output = null;
  352. if (input != null) {
  353. Calendar cal = Str2CalendarYMDHM(input);
  354. output = cal.getTime();
  355. return output;
  356. } else {
  357. return null;
  358. }
  359. }
  360. /**
  361. * 转换字符串(yyyy-mm-dd hh:mm:ss) to a Date
  362. *
  363. * @param input a String with format of yyyy-mm-dd hh:mm:ss to be switched to Date
  364. * @return output the Date has been switched
  365. */
  366. public static Date Str2DateYMDHMS(String input) {
  367. Date output = null;
  368. if (input != null) {
  369. Calendar cal = Str2CalendarYMDHMS(input);
  370. output = cal.getTime();
  371. return output;
  372. } else {
  373. return null;
  374. }
  375. }
  376. /**
  377. * 转换字符串(hh:mm) to a Date
  378. *
  379. * @param input a String with format of hh:mm to be switched to Date
  380. * @return output the Date has been switched
  381. */
  382. public static Date Str2DateHM(String input) {
  383. Date output = null;
  384. if (input != null) {
  385. Calendar cal = Str2CalendarHM(input);
  386. output = cal.getTime();
  387. return output;
  388. } else {
  389. return null;
  390. }
  391. }
  392. /**
  393. * 转换字符串(hh:mm:ss) to a Date
  394. *
  395. * @param input a String with format of hh:mm:ss to be switched to Date
  396. * @return output the Date has been switched
  397. */
  398. public static Date Str2DateHMS(String input) {
  399. Date output = null;
  400. if (input != null) {
  401. Calendar cal = Str2CalendarHMS(input);
  402. output = cal.getTime();
  403. return output;
  404. } else {
  405. return null;
  406. }
  407. }
  408. /**
  409. * switch Date to Calendar
  410. *
  411. * @param date
  412. * @return cal
  413. */
  414. public static Calendar Date2Calendar(Date date) {
  415. Calendar cal = Calendar.getInstance();
  416. cal.setTimeInMillis(date.getTime());
  417. return cal;
  418. }
  419. /**
  420. * 转换日期为指定格式的字符串
  421. *
  422. * @param date
  423. * @param pattern 返回格式,如:yyyy-MM-dd HH:mm:ss
  424. * @return String
  425. */
  426. public static String Date2Str(Date date, String pattern) {
  427. if (date == null) {
  428. return "";
  429. }
  430. SimpleDateFormat time = new SimpleDateFormat(pattern);
  431. time.setTimeZone(DateUtils.zone);
  432. return time.format(date);
  433. }
  434. /**
  435. * 转换Calendar为指定格式的字符串
  436. *
  437. * @param cal
  438. * @param pattern 返回格式,如:yyyy-MM-dd HH:mm:ss
  439. * @return String
  440. */
  441. public static String Calendar2Str(Calendar cal, String pattern) {
  442. SimpleDateFormat time = new SimpleDateFormat(pattern);
  443. time.setTimeZone(DateUtils.zone);
  444. Date date = cal.getTime();
  445. return time.format(date);
  446. }
  447. /**
  448. * 转换时间到字符串(yyyy-MM-dd)
  449. *
  450. * @param date
  451. * @return String
  452. */
  453. public static String Date2Str(Date date) {
  454. SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
  455. time.setTimeZone(DateUtils.zone);
  456. return time.format(date);
  457. }
  458. /**
  459. * 转换时间到字符串(yyyy-MM-dd HH:mm)
  460. *
  461. * @param date
  462. * @return String
  463. */
  464. public static String Date2StrYMDHM(Date date) {
  465. SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  466. time.setTimeZone(DateUtils.zone);
  467. return time.format(date);
  468. }
  469. /**
  470. * 转换时间到字符串(yyyy-MM-dd HH:mm:ss)
  471. *
  472. * @param date
  473. * @return String
  474. */
  475. public static String Date2StrYMDHMS(Date date) {
  476. SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  477. time.setTimeZone(DateUtils.zone);
  478. return time.format(date);
  479. }
  480. /**
  481. * 转换时间到字符串(HH:mm)
  482. *
  483. * @param date
  484. * @return String
  485. */
  486. public static String Date2StrHM(Date date) {
  487. SimpleDateFormat time = new SimpleDateFormat("HH:mm");
  488. time.setTimeZone(DateUtils.zone);
  489. return time.format(date);
  490. }
  491. /**
  492. * 转换时间到字符串(HH:mm:ss)
  493. *
  494. * @param date
  495. * @return String
  496. */
  497. public static String Date2StrHMS(Date date) {
  498. SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
  499. time.setTimeZone(DateUtils.zone);
  500. return time.format(date);
  501. }
  502. /* ******************* 取得当前日期 ********************* */
  503. /**
  504. * get today (yyyy-MM-dds)
  505. *
  506. * @return
  507. */
  508. public static String getTodayYMD() {
  509. return getToday("yyyy-MM-dd");
  510. }
  511. /**
  512. * get today (yyyy-MM-dd HH:mm)
  513. *
  514. * @return
  515. */
  516. public static String getTodayYMDHM() {
  517. return getToday("yyyy-MM-dd HH:mm");
  518. }
  519. /**
  520. * get today (yyyy-MM-dd HH:mm:ss)
  521. *
  522. * @return
  523. */
  524. public static String getTodayYMDHMS() {
  525. return getToday("yyyy-MM-dd HH:mm:ss");
  526. }
  527. /**
  528. * 取得当前日期按指定格式显示的字符串
  529. *
  530. * @param format
  531. * @return
  532. */
  533. public static String getToday(String format) {
  534. Date date = new Date();
  535. SimpleDateFormat sd = new SimpleDateFormat(format);
  536. sd.setTimeZone(DateUtils.zone);
  537. return sd.format(date);
  538. }
  539. /* ******************* 取得上月今天 ********************* */
  540. /**
  541. * get today of last month
  542. *
  543. * @return
  544. */
  545. public static String getLastMonthDate() {
  546. GregorianCalendar cal = new GregorianCalendar();
  547. int year = cal.get(Calendar.YEAR);
  548. int month = cal.get(Calendar.MONTH);
  549. int day = cal.get(Calendar.DAY_OF_MONTH);
  550. if (month == 1) {
  551. // 如果今天是1月某天,计算上月今天则为去年12月某天
  552. year = year - 1;
  553. month = 12;
  554. } else {
  555. // 如果今天不是1月某天,计算上月今天则为上月某天
  556. month = month - 1;
  557. }
  558. Calendar lastMonthLastDay = getBeforeLastDay(cal);
  559. int lastMonthDay = lastMonthLastDay.get(Calendar.DAY_OF_MONTH);
  560. if (lastMonthDay < day) {
  561. day = lastMonthDay;
  562. }
  563. Calendar lastMonthToday = new GregorianCalendar(year, month, day);
  564. return Date2Str(lastMonthToday.getTime());
  565. }
  566. /* ******************* 取得指定月的上月今天 ********************* */
  567. /**
  568. * get the time on designated of last month
  569. *
  570. * @return
  571. */
  572. public static String getOnDesignatedLastMonthDate(String time) {
  573. Calendar cal = DateUtils.Str2CalendarYMDHMS(time);
  574. int year = cal.get(Calendar.YEAR);
  575. int month = cal.get(Calendar.MONTH);
  576. int day = cal.get(Calendar.DAY_OF_MONTH);
  577. int hour = cal.get(Calendar.HOUR_OF_DAY);
  578. int minute = cal.get(Calendar.MINUTE);
  579. int second = cal.get(Calendar.SECOND);
  580. if (month == 1) {
  581. // 如果天是1月某天,计算上月今天则为去年12月某天
  582. year = year - 1;
  583. month = 12;
  584. } else {
  585. // 如果天不是1月某天,计算上月今天则为上月某天
  586. month = month - 1;
  587. }
  588. Calendar lastMonthLastDay = getBeforeLastDay(cal);
  589. int lastMonthDay = lastMonthLastDay.get(Calendar.DAY_OF_MONTH);
  590. if (lastMonthDay < day) {
  591. day = lastMonthDay;
  592. }
  593. Calendar lastMonthToday = new GregorianCalendar(year, month, day, hour, minute, second);
  594. return Date2StrYMDHMS(lastMonthToday.getTime());
  595. }
  596. /* ******************* 取得指定月的下月今天 ********************* */
  597. /**
  598. * get the time on designated of last month
  599. *
  600. * @return
  601. */
  602. public static String getOnDesignatedNextMonthDate(String time) {
  603. Calendar cal = DateUtils.Str2CalendarYMDHMS(time);
  604. int year = cal.get(Calendar.YEAR);
  605. int month = cal.get(Calendar.MONTH);
  606. int day = cal.get(Calendar.DAY_OF_MONTH);
  607. int hour = cal.get(Calendar.HOUR_OF_DAY);
  608. int minute = cal.get(Calendar.MINUTE);
  609. int second = cal.get(Calendar.SECOND);
  610. if (month == 12) {
  611. // 如果天是1月某天,计算上月今天则为去年12月某天
  612. year = year + 1;
  613. month = 1;
  614. } else {
  615. // 如果天不是1月某天,计算上月今天则为上月某天
  616. month = month + 1;
  617. }
  618. Calendar lastMonthLastDay = getAfterLastDay(cal);
  619. int lastMonthDay = lastMonthLastDay.get(Calendar.DAY_OF_MONTH);
  620. if (lastMonthDay < day) {
  621. day = lastMonthDay;
  622. }
  623. Calendar lastMonthToday = new GregorianCalendar(year, month, day, hour, minute, second);
  624. return Date2StrYMDHMS(lastMonthToday.getTime());
  625. }
  626. public static String getLastYearDate() {
  627. GregorianCalendar cal = new GregorianCalendar();
  628. int year = cal.get(Calendar.YEAR);
  629. int month = cal.get(Calendar.MONTH);
  630. int day = cal.get(Calendar.DAY_OF_MONTH);
  631. year = year - 1;
  632. Calendar lastMonthToday = new GregorianCalendar(year, month, day);
  633. return Date2Str(lastMonthToday.getTime());
  634. }
  635. /* ******************* 一些关于旬的方法 ********************* */
  636. /**
  637. * 判断日期是否是一旬的开始日
  638. *
  639. * @param cal
  640. * @return true if 1日、11日、21日
  641. */
  642. public static boolean isStartOf10Days(Calendar cal) {
  643. boolean ret = false;
  644. int intDay = cal.get(Calendar.DAY_OF_MONTH);
  645. if (intDay == 1 || intDay == 11 || intDay == 21) {
  646. ret = true;
  647. }
  648. return ret;
  649. }
  650. /**
  651. * 判断日期是否是一旬的结束日
  652. *
  653. * @param cal
  654. * @return true if 10日、20日、当月最后一天
  655. */
  656. public static boolean isEndOf10Days(Calendar cal) {
  657. boolean ret = false;
  658. int intDay = cal.get(Calendar.DAY_OF_MONTH);
  659. if (intDay == 10 || intDay == 20 || calEquals(cal, getLastDay(cal)) == true) {
  660. ret = true;
  661. }
  662. return ret;
  663. }
  664. /**
  665. * 判断两个日期的年月日部分是否相同
  666. *
  667. * @param first
  668. * @param second
  669. * @return
  670. */
  671. private static boolean calEquals(Calendar first, Calendar second) {
  672. return Date2Str(first.getTime()).equals(Date2Str(second.getTime()));
  673. }
  674. /**
  675. * 判断两个日期是否在同一个旬中
  676. * cal1、cal2都不是某旬的开始日或结束日
  677. *
  678. * @param cal1
  679. * @param cal2
  680. * @return
  681. */
  682. public static boolean isInSame10Days(Calendar cal1, Calendar cal2) {
  683. boolean ret = false;
  684. // 取得给出日期之前出现的最后一个旬的开始日
  685. Calendar calStart = getS10DaysBefore(cal1);
  686. // 取得给出日期之后出现的第一个旬的结束日
  687. Calendar calEnd = getE10DaysAfter(cal1);
  688. if (cal2.after(calStart) == true && cal2.before(calEnd) == true) {
  689. ret = true;
  690. }
  691. return ret;
  692. }
  693. /**
  694. * 取得当月第一天
  695. *
  696. * @param cal
  697. * @return
  698. */
  699. private static Calendar getFirstDay(Calendar cal) {
  700. Calendar ret = (Calendar) cal.clone();
  701. ret.set(Calendar.DAY_OF_MONTH, 1);
  702. return ret;
  703. }
  704. /**
  705. * 取得当月最后一天
  706. *
  707. * @param cal
  708. * @return
  709. */
  710. private static Calendar getLastDay(Calendar cal) {
  711. Calendar ret = (Calendar) cal.clone();
  712. ret.add(Calendar.MONTH, 1);
  713. ret.set(Calendar.DAY_OF_MONTH, 1);
  714. ret.add(Calendar.DAY_OF_MONTH, -1);
  715. return ret;
  716. }
  717. /**
  718. * 取得次月第一天
  719. *
  720. * @param cal
  721. * @return
  722. */
  723. public static Calendar getNextFirstDay(Calendar cal) {
  724. Calendar ret = getFirstDay(cal);
  725. ret.add(Calendar.MONTH, 1);
  726. return ret;
  727. }
  728. /**
  729. * 取得次月最后一天
  730. *
  731. * @param cal
  732. * @return
  733. */
  734. public static Calendar getNextLastDay(Calendar cal) {
  735. Calendar ret = (Calendar) cal.clone();
  736. ret.add(Calendar.MONTH, 1);
  737. ret = getLastDay(ret);
  738. return ret;
  739. }
  740. /**
  741. * 取得前月第一天
  742. *
  743. * @param cal
  744. * @return
  745. */
  746. public static Calendar getBeforeFirstDay(Calendar cal) {
  747. Calendar ret = getFirstDay(cal);
  748. ret.add(Calendar.MONTH, -1);
  749. return ret;
  750. }
  751. /**
  752. * 取得前月最后一天
  753. *
  754. * @param cal
  755. * @return
  756. */
  757. public static Calendar getBeforeLastDay(Calendar cal) {
  758. Calendar ret = (Calendar) cal.clone();
  759. ret.add(Calendar.MONTH, -1);
  760. ret = getLastDay(ret);
  761. return ret;
  762. }
  763. /**
  764. * 取得下月最后一天
  765. *
  766. * @param cal
  767. * @return
  768. */
  769. public static Calendar getAfterLastDay(Calendar cal) {
  770. Calendar ret = (Calendar) cal.clone();
  771. ret.add(Calendar.MONTH, +1);
  772. ret = getLastDay(ret);
  773. return ret;
  774. }
  775. /**
  776. * 取得给出日期之前出现的最后一个旬的开始日
  777. *
  778. * @param cal
  779. * @return
  780. */
  781. public static Calendar getS10DaysBefore(Calendar cal) {
  782. int day = cal.get(Calendar.DAY_OF_MONTH);
  783. Calendar ret = (Calendar) cal.clone();
  784. if (day == 1 || day == 11 || day == 21) {
  785. ret = getStart10DaysBefore(ret);
  786. } else if (day > 1 && day < 11) {
  787. ret.set(Calendar.DAY_OF_MONTH, 1);
  788. } else if (day > 11 && day < 21) {
  789. ret.set(Calendar.DAY_OF_MONTH, 11);
  790. } else {
  791. ret.set(Calendar.DAY_OF_MONTH, 21);
  792. }
  793. return ret;
  794. }
  795. /**
  796. * 取得给出日期之后出现的第一个旬的开始日
  797. *
  798. * @param cal
  799. * @return
  800. */
  801. public static Calendar getS10DaysAfter(Calendar cal) {
  802. int day = cal.get(Calendar.DAY_OF_MONTH);
  803. Calendar ret = (Calendar) cal.clone();
  804. if (day == 1 || day == 11 || day == 21) {
  805. ret = getStart10DaysAfter(ret);
  806. } else if (day > 1 && day < 11) {
  807. ret.set(Calendar.DAY_OF_MONTH, 11);
  808. } else if (day > 11 && day < 21) {
  809. ret.set(Calendar.DAY_OF_MONTH, 21);
  810. } else {
  811. ret.set(Calendar.DAY_OF_MONTH, 21);
  812. ret = getStart10DaysAfter(ret);
  813. }
  814. return ret;
  815. }
  816. /**
  817. * 取得给出日期之前出现的最后一个旬的结束日
  818. *
  819. * @param cal
  820. * @return
  821. */
  822. public static Calendar getE10DaysBefore(Calendar cal) {
  823. int day = cal.get(Calendar.DAY_OF_MONTH);
  824. Calendar ret = (Calendar) cal.clone();
  825. if (day == 1 || day == 11 || day == 21) {
  826. ret.add(Calendar.DAY_OF_MONTH, -1);
  827. } else if (day > 1 && day < 11) {
  828. ret = getBeforeLastDay(ret);
  829. } else if (day > 11 && day < 21) {
  830. ret.set(Calendar.DAY_OF_MONTH, 10);
  831. } else {
  832. ret.set(Calendar.DAY_OF_MONTH, 20);
  833. }
  834. return ret;
  835. }
  836. /**
  837. * 取得给出日期之后出现的第一个旬的结束日
  838. *
  839. * @param cal
  840. * @return
  841. */
  842. public static Calendar getE10DaysAfter(Calendar cal) {
  843. int day = cal.get(Calendar.DAY_OF_MONTH);
  844. Calendar ret = (Calendar) cal.clone();
  845. if (day == 10 || day == 20 || day == getLastDay(ret).get(Calendar.DAY_OF_MONTH)) {
  846. // 原值返回
  847. } else if (day >= 1 && day < 10) {
  848. ret.set(Calendar.DAY_OF_MONTH, 10);
  849. } else if (day >= 11 && day < 20) {
  850. ret.set(Calendar.DAY_OF_MONTH, 20);
  851. } else {
  852. ret = getLastDay(ret);
  853. }
  854. return ret;
  855. }
  856. /**
  857. * 取得1旬之后的开始日期
  858. *
  859. * @param cal 旬的开始时间(1日或11日或21)
  860. * @return
  861. */
  862. public static Calendar getStart10DaysAfter(Calendar cal) {
  863. Calendar ret = (Calendar) cal.clone();
  864. int day = cal.get(Calendar.DAY_OF_MONTH);
  865. if (day != 21) {
  866. // 1日或11日,直接加10天,则为下一个旬的开始日
  867. ret.add(Calendar.DAY_OF_MONTH, 10);
  868. } else {
  869. // 21日,取次月1日为下一个旬的开始日
  870. ret = getLastDay(ret);
  871. ret.add(Calendar.DAY_OF_MONTH, 1);
  872. }
  873. return ret;
  874. }
  875. /**
  876. * 取得1旬之后的结束日期
  877. *
  878. * @param cal 旬的开始时间(1日或11日或21)
  879. * @return
  880. */
  881. public static Calendar getEnd10DaysAfter(Calendar cal) {
  882. Calendar ret = getStart10DaysAfter(cal);
  883. ret.add(Calendar.DAY_OF_MONTH, -1);
  884. return ret;
  885. }
  886. /**
  887. * 取得1旬之前的结束日期
  888. *
  889. * @param cal 旬的结束日 (10日或20日或月末)
  890. * @return
  891. */
  892. private static Calendar getEnd10DaysBefore(Calendar cal) {
  893. Calendar ret = (Calendar) cal.clone();
  894. int day = cal.get(Calendar.DAY_OF_MONTH);
  895. if (day != 10) {
  896. // 20日或月末,直接减10天,则为前一个旬的结束日
  897. ret.add(Calendar.DAY_OF_MONTH, -10);
  898. } else {
  899. // 10日,取前月月末为前一个旬的结束日
  900. ret = getFirstDay(ret);
  901. ret.add(Calendar.DAY_OF_MONTH, -1);
  902. }
  903. return ret;
  904. }
  905. /**
  906. * 取得1旬之前的开始日期
  907. *
  908. * @param cal 旬的结束日 (10日或20日或月末)
  909. * @return
  910. */
  911. private static Calendar getStart10DaysBefore(Calendar cal) {
  912. Calendar ret = getEnd10DaysBefore(cal);
  913. ret.add(Calendar.DAY_OF_MONTH, 1);
  914. return ret;
  915. }
  916. /**
  917. * 将String数组(特定的格式)转换为Date数组
  918. *
  919. * @param input
  920. * @param format
  921. * @return
  922. */
  923. public static Date[] StrArray2DateArray(String[] input, String format) {
  924. int size = input.length;
  925. Date[] output = new Date[size];
  926. if (input != null) {
  927. for (int i = 0; i < size; i++) {
  928. output[i] = Str2Date(input[i], format);
  929. }
  930. return output;
  931. } else {
  932. return null;
  933. }
  934. }
  935. /**
  936. * 将String数组(特定的格式)转换为Calendar数组
  937. *
  938. * @param input
  939. * @param format
  940. * @return
  941. */
  942. public static Calendar[] StrArray2CalArray(String[] input, String format) {
  943. int size = input.length;
  944. Calendar[] output = new Calendar[size];
  945. if (input != null) {
  946. for (int i = 0; i < size; i++) {
  947. output[i] = Str2Calendar(input[i], format);
  948. }
  949. return output;
  950. } else {
  951. return null;
  952. }
  953. }
  954. /**
  955. * 将Calendar数组(特定的格式)转换为String数组
  956. *
  957. * @param input
  958. * @param format
  959. * @return
  960. */
  961. public static String[] CalArray2StrArray(Calendar[] input, String format) {
  962. int size = input.length;
  963. String[] output = new String[size];
  964. if (input != null) {
  965. for (int i = 0; i < size; i++) {
  966. if (input[i] == null) {
  967. output[i] = null;
  968. } else {
  969. output[i] = Calendar2Str(input[i], format);
  970. }
  971. }
  972. return output;
  973. } else {
  974. return null;
  975. }
  976. }
  977. /**
  978. * 将Calendar 的list转换成String类型
  979. *
  980. * @param input
  981. * @param format
  982. * @return
  983. */
  984. public static String calList2Str(List input, String format) {
  985. String result = "";
  986. for (int i = 0; i < input.size(); i++) {
  987. result += i + " : " + DateUtils.Calendar2Str((Calendar) input.get(i), format) + "\n";
  988. }
  989. return result;
  990. }
  991. /**
  992. * 求两个日期相差的年数
  993. *
  994. * @param cal1:结束时间
  995. * @param cal2:开始时间
  996. * @return
  997. */
  998. public static int subCalendar2Year(Calendar cal1, Calendar cal2) {
  999. long sub = cal1.getTimeInMillis() - cal2.getTimeInMillis();
  1000. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24 * 30 * 12)));
  1001. }
  1002. /**
  1003. * 求两个日期相差的月数
  1004. *
  1005. * @param cal1:结束时间
  1006. * @param cal2:开始时间
  1007. * @return
  1008. */
  1009. public static int subCalendar2Month(Calendar cal1, Calendar cal2) {
  1010. long sub = cal1.getTimeInMillis() - cal2.getTimeInMillis();
  1011. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24 * 30)));
  1012. }
  1013. /**
  1014. * 求两个日期相差的日数
  1015. *
  1016. * @param cal1:结束时间
  1017. * @param cal2:开始时间
  1018. * @return
  1019. */
  1020. public static int subCalendar2Day(Calendar cal1, Calendar cal2) {
  1021. long sub = cal1.getTimeInMillis() - cal2.getTimeInMillis();
  1022. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24)));
  1023. }
  1024. /**
  1025. * 求两个日期相差的小时数
  1026. *
  1027. * @param cal1:结束时间
  1028. * @param cal2:开始时间
  1029. * @return
  1030. */
  1031. public static int subCalendar2Hour(Calendar cal1, Calendar cal2) {
  1032. long sub = cal1.getTimeInMillis() - cal2.getTimeInMillis();
  1033. return (int) Math.round((sub / (1000.0 * 60 * 60)));
  1034. }
  1035. public static int subCalendar2Minute(Calendar cal1, Calendar cal2) {
  1036. long sub = cal1.getTimeInMillis() - cal2.getTimeInMillis();
  1037. return (int) Math.round((sub / (1000.0 * 60)));
  1038. }
  1039. public static int subCalendar2Minute(String cal1, String cal2, String pattern) {
  1040. return subCalendar2Minute(Str2Calendar(cal1, pattern), Str2Calendar(cal2, pattern));
  1041. }
  1042. /**
  1043. * 求两个日期相差的时间(cal2 - cal1)
  1044. *
  1045. * @param interval :
  1046. * yyyy 年
  1047. * q 季度
  1048. * m 月
  1049. * y 一年的日数
  1050. * d 日
  1051. * w 一周的日数
  1052. * ww 周
  1053. * h 小时
  1054. * n 分钟
  1055. * s 秒
  1056. * @param cal1
  1057. * @param cal2
  1058. * @return
  1059. */
  1060. public static int DateDiff(String interval, Calendar cal1, Calendar cal2) {
  1061. int ret = 0;
  1062. if ("y".equals(interval) == true) {
  1063. ret = DateDiff2Year(cal1, cal2); // 年
  1064. } else if ("m".equals(interval) == true) {
  1065. ret = DateDiff2Month(cal1, cal2); // 月
  1066. } else if ("d".equals(interval) == true) {
  1067. ret = DateDiff2Day(cal1, cal2); // 日
  1068. } else if ("h".equals(interval) == true) {
  1069. ret = DateDiff2Hour(cal1, cal2); // 小时
  1070. } else if ("n".equals(interval) == true) {
  1071. ret = DateDiff2Minute(cal1, cal2); // 分钟
  1072. }
  1073. return ret;
  1074. }
  1075. /**
  1076. * 求两个日期相加的时间(cal + number)
  1077. *
  1078. * @param interval :
  1079. * yyyy 年
  1080. * q 季度
  1081. * m 月
  1082. * y 一年的日数
  1083. * d 日
  1084. * w 一周的日数
  1085. * ww 周
  1086. * h 小时
  1087. * n 分钟
  1088. * s 秒
  1089. * @param number : 正负整数
  1090. * @param cal : 操作的日期
  1091. * @return
  1092. */
  1093. public static Calendar DateAdd(String interval, int number, Calendar cal) {
  1094. Calendar ret = (Calendar) cal.clone();
  1095. if ("yyyy".equals(interval) == true) {
  1096. ret.add(Calendar.YEAR, number); // 年
  1097. } else if ("y".equals(interval) == true) {
  1098. ret.add(Calendar.DAY_OF_YEAR, number); // 一年的日数
  1099. } else if ("m".equals(interval) == true) {
  1100. ret.add(Calendar.MONTH, number); // 月
  1101. } else if ("d".equals(interval) == true) {
  1102. ret.add(Calendar.DAY_OF_MONTH, number); // 日
  1103. } else if ("w".equals(interval) == true) {
  1104. ret.add(Calendar.DAY_OF_WEEK, number); // 一周的日数
  1105. } else if ("ww".equals(interval) == true) {
  1106. ret.add(Calendar.WEEK_OF_MONTH, number); // 周
  1107. } else if ("h".equals(interval) == true) {
  1108. ret.add(Calendar.HOUR_OF_DAY, number); // 小时
  1109. } else if ("n".equals(interval) == true) {
  1110. ret.add(Calendar.MINUTE, number); // 分钟
  1111. } else if ("s".equals(interval) == true) {
  1112. ret.add(Calendar.SECOND, number); // 秒
  1113. }
  1114. return ret;
  1115. }
  1116. public static Calendar format(Calendar cal, String pattern) {
  1117. Calendar ret = (Calendar) cal.clone();
  1118. String strCal = Calendar2Str(cal, pattern);
  1119. ret = Str2Calendar(strCal, "yyyy-MM-dd HH:mm");
  1120. return ret;
  1121. }
  1122. /**
  1123. * 求两个日期相差的年数(cal2 - cal1)
  1124. *
  1125. * @param cal1:开始时间
  1126. * @param cal2:结束时间
  1127. * @return
  1128. */
  1129. public static int DateDiff2Year(Calendar cal1, Calendar cal2) {
  1130. long sub = cal2.getTimeInMillis() - cal1.getTimeInMillis();
  1131. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24 * 30 * 12)));
  1132. }
  1133. /**
  1134. * 求两个日期相差的月数(cal2 - cal1)
  1135. *
  1136. * @param cal1:开始时间
  1137. * @param cal2:结束时间
  1138. * @return
  1139. */
  1140. public static int DateDiff2Month(Calendar cal1, Calendar cal2) {
  1141. long sub = cal2.getTimeInMillis() - cal1.getTimeInMillis();
  1142. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24 * 30)));
  1143. }
  1144. /**
  1145. * 求两个日期相差的日数(cal2 - cal1)
  1146. *
  1147. * @param cal1:开始时间
  1148. * @param cal2:结束时间
  1149. * @return
  1150. */
  1151. public static int DateDiff2Day(Calendar cal1, Calendar cal2) {
  1152. long sub = cal2.getTimeInMillis() - cal1.getTimeInMillis();
  1153. return (int) Math.round((sub / (1000.0 * 60 * 60 * 24)));
  1154. }
  1155. /**
  1156. * 求两个日期相差的小时数(cal2 - cal1)
  1157. *
  1158. * @param cal1:开始时间
  1159. * @param cal2:结束时间
  1160. * @return
  1161. */
  1162. public static int DateDiff2Hour(Calendar cal1, Calendar cal2) {
  1163. long sub = cal2.getTimeInMillis() - cal1.getTimeInMillis();
  1164. return (int) Math.round((sub / (1000.0 * 60 * 60)));
  1165. }
  1166. /**
  1167. * 求两个日期相差的分钟数(cal2 - cal1)
  1168. *
  1169. * @param cal1:开始时间
  1170. * @param cal2:结束时间
  1171. * @return
  1172. */
  1173. public static int DateDiff2Minute(Calendar cal1, Calendar cal2) {
  1174. if (cal1 == null && cal2 == null) {
  1175. return 0;
  1176. }
  1177. if (cal1 == null) {
  1178. return (int) Math.round((cal2.getTimeInMillis() / (1000.0 * 60)));
  1179. }
  1180. if (cal2 == null) {
  1181. return -(int) Math.round((cal1.getTimeInMillis() / (1000.0 * 60)));
  1182. }
  1183. long sub = cal2.getTimeInMillis() - cal1.getTimeInMillis();
  1184. return (int) Math.round((sub / (1000.0 * 60)));
  1185. }
  1186. /**
  1187. * 将日期的小时设置为8点0分,其他不变
  1188. *
  1189. * @param input
  1190. * @return
  1191. */
  1192. public static Calendar set8Hour(Calendar input) {
  1193. Calendar ret = (Calendar) input.clone();
  1194. ret.set(Calendar.HOUR_OF_DAY, 8);
  1195. ret.set(Calendar.MINUTE, 0);
  1196. ret.set(Calendar.MILLISECOND, 0);
  1197. return ret;
  1198. }
  1199. //1:当日,2:本周,3:本月,4:本年
  1200. public static Date getStartDateForQueryType(int type) {
  1201. Calendar cal = Calendar.getInstance();
  1202. if (type == 2) {
  1203. cal.set(Calendar.DAY_OF_WEEK, 1);
  1204. cal.add(Calendar.DATE, 1);
  1205. } else if (type == 3) {
  1206. cal.set(Calendar.DAY_OF_MONTH, 1);
  1207. } else if (type == 4) {
  1208. cal.set(Calendar.DAY_OF_YEAR, 1);
  1209. }
  1210. return cal.getTime();
  1211. }
  1212. public static Date getTorrow() {
  1213. Calendar cal = Calendar.getInstance();
  1214. cal.add(Calendar.DATE, 1);
  1215. return cal.getTime();
  1216. }
  1217. public static Date getTorrow(Date date, int amount) {
  1218. Calendar cal = Date2Calendar(date);
  1219. cal.add(Calendar.DATE, amount);
  1220. return cal.getTime();
  1221. }
  1222. public static int getYear(Date date) {
  1223. Calendar cal = Date2Calendar(date);
  1224. return cal.get(Calendar.YEAR);
  1225. }
  1226. public static int getMonth(Date date) {
  1227. Calendar cal = Date2Calendar(date);
  1228. return cal.get(Calendar.MONTH) + 1;
  1229. }
  1230. public static List<String> getLast12Month() {
  1231. List<String> list = new ArrayList<>();
  1232. List<String> list2 = new ArrayList<>();
  1233. Calendar cal = Calendar.getInstance();
  1234. list.add(Calendar2Str(cal, "yyyy-MM"));
  1235. for (int i = 0; i < 11; i++) {
  1236. cal.add(Calendar.MONTH, -1);
  1237. list.add(Calendar2Str(cal, "yyyy-MM"));
  1238. }
  1239. for (int i = list.size() - 1; i >= 0; i--) {
  1240. list2.add(list.get(i));
  1241. }
  1242. return list2;
  1243. }
  1244. /**
  1245. * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
  1246. *
  1247. * @param nowTime 当前时间
  1248. * @param startTime 开始时间
  1249. * @param endTime 结束时间
  1250. * @return
  1251. */
  1252. public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
  1253. if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
  1254. return true;
  1255. }
  1256. Calendar date = Calendar.getInstance();
  1257. date.setTime(nowTime);
  1258. Calendar begin = Calendar.getInstance();
  1259. begin.setTime(startTime);
  1260. Calendar end = Calendar.getInstance();
  1261. end.setTime(endTime);
  1262. return date.after(begin) && date.before(end);
  1263. }
  1264. }