d60199eb6117771cefcc161b1e5ad9ef54446121.svn-base 41 KB

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