DateUtils.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. package com.behosoft.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.GregorianCalendar;
  7. public class DateUtils extends org.apache.commons.lang.time.DateUtils {
  8. /** 数据库存储的时间格式串,如yyyymmdd 或yyyymmddHHMiSS */
  9. public static final int DB_STORE_DATE = 1;
  10. /** 用连字符-分隔的时间时间格式串,如yyyy-mm-dd 或yyyy-mm-dd HH:Mi:SS */
  11. public static final int HYPHEN_DISPLAY_DATE = 2;
  12. /** 用连字符.分隔的时间时间格式串,如yyyy.mm.dd 或yyyy.mm.dd HH:Mi:SS */
  13. public static final int DOT_DISPLAY_DATE = 3;
  14. /** 用中文字符分隔的时间格式串,如yyyy年mm月dd 或yyyy年mm月dd HH:Mi:SS */
  15. public static final int CN_DISPLAY_DATE = 4;
  16. /***** 数据库存储时间格式串,如yyyymmddHHMissSS **/
  17. public static final int DB_LONG_DATE = 5;
  18. public static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  19. public static Date setDateTime(Date date, int hour, int minute, int second) {
  20. Calendar c = Calendar.getInstance();
  21. c.setTime(date);
  22. c.set(Calendar.HOUR_OF_DAY, hour);
  23. c.set(Calendar.MINUTE, minute);
  24. c.set(Calendar.SECOND, second);
  25. return c.getTime();
  26. }
  27. public static Date getNextDay() {
  28. Calendar calendar = Calendar.getInstance();
  29. calendar.add(Calendar.DAY_OF_MONTH, 1);
  30. return calendar.getTime();
  31. }
  32. public static String getDateTimeStr(Date date) {
  33. return DATETIME_FORMAT.format(date);
  34. }
  35. /**
  36. * 返回当前时间
  37. *
  38. * @return
  39. */
  40. public static Date getCurrentDate() {
  41. return new Date(System.currentTimeMillis());
  42. }
  43. /**
  44. * 得到精确到秒的格式化当前时间串
  45. *
  46. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  47. * @return 当前时间格式化时间串
  48. */
  49. public static String getCurrTimeStr(int formatType) {
  50. return getTimeStr(new Date(), formatType);
  51. }
  52. public static String getCurrTimeStr() {
  53. return getCurrTimeStr(HYPHEN_DISPLAY_DATE);
  54. }
  55. public static String getTimeStr(Date date) {
  56. return getTimeStr(date, HYPHEN_DISPLAY_DATE);
  57. }
  58. /**
  59. * 得到精确到秒的格式化时间串
  60. *
  61. * @param date 指定时间
  62. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  63. * @return 指定时间的格式化时间串
  64. */
  65. public static String getTimeStr(Date date, int formatType) {
  66. if (formatType < DB_STORE_DATE || formatType > DB_LONG_DATE) {
  67. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  68. }
  69. else {
  70. String formatStr = null;
  71. switch (formatType) {
  72. case DB_STORE_DATE:
  73. formatStr = "yyyyMMddHHmmss";
  74. break;
  75. case HYPHEN_DISPLAY_DATE:
  76. formatStr = "yyyy'-'MM'-'dd HH:mm:ss";
  77. break;
  78. case DOT_DISPLAY_DATE:
  79. formatStr = "yyyy.MM.dd HH:mm:ss";
  80. break;
  81. case CN_DISPLAY_DATE:
  82. formatStr = "yyyy'年'MM'月'dd'日' HH:mm:ss";
  83. break;
  84. case DB_LONG_DATE:
  85. formatStr = "yyyyMMddHHmmssSS";
  86. }
  87. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  88. return sdf.format(date);
  89. }
  90. }
  91. /**
  92. * 得到精确到天的当前格式化日期串
  93. *
  94. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  95. * @return
  96. */
  97. public static String getCurrDateStr(int formatType) {
  98. return getDateStr(new Date(), formatType);
  99. }
  100. public static String getCurrDateStr() {
  101. return getCurrDateStr(HYPHEN_DISPLAY_DATE);
  102. }
  103. /**
  104. * 得到精确到天的指定时间格式化日期串
  105. *
  106. * @param date 指定时间
  107. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  108. * @return 指定时间格式化日期串
  109. */
  110. public static String getDateStr(Date date, int formatType) {
  111. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  112. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  113. }
  114. else {
  115. String formatStr = null;
  116. switch (formatType) {
  117. case DB_STORE_DATE:
  118. formatStr = "yyyyMMdd";
  119. break;
  120. case HYPHEN_DISPLAY_DATE:
  121. formatStr = "yyyy-MM-dd";
  122. break;
  123. case DOT_DISPLAY_DATE:
  124. formatStr = "yyyy.MM.dd";
  125. break;
  126. case CN_DISPLAY_DATE:
  127. formatStr = "yyyy'年'MM'月'dd'日'";
  128. break;
  129. }
  130. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  131. return sdf.format(date);
  132. }
  133. }
  134. /**
  135. * 得到精确到月的当前时间格式化年月串
  136. *
  137. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  138. * @return 精确到月当前时间格式化年月串
  139. */
  140. public static String getYearMonthStr(int formatType) {
  141. return getYearMonthStr(new Date(), formatType);
  142. }
  143. /**
  144. * 得到精确到月的指定时间格式化年月串
  145. *
  146. * @param date 指定的时间
  147. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  148. * @return 精确到月当前时间格式化年月串
  149. */
  150. public static String getYearMonthStr(Date date, int formatType) {
  151. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  152. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  153. }
  154. else {
  155. String formatStr = null;
  156. switch (formatType) {
  157. case DB_STORE_DATE:
  158. formatStr = "yyyyMM";
  159. break;
  160. case HYPHEN_DISPLAY_DATE:
  161. formatStr = "yyyy-MM";
  162. break;
  163. case DOT_DISPLAY_DATE:
  164. formatStr = "yyyy.MM";
  165. break;
  166. case CN_DISPLAY_DATE:
  167. formatStr = "yyyy'年'MM'月'";
  168. break;
  169. }
  170. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  171. return sdf.format(date);
  172. }
  173. }
  174. /**
  175. * 将数据库存储的日期格式串转换为各种显示的格式
  176. *
  177. * @param dateStr 最小6位,最大14位的数据库存储格式时间串如:20041212
  178. * @param formatType 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  179. * @return 格式化的时间串
  180. */
  181. public static String toDisplayStr(String dateStr, int formatType) {
  182. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  183. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  184. }
  185. if (dateStr == null || dateStr.length() < 6 || dateStr.length() > 14 || formatType == DB_STORE_DATE) {
  186. return StringUtils.toVisualString(dateStr);
  187. }
  188. else {
  189. char[] charArr = null;
  190. switch (formatType) {
  191. case HYPHEN_DISPLAY_DATE:
  192. charArr = new char[] { '-', '-', ' ', ':', ':' };
  193. break;
  194. case DOT_DISPLAY_DATE:
  195. charArr = new char[] { '.', '.', ' ', ':', ':' };
  196. break;
  197. case CN_DISPLAY_DATE:
  198. charArr = new char[] { '年', '月', ' ', ':', ':' };
  199. break;
  200. default:
  201. charArr = new char[] { '-', '-', ' ', ':', ':' };
  202. }
  203. try {
  204. SimpleDateFormat sdf_1 = null;
  205. SimpleDateFormat sdf_2 = null;
  206. switch (dateStr.length()) {
  207. case 6:
  208. sdf_1 = new SimpleDateFormat("yyyyMM");
  209. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM");
  210. break;
  211. case 8:
  212. sdf_1 = new SimpleDateFormat("yyyyMMdd");
  213. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd");
  214. break;
  215. case 10:
  216. sdf_1 = new SimpleDateFormat("yyyyMMddHH");
  217. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + "+charArr[2]" + "'HH");
  218. break;
  219. case 12:
  220. sdf_1 = new SimpleDateFormat("yyyyMMddHHmm");
  221. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm");
  222. break;
  223. case 14:
  224. sdf_1 = new SimpleDateFormat("yyyyMMddHHmmss");
  225. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm'" + charArr[4] + "'ss");
  226. break;
  227. default:
  228. return dateStr;
  229. }
  230. return sdf_2.format(sdf_1.parse(dateStr));
  231. }
  232. catch (ParseException ex) {
  233. return dateStr;
  234. }
  235. }
  236. }
  237. /**
  238. * 将显示格式的时间字符串转换为数据库存储的类型
  239. *
  240. * @param dateStr 最小4位,最大19位。显示的时间格式时间串如:2004-12-12
  241. * @return 数据库存储的时间字符串
  242. */
  243. public static String toStoreStr(String dateStr) {
  244. if (dateStr == null || dateStr.trim().equals("")) {
  245. return "";
  246. }
  247. StringBuilder strBuf = new StringBuilder();
  248. for (int i = 0; i < dateStr.length(); i++) {
  249. if (dateStr.charAt(i) >= '0' && dateStr.charAt(i) <= '9') {
  250. strBuf.append(dateStr.charAt(i));
  251. }
  252. }
  253. return strBuf.toString();
  254. }
  255. /**
  256. * <b>功能描述:</b> 把时间转换成14位的字符串,不足位数补充 0 或者 9
  257. *
  258. * @param dateStr String
  259. * @param key int
  260. * @return String
  261. */
  262. public static String toStoreStr14(String dateStr, boolean bMax) {
  263. if (dateStr == null || dateStr.trim().equals("")) {
  264. return "";
  265. }
  266. StringBuilder strBuf = new StringBuilder(toStoreStr(dateStr));
  267. String retStr = strBuf.toString();
  268. if (bMax) {
  269. retStr = StringUtils.pad(retStr, 14, '9', true);
  270. }
  271. else {
  272. retStr = StringUtils.pad(retStr, 14, '0', true);
  273. }
  274. return retStr;
  275. }
  276. /**
  277. * 将生日存储的时间格式转化为年龄(周岁,小数点后不计)
  278. *
  279. * @param birthdayStr 生日字段 "yyyymmdd"
  280. * @return 年龄
  281. */
  282. public static String birthdayToAge(String birthdayStr) {
  283. if (birthdayStr == null || birthdayStr.length() < 6) {
  284. return "";
  285. }
  286. else {
  287. int birthYear = Integer.parseInt(birthdayStr.substring(0, 4));
  288. int birthMonth = Integer.parseInt(birthdayStr.substring(4, 6));
  289. Calendar cal = new GregorianCalendar();
  290. int currYear = cal.get(Calendar.YEAR);
  291. int currMonth = cal.get(Calendar.MONTH);
  292. int age = currYear - birthYear;
  293. age -= (currMonth < birthMonth) ? 1 : 0;
  294. return "" + age;
  295. }
  296. }
  297. /**
  298. * 功能描述: 将年龄转换为生日
  299. *
  300. * @param age int
  301. * @return String
  302. */
  303. public static String ageToBirthday(int age) {
  304. String currDateStr = DateUtils.getYearMonthStr(DB_STORE_DATE);
  305. int currDateInt = Integer.parseInt(currDateStr);
  306. int ageDateInt = currDateInt - age * 100;
  307. return String.valueOf(ageDateInt);
  308. }
  309. /**
  310. * 功能描述: 增加一年
  311. *
  312. * @param age int
  313. * @return String
  314. */
  315. public static String nextAgeDate(String birDate) {
  316. int currDateInt = Integer.parseInt(birDate);
  317. int ageDateInt = currDateInt + 100;
  318. return String.valueOf(ageDateInt);
  319. }
  320. /**
  321. * @param dateTimeStr String 格式化的时间串
  322. * @param formatType 数据格式类型 {@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE}, {@link #CN_HTML_DISPLAY_DATE}
  323. * @param detal int 增加或减少的时间
  324. * @param field int 参见Calendar中关于时间字段属性的定义
  325. * @return String 返回的
  326. */
  327. public static String add(String dateTimeStr, int formatType, int detal, int field) {
  328. if (dateTimeStr == null || dateTimeStr.length() < 6) {
  329. return dateTimeStr;
  330. }
  331. else {
  332. try {
  333. String formatStr = null;
  334. switch (formatType) {
  335. case DB_STORE_DATE:
  336. formatStr = "yyyyMMddHHmmss";
  337. break;
  338. case HYPHEN_DISPLAY_DATE:
  339. formatStr = "yyyy-MM-dd HH:mm:ss";
  340. break;
  341. case DOT_DISPLAY_DATE:
  342. formatStr = "yyyy.MM.dd HH:mm:ss";
  343. break;
  344. case CN_DISPLAY_DATE:
  345. formatStr = "yyyy'年'MM'月' HH:mm:ss";
  346. break;
  347. default:
  348. formatStr = "yyyyMMddHHmmss";
  349. break;
  350. }
  351. formatStr = formatStr.substring(0, dateTimeStr.length());
  352. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  353. Date d = sdf.parse(dateTimeStr);
  354. GregorianCalendar g = new GregorianCalendar();
  355. g.setTime(d);
  356. g.add(field, detal);
  357. d = g.getTime();
  358. return sdf.format(d);
  359. }
  360. catch (ParseException ex) {
  361. ex.printStackTrace();
  362. return dateTimeStr;
  363. }
  364. }
  365. }
  366. // /**
  367. // * @param date Date 时间
  368. // * @param detal int 增加的时间
  369. // * @param field int 参见Calendar中关于时间字段属性的定义
  370. // * @return Date
  371. // */
  372. // public static Date add(Date date, int detal, int field)
  373. // {
  374. // Calendar g = new GregorianCalendar();
  375. // g.setTime(date);
  376. // g.add(field, detal);
  377. // return g.getTime();
  378. // }
  379. /**
  380. * 日期、时间格式化
  381. *
  382. * @param date Date 将要被格式化的日期对象
  383. * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
  384. * @return String 格式化后的日期、时间字符串,data为null时返回null,outFmt非法时返回yyyyMMdd格式
  385. */
  386. public static String getDateFormat(Date date, String outFmt) {
  387. if (null == date) {
  388. return null;
  389. }
  390. if (null == outFmt || "".equals(outFmt.trim())) { // outFmt非法
  391. outFmt = "yyyyMMdd";
  392. }
  393. String retu = null;
  394. SimpleDateFormat dateFormat = null;
  395. try {
  396. dateFormat = new SimpleDateFormat(outFmt);
  397. }
  398. catch (IllegalArgumentException iaex) { // outFmt非法
  399. dateFormat = new SimpleDateFormat("yyyyMMdd");
  400. }
  401. retu = dateFormat.format(date);
  402. dateFormat = null;
  403. return retu;
  404. }
  405. /**
  406. * 把日期时间对象格式化为yyyyMMdd样式
  407. *
  408. * @param date Date 将要被格式化的日期对象
  409. * @return String 格式化后的日期、时间字符串,如:20041001
  410. */
  411. public static String getDateFormat(Date date) {
  412. return getDateFormat(date, "yyyyMMdd");
  413. }
  414. /**
  415. * 把系统当前日期时间格式化为指定的样式
  416. *
  417. * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
  418. * @return String 格式化后的日期、时间字符串,如:2004年10月01日
  419. */
  420. public static String getDateFormat(String outFmt) {
  421. return getDateFormat(new Date(), outFmt);
  422. }
  423. /**
  424. * 把系统当前日期时间格式化为默认样式yyyyMMdd
  425. *
  426. * @return String 格式化后的日期、时间字符串,如:20041001
  427. */
  428. public static String getDateFormat() {
  429. return getDateFormat(new Date(), "yyyyMMdd");
  430. }
  431. /**
  432. * 日期、时间格式化
  433. *
  434. * @param millis long the number of milliseconds(毫秒) since January 1, 1970, 00:00:00 GMT.
  435. * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
  436. * @return String 格式化后的日期、时间字符串
  437. */
  438. public static String getDateFormat(long millis, String outFmt) {
  439. Date d = new Date(millis);
  440. Calendar calendar = Calendar.getInstance();
  441. calendar.setTime(d);
  442. String retu = getDateFormat(calendar.getTime(), outFmt);
  443. calendar = null;
  444. return retu;
  445. }
  446. /**
  447. * 日期、时间格式化
  448. *
  449. * @param datestr String 存在一定格式的日期、时间字符串,如:20041001、200410011503
  450. * @param inFmt String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
  451. * @param outFmt String 返回样式,参照类说明,如:yyyy年MM月dd日
  452. * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
  453. * @return String 格式化后的日期、时间字符串,如:2004年10月01日、2004年10月01日 <BR>
  454. * 输出样式outFmt非法时,使用yyyyMMdd格式输出
  455. */
  456. public static String getDateFormat(String datestr, String inFmt, String outFmt) throws ParseException {
  457. if (null == datestr || "".equals(datestr.trim())) {
  458. return datestr;
  459. }
  460. if (null == inFmt || "".equals(inFmt.trim())) {
  461. return datestr;
  462. }
  463. if (null == outFmt || "".equals(outFmt.trim())) { // 输出样式非法
  464. outFmt = "yyyyMMdd";
  465. }
  466. java.util.Date inDate = getDate(datestr, inFmt);
  467. if (null == inDate) { // 根据inFmt分析datestr时抛出异常
  468. return datestr;
  469. }
  470. String retu = getDateFormat(inDate, outFmt);
  471. inDate = null;
  472. return retu;
  473. }
  474. /**
  475. * 把日期时间字符串,按inFmt样式转化为日期对象,然后格式化为默认样式yyyyMMdd
  476. *
  477. * @param datestr String 存在一定格式的日期、时间字符串,如:20041001、200410011503
  478. * @param inFmt String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
  479. * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
  480. * @return String 格式化后的日期、时间字符串,如:20041001、20041001
  481. */
  482. public static String getDateFormat(String datestr, String inFmt) throws ParseException {
  483. return getDateFormat(datestr, inFmt, "yyyyMMdd");
  484. }
  485. /**
  486. * 根据inFmt的样式,日期时间字符串转化为日期时间对象
  487. *
  488. * @param datestr String 日期时间字符串,如:20041001、2004年10月01日 15:03
  489. * @param inFmt String 对datestr参数格式说明,参照类说明,如yyyyMMdd、yyyy年MM月dd日 HH:mm
  490. * @throws ParseException 当datestr不能格式化为inFmt格式时抛出此异常
  491. * @return Date 日期时间对象,格式inFmt非法时,使用yyyyMMdd格式
  492. */
  493. public static Date getDate(String datestr, String inFmt) throws ParseException {
  494. if (null == datestr || "".equals(datestr.trim())) {
  495. return null;
  496. }
  497. if (null == inFmt || "".equals(inFmt.trim())) { // inFmt非法
  498. inFmt = "yyyyMMdd";
  499. }
  500. java.util.Date inDate = null;
  501. // 依据inFmt格式把日期字符串转化为日期对象
  502. SimpleDateFormat inDateFormat = new SimpleDateFormat(inFmt);
  503. inDateFormat.setLenient(true);
  504. inDate = inDateFormat.parse(datestr);
  505. inDateFormat = null;
  506. return inDate;
  507. }
  508. /**
  509. * 对日期时间对象进行调整,实现如昨天是几号,去年的今天星期几等. <BR>
  510. * 例子:
  511. *
  512. * <pre>
  513. * <blockquote>
  514. * 计算去年今天星期几
  515. * Date date = DateUtils.addDate(new Date(),Calendar.YEAR,-1);
  516. * System.out.println(DateUtils.getDateFormat(date,"E"));
  517. * 打印60天后是什么日期,并显示为 yyyy-MM-dd 星期
  518. * Date date = DateUtils.addDate(new Date(),Calendar.DATE,60);
  519. * System.out.println(DateUtils.getDateFormat(date,"yyyy-MM-dd E"));
  520. * </blockquote>
  521. * </pre>
  522. *
  523. * @param date Date 需要调整的日期时间对象
  524. * @param CALENDARFIELD int 对日期时间对象以什么单位进行调整:
  525. *
  526. * <pre>
  527. * <blockquote>
  528. * 年 Calendar.YEAR
  529. * 月 Calendar.MONTH
  530. * 日 Calendar.DATE
  531. * 时 Calendar.HOUR
  532. * 分 Calendar.MINUTE
  533. * 秒 Calendar.SECOND
  534. * </blockquote>
  535. * </pre>
  536. * @param amount int 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
  537. * @return Date 调整后的日期时间对象
  538. */
  539. public static Date addDate(Date date, int CALENDARFIELD, int amount) {
  540. if (null == date) {
  541. return date;
  542. }
  543. Calendar calendar = Calendar.getInstance();
  544. calendar.setTime(date);
  545. calendar.add(CALENDARFIELD, amount);
  546. return calendar.getTime();
  547. }
  548. /**
  549. * 对日期时间对象进行调整.
  550. *
  551. * @param datestr String 需要调整的日期时间字符串,它的格式为yyyyMMdd
  552. * @param CALENDARFIELD int 对日期时间对象以什么单位进行调整
  553. * @param amount int 调整数量
  554. * @throws ParseException 当datestr不能格式化为yyyyMMdd格式时抛出此异常
  555. * @return Date 调整后的日期时间对象
  556. * @see #addDate(java.util.Date, int, int)
  557. */
  558. public static Date addDate(String datestr, int CALENDARFIELD, int amount) throws ParseException {
  559. return addDate(getDate(datestr, "yyyyMMdd"), CALENDARFIELD, amount);
  560. }
  561. /**
  562. * 根据出生日期,计算出在某一个日期的年龄
  563. *
  564. * @param birthday Date 出生日期时间对象
  565. * @param date2 Date 计算日期对象
  566. * @return int 返回date2那一天出生日期为birthday的年龄,如果birthday大于date2则返回-1
  567. */
  568. public static int getAge(Date birthday, Date date2) {
  569. if (null == birthday || null == date2) {
  570. return -1;
  571. }
  572. if (birthday.after(date2)) { // birthday大于date2
  573. return -1;
  574. }
  575. int ibdYear = StringUtils.getInt(getDateFormat(birthday, "yyyy"), -1);
  576. int idate2Year = StringUtils.getInt(getDateFormat(date2, "yyyy"), -1);
  577. if (ibdYear < 0 || idate2Year < 0) {
  578. return -1;
  579. }
  580. if (ibdYear > idate2Year) {
  581. return -1;
  582. }
  583. return idate2Year - ibdYear + 1;
  584. }
  585. /**
  586. * 根据出生日期,计算出当前的年龄
  587. *
  588. * @param birthday Date 出生日期时间对象
  589. * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
  590. */
  591. public static int getAge(Date birthday) {
  592. return getAge(birthday, new Date());
  593. }
  594. /**
  595. * 根据出生日期,计算出当前的年龄
  596. *
  597. * @param birthdaystr String 出生日期时间字符串,其格式一定为yyyyMMdd
  598. * @throws ParseException 当datestr不能格式化为yyyyMMdd格式时抛出此异常
  599. * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
  600. */
  601. public static int getAge(String birthdaystr) throws ParseException {
  602. return getAge(getDate(birthdaystr, "yyyyMMdd"));
  603. }
  604. public static Date getCurDate() {
  605. return Calendar.getInstance().getTime();
  606. }
  607. public static String getCurDay(Date date) {
  608. String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  609. Calendar cal = Calendar.getInstance();
  610. cal.setTime(date);
  611. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  612. if (w < 0)
  613. w = 0;
  614. return weekDays[w];
  615. }
  616. /**
  617. * 格式化
  618. *
  619. * @param str 字符
  620. * @param format 格式化
  621. *
  622. * @return Date
  623. * @throws ParseException 异常
  624. */
  625. public static Date parse(final String str) throws ParseException
  626. {
  627. final Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
  628. return date;
  629. }
  630. /**
  631. *
  632. * <p>
  633. * 计算指定日期到今天的天数
  634. * </p>
  635. * @param sql 参数说明
  636. * @param args 参数说明
  637. * @return 返回值说明
  638. */
  639. public static Long betweenDay(String beginDate) throws ParseException{
  640. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  641. Date cDate = df.parse(beginDate);
  642. Date dDate = new Date();
  643. return (dDate.getTime()-cDate.getTime())/(24*60*60*1000);
  644. }
  645. public static String dateToStr(String dateFormat, Date date){
  646. //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  647. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
  648. return sdf.format(date);
  649. }
  650. //获取几天前日期
  651. public static String getDateBefore(Date d,int day){
  652. Calendar now =Calendar.getInstance();
  653. now.setTime(d);
  654. now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
  655. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  656. return sdf.format(now.getTime());
  657. }
  658. public static Long subDay(Date day1,Date day2){
  659. return (day1.getTime()-day2.getTime())/(24*60*60*1000);
  660. }
  661. public static double subDouble(Object a,Object b)
  662. {
  663. return asDouble(a, 0d) - asDouble(b, 0d);
  664. }
  665. public static double asDouble(Object obj, double defaultValue) {
  666. double value = defaultValue;
  667. if (obj instanceof String) {
  668. String str = (String) obj;
  669. if (StringUtils.isNotBlank(str)) {
  670. try {
  671. value = Double.parseDouble(str);
  672. } catch (NumberFormatException e) {
  673. }
  674. }
  675. } else if (obj instanceof Number) {
  676. value = ((Number) obj).doubleValue();
  677. }
  678. return value;
  679. }
  680. }