java date 日期操作

  
  
  1. * 类描述:日期操作工具类
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.GregorianCalendar;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.TimeZone;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. /**
  15. * <pre>
  16. * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;DateUtils&lt;/font&gt;&lt;/b&gt;
  17. * </pre>
  18. *
  19. * <pre>
  20. * &lt;b&gt; --描述说明--&lt;/b&gt;
  21. * </pre>
  22. *
  23. * <pre>
  24. * 日期操作工具类
  25. * </pre>
  26. *
  27. * <pre>
  28. * &lt;b&gt;--样例--&lt;/b&gt;
  29. * DateUtils.method();
  30. * </pre>
  31. *
  32. * JDK版本:JDK1.5
  33. *
  34. */
  35. public class DateUtils extends org.apache.commons.lang.time.DateUtils {
  36. private DateUtils() {
  37. }
  38. /** 一分钟的秒数 */
  39. public static final int SECONDS_ONE_MINUTE = 60;
  40. /** 一秒的毫秒数 */
  41. public static final int MILLISECONDS_ONE_SECOND = 1000;
  42. /** 一小时的秒数 */
  43. public static final int SECONDS_ONE_HOUR = 60 * SECONDS_ONE_MINUTE;
  44. /** 一天的秒数 */
  45. public static final int SECONDS_ONE_DAY = 24 * SECONDS_ONE_HOUR;
  46. /** 数据库存储的时间格式串,如yyyymmdd 或yyyymmddHHMiSS */
  47. public static final int DB_STORE_DATE = 1;
  48. /** 用连字符-分隔的时间时间格式串,如yyyy-mm-dd 或yyyy-mm-dd HH:Mi:SS */
  49. public static final int HYPHEN_DISPLAY_DATE = 2;
  50. /** 用连字符.分隔的时间时间格式串,如yyyy.mm.dd 或yyyy.mm.dd HH:Mi:SS */
  51. public static final int DOT_DISPLAY_DATE = 3;
  52. /** 用中文字符分隔的时间格式串,如yyyy年mm月dd 或yyyy年mm月dd HH:Mi:SS */
  53. public static final int CN_DISPLAY_DATE = 4;
  54. public static final int DB_STORE_MONTH = 5;
  55. /** 数据库存储的时间格式串,时间数字串 */
  56. public static final int DB_STORE_DATE_SPLIT = 1;
  57. /** 数据库存储的时间格式串,如yyyyMMdd */
  58. public static final String DB_STORE_DATE_FMT = "yyyyMMdd";
  59. /** 数据库存储的时间格式串,如yyyyMMddHHMiSS */
  60. public static final String DB_STORE_DATE_FULL = "yyyyMMddHHmmss";
  61. /** 数据库存储的时间格式串,如yyyyMM */
  62. public static final String DB_STORE_DATE_MONTH = "yyyyMM";
  63. /** 用连字符-分隔的时间格式串 */
  64. public static final int LINK_DISPLAY_DATE_SPLIT = 2;
  65. /** 用连字符-分隔的时间格式串,如yyyy-MM-dd */
  66. public static final String LINK_DISPLAY_DATE = "yyyy-MM-dd";
  67. /** 用连字符-分隔的时间格式串,如yyyy-MM-dd HH:Mi:SS */
  68. public static final String LINK_DISPLAY_DATE_FULL = "yyyy-MM-dd HH:mm:ss";
  69. /** 用连字符-分隔的时间格式串,如yyyy-MM */
  70. public static final String LINK_DISPLAY_DATE_MONTH = "yyyy-MM";
  71. /** 用连字符.分隔的时间格式串 */
  72. public static final int DOT_DISPLAY_DATE_SPLIT = 3;
  73. /** 用连字符.分隔的时间格式串,如yyyy.MM.dd */
  74. public static final String DOT_DISPLAY_DATE_FMT = "yyyy.MM.dd";
  75. /** 用连字符.分隔的时间格式串,如yyyy.MM.dd HH:Mi:SS */
  76. public static final String DOT_DISPLAY_DATE_FULL = "yyyy.MM.dd HH:mm:ss";
  77. /** 用连字符.分隔的时间格式串,如yyyy.MM */
  78. public static final String DOT_DISPLAY_DATE_MONTH = "yyyy.MM";
  79. /** 用中文字符分隔的时间格式串 */
  80. public static final int CN_DISPLAY_DATE_SPLIT = 4;
  81. /** 用中文字符分隔的时间格式串 */
  82. public static final int CN_DISPLAY_TIME_SPLIT = 5;
  83. /** 用中文字符分隔的时间格式串,如yyyy年MM月dd日 */
  84. public static final String CN_DISPLAY_DATE_FMT = "yyyy年MM月dd日";
  85. /** 用中文字符分隔的时间格式串,如yyyy年MM月dd日 HH时Mi分SS秒 */
  86. public static final String CN_DISPLAY_TIME_FULL = "yyyy年MM月dd日 HH时mm分ss秒";
  87. /** 用中文字符分隔的时间格式串,如yyyy年MM月dd日 HH:Mi:SS */
  88. public static final String CN_DISPLAY_DATE_FULL = "yyyy年MM月dd日 HH:mm:ss";
  89. /** 用中文字符分隔的时间格式串,如yyyy年MM月 */
  90. public static final String CN_DISPLAY_DATE_MONTH = "yyyy年MM月";
  91. private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
  92. /**
  93. * 根据相应的长度,及格式化分隔符返回日期类型
  94. *
  95. * @param formatType
  96. * @param length
  97. * @return
  98. */
  99. private static String getFormatType(int formatSplitType, int length) {
  100. String formatStr = null;
  101. switch (formatSplitType) {
  102. case LINK_DISPLAY_DATE_SPLIT:
  103. switch (length) {
  104. case 14:
  105. formatStr = LINK_DISPLAY_DATE_FULL;
  106. break;
  107. default:
  108. formatStr = LINK_DISPLAY_DATE;
  109. }
  110. break;
  111. case DOT_DISPLAY_DATE_SPLIT:
  112. switch (length) {
  113. case 14:
  114. formatStr = DOT_DISPLAY_DATE_FULL;
  115. break;
  116. default:
  117. formatStr = DOT_DISPLAY_DATE_FMT;
  118. }
  119. break;
  120. case CN_DISPLAY_DATE_SPLIT:
  121. switch (length) {
  122. case 14:
  123. formatStr = CN_DISPLAY_DATE_FULL;
  124. break;
  125. default:
  126. formatStr = CN_DISPLAY_DATE_FMT;
  127. }
  128. break;
  129. case CN_DISPLAY_TIME_SPLIT:
  130. switch (length) {
  131. case 14:
  132. formatStr = CN_DISPLAY_TIME_FULL;
  133. break;
  134. default:
  135. formatStr = CN_DISPLAY_DATE_FMT;
  136. }
  137. break;
  138. default:
  139. switch (length) {
  140. case 14:
  141. formatStr = DB_STORE_DATE_FULL;
  142. break;
  143. default:
  144. formatStr = DB_STORE_DATE_FMT;
  145. }
  146. break;
  147. }
  148. return formatStr;
  149. }
  150. /**
  151. * 得到格式化时间串
  152. *
  153. * @param date
  154. * 指定时间
  155. * @param formatType
  156. * 时间格式的类型
  157. * @return 指定时间的格式化时间串
  158. */
  159. private static String getDateStr(Date date, String formatStr) {
  160. SimpleDateFormat fomate = new SimpleDateFormat(formatStr);
  161. return fomate.format(date);
  162. }
  163. /**
  164. * 将日期格式串(数字字符串)转换为各种显示的格式
  165. *
  166. * @param dateStr
  167. * 最小6位,最大14位的数据库存储格式时间串如:20041212
  168. * @param formatType
  169. * 时间格式的类型
  170. * @return 格式化的时间串
  171. */
  172. private static String toDisplayStr(String dateStr, String formatStr) {
  173. dateStr = dateStr.replaceAll("[^0-9]", "");
  174. SimpleDateFormat fomateDate = null;
  175. switch (dateStr.length()) {
  176. case 4:
  177. fomateDate = new SimpleDateFormat("yyyy");
  178. break;
  179. case 6:
  180. fomateDate = new SimpleDateFormat("yyyyMM");
  181. break;
  182. case 8:
  183. fomateDate = new SimpleDateFormat("yyyyMMdd");
  184. break;
  185. case 10:
  186. fomateDate = new SimpleDateFormat("yyyyMMddHH");
  187. break;
  188. case 12:
  189. fomateDate = new SimpleDateFormat("yyyyMMddHHmm");
  190. break;
  191. case 14:
  192. fomateDate = new SimpleDateFormat("yyyyMMddHHmmss");
  193. break;
  194. }
  195. try {
  196. return getDateStr(fomateDate.parse(dateStr), formatStr);
  197. } catch (ParseException e) {
  198. return StringUtils.toVisualString(dateStr);
  199. }
  200. }
  201. /**
  202. * 时间字符格式化检验
  203. *
  204. * @param dateStr
  205. * @return
  206. */
  207. private static boolean checkDateStr(String dateStr) {
  208. if (dateStr != null) {
  209. String dateStrCheck = dateStr.replaceAll("[^0-9]", "");
  210. if (dateStrCheck.length() >= 4 && dateStrCheck.length() <= 14 && dateStrCheck.length() % 2 == 0) {
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. /** 获取当前日期字符串 yyyyMMdd */
  217. public static String getCurrentDate() {
  218. Date date = new Date();
  219. return getDateStr(date, DB_STORE_DATE_FMT);
  220. }
  221. /** 获取当前日期+时间字符串 yyyyMMddHHmmss */
  222. public static String getCurrentTime() {
  223. Date date = new Date();
  224. return getDateStr(date, DB_STORE_DATE_FULL);
  225. }
  226. /**
  227. * 根据格式化类型将时间类型的数据格式化,并以字符串形式返回。
  228. *
  229. * @param object
  230. * @param formatType
  231. * @return
  232. */
  233. public static String getDateStr(Object object, String formatStr) {
  234. if (object == null) {
  235. return null;
  236. }
  237. String resultDateStr = null;
  238. if (object instanceof java.util.Date) {
  239. resultDateStr = getDateStr((Date) object, formatStr);
  240. } else if (object instanceof String) {
  241. String dateStr = (String) object;
  242. if (!checkDateStr(dateStr)) {
  243. return null;
  244. }
  245. resultDateStr = toDisplayStr(dateStr, formatStr);
  246. } else if (object instanceof Integer) {
  247. String dateStr = ((Integer) object).toString();
  248. if (!checkDateStr(dateStr)) {
  249. return null;
  250. }
  251. resultDateStr = toDisplayStr(dateStr, formatStr);
  252. }
  253. return resultDateStr;
  254. }
  255. /**
  256. * 根据格式化类型将时间类型的数据格式化,并以字符串形式返回。
  257. *
  258. * @param object
  259. * @param formatType
  260. * @return
  261. */
  262. public static String getDateStrByStr(String dateStr, int formatSliptType) {
  263. if (!checkDateStr(dateStr)) {
  264. return dateStr;
  265. }
  266. dateStr = dateStr.replaceAll("[^0-9]", "");
  267. String formateType = getFormatType(formatSliptType, dateStr.length());
  268. return toDisplayStr(dateStr, formateType);
  269. }
  270. /**
  271. * 转化Date类型
  272. *
  273. * @param dateStr
  274. * @return
  275. */
  276. public static Date changeStrToDate(String dateStr) {
  277. if (!checkDateStr(dateStr)) {
  278. return null;
  279. }
  280. dateStr = dateStr.replaceAll("[^0-9]", "");
  281. String formatStr = getFormatType(DB_STORE_DATE_SPLIT, dateStr.length());
  282. SimpleDateFormat fomateDate = new SimpleDateFormat(formatStr);
  283. try {
  284. return fomateDate.parse(dateStr);
  285. } catch (ParseException e) {
  286. e.printStackTrace();
  287. }
  288. return null;
  289. }
  290. /**
  291. * 获取某个分钟之后的时间
  292. *
  293. * @param dateStr
  294. * String 时间字符串
  295. * @param later
  296. * long 分钟
  297. * @return
  298. */
  299. public static Date getLaterDate(String dateStr, long later) {
  300. Date date = changeStrToDate(dateStr);
  301. date.setTime(date.getTime() + later * 60 * 1000);
  302. return date;
  303. }
  304. public static void convertDate(String datestr) {
  305. try {
  306. SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd HH时mm分ss秒");
  307. Date date = format.parse(datestr);
  308. System.out.println(date.toString());
  309. } catch (Exception ex) {
  310. ex.printStackTrace();
  311. }
  312. }
  313. /**
  314. * 获取某个分钟之后的时间
  315. *
  316. * @param date
  317. * Date 时间
  318. * @param later
  319. * long 分钟
  320. * @return
  321. */
  322. public static Date getLaterDate(Date date, long later) {
  323. date.setTime(date.getTime() + later * 60 * 1000);
  324. return date;
  325. }
  326. /**
  327. * 获取某个分钟之后的时间字符串
  328. *
  329. * @param dateStr
  330. * String 时间字符串
  331. * @param later
  332. * int 分钟
  333. * @param formatStr
  334. * String 格式化字符串类型
  335. * @return
  336. */
  337. public static String getLaterDateStr(String dateStr, long later, String formatStr) {
  338. Date date = getLaterDate(dateStr, later);
  339. return getDateStr(date, formatStr);
  340. }
  341. /**
  342. * 获取某个分钟之前的时间
  343. *
  344. * @param dateStr
  345. * String 时间字符串
  346. * @param later
  347. * long 分钟
  348. * @return
  349. */
  350. public static Date getBeforeDate(String dateStr, long later) {
  351. Date date = changeStrToDate(dateStr);
  352. date.setTime(date.getTime() - later * 60 * 1000);
  353. return date;
  354. }
  355. /**
  356. * 获取某个分钟之前的时间字符串
  357. *
  358. * @param dateStr
  359. * String 时间字符串
  360. * @param later
  361. * int 分钟
  362. * @param formatStr
  363. * String 格式化字符串类型
  364. * @return
  365. */
  366. public static String getBeforeDateStr(String dateStr, long later, String formatStr) {
  367. Date date = getBeforeDate(dateStr, later);
  368. return getDateStr(date, formatStr);
  369. }
  370. public static int getDaysBetween(Date date1, Date date2) {
  371. Calendar c1 = Calendar.getInstance();
  372. c1.setTime(date1);
  373. Calendar c2 = Calendar.getInstance();
  374. c2.setTime(date2);
  375. if (c1.after(c2)) {
  376. Calendar swap = c1;
  377. c1 = c2;
  378. c2 = swap;
  379. }
  380. int days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
  381. int y2 = c2.get(Calendar.YEAR);
  382. if (c1.get(Calendar.YEAR) != y2) {
  383. c1 = (Calendar) c1.clone();
  384. do {
  385. days += c1.getActualMaximum(Calendar.DAY_OF_YEAR);
  386. c1.add(Calendar.YEAR, 1);
  387. } while (c1.get(Calendar.YEAR) != y2);
  388. }
  389. return days;
  390. }
  391. public static long getHoursBetween(String startTime, String endTime, String format) {
  392. // 按照传入的格式生成一个simpledateformate对象
  393. SimpleDateFormat sd = new SimpleDateFormat(format);
  394. long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
  395. long nh = 1000 * 60 * 60;// 一小时的毫秒数
  396. long nm = 1000 * 60;// 一分钟的毫秒数
  397. long ns = 1000;// 一秒钟的毫秒数long diff;try {
  398. // 获得两个时间的毫秒时间差异
  399. try {
  400. Long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
  401. long day = diff / nd;// 计算差多少天
  402. long hour = diff / nh;// 计算差多少小时
  403. long min = diff / nm;// 计算差多少分钟
  404. long sec = diff / ns;// 计算差多少秒//输出结果
  405. return hour;
  406. } catch (ParseException e) {
  407. e.printStackTrace();
  408. }
  409. return 0;
  410. }
  411. /**
  412. * 得到精确到秒的格式化当前时间串
  413. *
  414. * @param formatType
  415. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  416. * {@link #CN_HTML_DISPLAY_DATE}
  417. * @return 当前时间格式化时间串
  418. */
  419. public static String getCurrTimeStr(int formatType) {
  420. return getTimeStr(new Date(), formatType);
  421. }
  422. /**
  423. * 根据传入的OutFormat,DateStr进行格式化
  424. *
  425. * @param DateStr
  426. * 时间字符串,OutFormat输出格式字符串
  427. * @return 根据格式字符串格式后的字符串类型
  428. */
  429. public static String ToDisplayStrByFormarStr(String DateStr, String OutFormat) {
  430. OutFormat = OutFormat.trim();
  431. if (!"yyyyMMdd".equals(OutFormat) && !"yyyyMMddHHmmss".equals(OutFormat) && !"yyyy-MM-dd".equals(OutFormat) && !"yyyy-MM-dd HH:mm:ss".equals(OutFormat) && !"yyyy.MM.dd".equals(OutFormat) && !"yyyy.MM.dd HH:mm:ss".equals(OutFormat)
  432. && !"yyyy年MM月dd日".equals(OutFormat) && !"yyyy年MM月dd日 HH:mm:ss".equals(OutFormat) && !"yyyy年MM月dd日 HH时mm分ss秒".equals(OutFormat)) {
  433. throw new IllegalArgumentException("时间格式化字符串不是合法的值。");
  434. } else {
  435. String formatStr = null;
  436. if ("yyyyMMdd".equals(OutFormat) || "yyyyMMddHHmmss".equals(OutFormat)) {
  437. formatStr = toDisplayStr(DateStr, DB_STORE_DATE);
  438. }
  439. if ("yyyy-MM-dd".equals(OutFormat) || "yyyy-MM-dd HH:mm:ss".equals(OutFormat)) {
  440. formatStr = toDisplayStr(DateStr, HYPHEN_DISPLAY_DATE);
  441. }
  442. if ("yyyy.MM.dd".equals(OutFormat) || "yyyy.MM.dd HH:mm:ss".equals(OutFormat)) {
  443. formatStr = toDisplayStr(DateStr, DOT_DISPLAY_DATE);
  444. }
  445. if ("yyyy年MM月dd日".equals(OutFormat) || "yyyy年MM月dd日 HH:mm:ss".equals(OutFormat) || "yyyy年MM月dd日 HH时mm分ss秒".equals(OutFormat)) {
  446. formatStr = toDisplayStr(DateStr, CN_DISPLAY_DATE);
  447. }
  448. return formatStr;
  449. }
  450. }
  451. /**
  452. * 得到精确到秒的格式化时间串
  453. *
  454. * @param date
  455. * 指定时间
  456. * @param formatType
  457. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  458. * {@link #CN_HTML_DISPLAY_DATE}
  459. * @return 指定时间的格式化时间串
  460. */
  461. public static String getTimeStr(Date date, int formatType) {
  462. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  463. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  464. } else {
  465. String formatStr = null;
  466. switch (formatType) {
  467. case DB_STORE_DATE:
  468. formatStr = "yyyyMMddHHmmss";
  469. break;
  470. case HYPHEN_DISPLAY_DATE:
  471. formatStr = "yyyy'-'MM'-'dd HH:mm:ss";
  472. break;
  473. case DOT_DISPLAY_DATE:
  474. formatStr = "yyyy.MM.dd HH:mm:ss";
  475. break;
  476. case CN_DISPLAY_DATE:
  477. formatStr = "yyyy'年'MM'月'dd HH:mm:ss";
  478. break;
  479. }
  480. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  481. return sdf.format(date);
  482. }
  483. }
  484. /**
  485. * 得到精确到天的当前格式化日期串
  486. *
  487. * @param formatType
  488. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  489. * {@link #CN_HTML_DISPLAY_DATE}
  490. * @return
  491. */
  492. public static String getCurrDateStr(int formatType) {
  493. return getDateStr(new Date(), formatType);
  494. }
  495. /**
  496. * 得到精确到天的指定时间格式化日期串
  497. *
  498. * @param date
  499. * 指定时间
  500. * @param formatType
  501. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  502. * {@link #CN_HTML_DISPLAY_DATE}
  503. * @return 指定时间格式化日期串
  504. */
  505. public static String getDateStr(Date date, int formatType) {
  506. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  507. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  508. } else {
  509. String formatStr = null;
  510. switch (formatType) {
  511. case DB_STORE_DATE:
  512. formatStr = "yyyyMMdd";
  513. break;
  514. case HYPHEN_DISPLAY_DATE:
  515. formatStr = "yyyy-MM-dd";
  516. break;
  517. case DOT_DISPLAY_DATE:
  518. formatStr = "yyyy.MM.dd";
  519. break;
  520. case CN_DISPLAY_DATE:
  521. formatStr = "yyyy'年'MM'月'dd";
  522. break;
  523. }
  524. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  525. return sdf.format(date);
  526. }
  527. }
  528. /**
  529. * 得到精确到月的当前时间格式化年月串
  530. *
  531. * @param formatType
  532. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  533. * {@link #CN_HTML_DISPLAY_DATE}
  534. * @return 精确到月当前时间格式化年月串
  535. */
  536. public static String getYearMonthStr(int formatType) {
  537. return getYearMonthStr(new Date(), formatType);
  538. }
  539. /**
  540. * 得到精确到月的指定时间格式化年月串
  541. *
  542. * @param date
  543. * 指定的时间
  544. * @param formatType
  545. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  546. * {@link #CN_HTML_DISPLAY_DATE}
  547. * @return 精确到月当前时间格式化年月串
  548. */
  549. public static String getYearMonthStr(Date date, int formatType) {
  550. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  551. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  552. } else {
  553. String formatStr = null;
  554. switch (formatType) {
  555. case DB_STORE_DATE:
  556. formatStr = "yyyyMM";
  557. break;
  558. case HYPHEN_DISPLAY_DATE:
  559. formatStr = "yyyy-MM";
  560. break;
  561. case DOT_DISPLAY_DATE:
  562. formatStr = "yyyy.MM";
  563. break;
  564. case CN_DISPLAY_DATE:
  565. formatStr = "yyyy'年'MM'月'";
  566. break;
  567. }
  568. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  569. return sdf.format(date);
  570. }
  571. }
  572. /**
  573. * 将数据库存储的日期格式串转换为各种显示的格式
  574. *
  575. * @param dateStr
  576. * 最小6位,最大14位的数据库存储格式时间串如:20041212
  577. * @param formatType
  578. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  579. * {@link #CN_HTML_DISPLAY_DATE}
  580. * @return 格式化的时间串
  581. */
  582. public static String toDisplayStr(String dateStr, int formatType) {
  583. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  584. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  585. }
  586. if (dateStr == null || dateStr.length() < 6 || formatType == DB_STORE_DATE) {
  587. return StringUtils.toVisualString(dateStr);
  588. } else {
  589. if (dateStr.length() > 14) {
  590. dateStr = StringUtils.substring(dateStr, 0, 14);
  591. }
  592. char[] charArr = null;
  593. switch (formatType) {
  594. case HYPHEN_DISPLAY_DATE:
  595. charArr = new char[] { '-', '-', ' ', ':', ':' };
  596. break;
  597. case DOT_DISPLAY_DATE:
  598. charArr = new char[] { '.', '.', ' ', ':', ':' };
  599. break;
  600. case CN_DISPLAY_DATE:
  601. charArr = new char[] { '年', '月', '日', ':', ':' };
  602. break;
  603. default:
  604. charArr = new char[] { '-', '-', ' ', ':', ':' };
  605. }
  606. try {
  607. SimpleDateFormat sdf_1 = null;
  608. SimpleDateFormat sdf_2 = null;
  609. switch (dateStr.length()) {
  610. case 6:
  611. sdf_1 = new SimpleDateFormat("yyyyMM");
  612. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM");
  613. break;
  614. case 8:
  615. sdf_1 = new SimpleDateFormat("yyyyMMdd");
  616. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd");
  617. break;
  618. case 10:
  619. sdf_1 = new SimpleDateFormat("yyyyMMddHH");
  620. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH");
  621. break;
  622. case 12:
  623. sdf_1 = new SimpleDateFormat("yyyyMMddHHmm");
  624. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm");
  625. break;
  626. case 14:
  627. sdf_1 = new SimpleDateFormat("yyyyMMddHHmmss");
  628. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm'" + charArr[4] + "'ss");
  629. break;
  630. case 17:
  631. sdf_1 = new SimpleDateFormat("yyyyMMddHHmmss");
  632. sdf_2 = new SimpleDateFormat("yyyy'" + charArr[0] + "'MM'" + charArr[1] + "'dd'" + charArr[2] + "'HH'" + charArr[3] + "'mm'" + charArr[4] + "'ss");
  633. break;
  634. default:
  635. return dateStr;
  636. }
  637. return sdf_2.format(sdf_1.parse(dateStr));
  638. } catch (ParseException ex) {
  639. return dateStr;
  640. }
  641. }
  642. }
  643. /**
  644. * 将数据库存储的日期格式串转换为各种显示的格式
  645. *
  646. * @param dateStr
  647. * 最小6位,最大14位的数据库存储格式时间串如:20041212
  648. * @param formatType
  649. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  650. * {@link #CN_HTML_DISPLAY_DATE}
  651. * @return 格式化的时间串
  652. */
  653. public static String toDisplayStrCn(String dateStr, int dateLen) {
  654. String formatdate = dateStr.replaceAll("/", "").replaceAll(":", "").replaceAll("-", "").trim();
  655. String temp_str = "";
  656. char[] c = formatdate.toCharArray();
  657. boolean bb = false;
  658. for (int j = 0; j < c.length; j++) {
  659. if (!Character.isDigit(c[j])) {
  660. bb = true;
  661. break;
  662. }
  663. }
  664. if (bb) {
  665. return dateStr;
  666. }
  667. formatdate = formatdate.replaceAll(" ", "");
  668. if (formatdate == null || "".equals(formatdate.trim()))
  669. return "";
  670. switch (dateLen) {
  671. case 4:
  672. if (formatdate.length() >= dateLen)
  673. temp_str = formatdate.substring(0, dateLen) + "年";
  674. else
  675. temp_str = formatdate.substring(0, formatdate.length()) + "年";
  676. break;
  677. case 6:
  678. if (formatdate.length() >= dateLen) {
  679. temp_str = formatdate.substring(0, 4) + "年";
  680. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  681. } else
  682. temp_str = formatdate.substring(0, formatdate.length()) + "年";
  683. break;
  684. case 8:
  685. if (formatdate.length() >= dateLen) {
  686. temp_str = formatdate.substring(0, 4) + "年";
  687. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  688. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  689. } else if (formatdate.length() >= 6) {
  690. temp_str = formatdate.substring(0, 4) + "年";
  691. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  692. } else {
  693. temp_str = formatdate.substring(0, formatdate.length()) + "年";
  694. }
  695. break;
  696. case 12:
  697. if (formatdate.length() >= dateLen) {
  698. temp_str = formatdate.substring(0, 4) + "年";
  699. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  700. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  701. temp_str = temp_str + Integer.valueOf(formatdate.substring(8, 10)) + "时";
  702. temp_str = temp_str + Integer.valueOf(formatdate.substring(10, 12)) + "分";
  703. } else if (formatdate.length() >= 10) {
  704. temp_str = formatdate.substring(0, 4) + "年";
  705. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  706. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  707. temp_str = temp_str + Integer.valueOf(formatdate.substring(8, 10)) + "时";
  708. } else if (formatdate.length() >= 8) {
  709. temp_str = formatdate.substring(0, 4) + "年";
  710. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  711. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  712. } else if (formatdate.length() >= 6) {
  713. temp_str = formatdate.substring(0, 4) + "年";
  714. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  715. } else {
  716. temp_str = formatdate.substring(0, formatdate.length()) + "年";
  717. }
  718. break;
  719. case 14:
  720. if (formatdate.length() >= dateLen) {
  721. temp_str = formatdate.substring(0, 4) + "年";
  722. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  723. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  724. temp_str = temp_str + Integer.valueOf(formatdate.substring(8, 10)) + "时";
  725. temp_str = temp_str + Integer.valueOf(formatdate.substring(10, 12)) + "分";
  726. temp_str = temp_str + Integer.valueOf(formatdate.substring(12, 14)) + "秒";
  727. } else if (formatdate.length() >= 12) {
  728. temp_str = formatdate.substring(0, 4) + "年";
  729. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  730. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  731. temp_str = temp_str + Integer.valueOf(formatdate.substring(8, 10)) + "时";
  732. temp_str = temp_str + Integer.valueOf(formatdate.substring(10, 12)) + "分";
  733. } else if (formatdate.length() >= 10) {
  734. temp_str = formatdate.substring(0, 4) + "年";
  735. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  736. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  737. temp_str = temp_str + Integer.valueOf(formatdate.substring(8, 10)) + "时";
  738. } else if (formatdate.length() >= 8) {
  739. temp_str = formatdate.substring(0, 4) + "年";
  740. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  741. temp_str = temp_str + Integer.valueOf(formatdate.substring(6, 8)) + "日";
  742. } else if (formatdate.length() >= 6) {
  743. temp_str = formatdate.substring(0, 4) + "年";
  744. temp_str = temp_str + Integer.valueOf(formatdate.substring(4, 6)) + "月";
  745. } else {
  746. temp_str = formatdate.substring(0, formatdate.length()) + "年";
  747. }
  748. break;
  749. default:
  750. return dateStr;
  751. }
  752. return temp_str;
  753. }
  754. /**
  755. * 将显示格式的时间字符串转换为数据库存储的类型
  756. *
  757. * @param dateStr
  758. * 最小4位,最大19位。显示的时间格式时间串如:2004-12-12
  759. * @return 数据库存储的时间字符串
  760. */
  761. public static String toStoreStr(String dateStr) {
  762. if (dateStr == null || dateStr.trim().equals("")) {
  763. return "";
  764. }
  765. StringBuffer strBuf = new StringBuffer();
  766. for (int i = 0; i < dateStr.length(); i++) {
  767. if (dateStr.charAt(i) >= '0' && dateStr.charAt(i) <= '9') {
  768. strBuf.append(dateStr.charAt(i));
  769. }
  770. }
  771. return strBuf.toString();
  772. }
  773. /**
  774. * 将生日存储的时间格式转化为年龄(周岁,小数点后不计)
  775. *
  776. * @param birthdayStr
  777. * 生日字段 "yyyymmdd"
  778. * @return 年龄
  779. */
  780. public static String birthdayToAge(String birthdayStr) {
  781. if (birthdayStr == null || birthdayStr.length() < 6) {
  782. return "";
  783. } else {
  784. int birthYear = Integer.parseInt(birthdayStr.substring(0, 4));
  785. int birthMonth = Integer.parseInt(birthdayStr.substring(4, 6));
  786. Calendar cal = new GregorianCalendar();
  787. int currYear = cal.get(Calendar.YEAR);
  788. int currMonth = cal.get(Calendar.MONTH)+1;
  789. int age = currYear - birthYear;
  790. age -= (currMonth < birthMonth) ? 1 : 0;
  791. return "" + age;
  792. }
  793. }
  794. /**
  795. * 将生日存储的时间格式转化为年龄(周岁,小数点后不计)
  796. *
  797. * @param birthdayStr
  798. * 生日字段 "yyyy-MM-dd"
  799. * @return 年龄
  800. */
  801. public static String birthdayToAge2(String birthdayStr) {
  802. if (birthdayStr == null || birthdayStr.length() < 8) {
  803. return "";
  804. } else {
  805. Calendar cal2 = new GregorianCalendar();
  806. cal2.setTime(getDate(birthdayStr, "yyyy-MM-dd"));
  807. int birthYear = Integer.parseInt(birthdayStr.substring(0, 4));
  808. Calendar cal = new GregorianCalendar();
  809. int currYear = cal.get(Calendar.YEAR);
  810. int age = currYear - birthYear;
  811. cal2.add(Calendar.YEAR, age);
  812. if (cal.getTimeInMillis() < cal2.getTimeInMillis()) {
  813. age = age - 1;
  814. }
  815. if (age < 0) {
  816. age = 0;
  817. }
  818. return "" + age;
  819. }
  820. }
  821. /**
  822. * @param dateTimeStr
  823. * String 格式化的时间串
  824. * @param formatType
  825. * 数据格式类型 {@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  826. * {@link #CN_HTML_DISPLAY_DATE}
  827. * @param detal
  828. * int 增加或减少的时间
  829. * @param field
  830. * int 参见Calendar中关于时间字段属性的定义
  831. * @return String 返回的
  832. */
  833. public static String add(String dateTimeStr, int formatType, int detal, int field) {
  834. if (dateTimeStr == null || dateTimeStr.length() < 6) {
  835. return dateTimeStr;
  836. } else {
  837. try {
  838. String formatStr = null;
  839. switch (formatType) {
  840. case DB_STORE_DATE:
  841. formatStr = "yyyyMMddHHmmss";
  842. break;
  843. case HYPHEN_DISPLAY_DATE:
  844. formatStr = "yyyy-MM-dd HH:mm:ss";
  845. break;
  846. case DOT_DISPLAY_DATE:
  847. formatStr = "yyyy.MM.dd HH:mm:ss";
  848. break;
  849. case CN_DISPLAY_DATE:
  850. formatStr = "yyyy'年'MM'月' HH:mm:ss";
  851. break;
  852. default:
  853. formatStr = "yyyyMMddHHmmss";
  854. break;
  855. }
  856. formatStr = formatStr.substring(0, dateTimeStr.length());
  857. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  858. Date d = sdf.parse(dateTimeStr);
  859. GregorianCalendar g = new GregorianCalendar();
  860. g.setTime(d);
  861. g.add(field, detal);
  862. d = g.getTime();
  863. return sdf.format(d);
  864. } catch (ParseException ex) {
  865. ex.printStackTrace();
  866. return dateTimeStr;
  867. }
  868. }
  869. }
  870. // /**
  871. // * @param date Date 时间
  872. // * @param detal int 增加的时间
  873. // * @param field int 参见Calendar中关于时间字段属性的定义
  874. // * @return Date
  875. // */
  876. // public static Date add(Date date, int detal, int field)
  877. // {
  878. // Calendar g = new GregorianCalendar();
  879. // g.setTime(date);
  880. // g.add(field, detal);
  881. // return g.getTime();
  882. // }
  883. /**
  884. * 日期、时间格式化
  885. *
  886. * @param date
  887. * Date 将要被格式化的日期对象
  888. * @param outFmt
  889. * String 返回样式,参照类说明,如:yyyy年MM月dd日
  890. * @return String 格式化后的日期、时间字符串,data为null时返回null,outFmt非法时返回yyyyMMdd格式
  891. */
  892. public static String getDateFormat(Date date, String outFmt) {
  893. if (null == date) {
  894. return null;
  895. }
  896. if (null == outFmt || "".equals(outFmt.trim())) { // outFmt非法
  897. outFmt = "yyyyMMdd";
  898. }
  899. String retu = null;
  900. SimpleDateFormat dateFormat = null;
  901. try {
  902. dateFormat = new SimpleDateFormat(outFmt);
  903. } catch (IllegalArgumentException iaex) { // outFmt非法
  904. dateFormat = new SimpleDateFormat("yyyyMMdd");
  905. }
  906. retu = dateFormat.format(date);
  907. dateFormat = null;
  908. return retu;
  909. }
  910. /**
  911. * 把日期时间对象格式化为yyyyMMdd样式
  912. *
  913. * @param date
  914. * Date 将要被格式化的日期对象
  915. * @return String 格式化后的日期、时间字符串,如:20041001
  916. */
  917. public static String getDateFormat(Date date) {
  918. return getDateFormat(date, "yyyyMMdd");
  919. }
  920. /**
  921. * 把系统当前日期时间格式化为指定的样式
  922. *
  923. * @param outFmt
  924. * String 返回样式,参照类说明,如:yyyy年MM月dd日
  925. * @return String 格式化后的日期、时间字符串,如:2004年10月01日
  926. */
  927. public static String getDateFormat(String outFmt) {
  928. return getDateFormat(new Date(), outFmt);
  929. }
  930. /**
  931. * 把系统当前日期时间格式化为默认样式yyyyMMdd
  932. *
  933. * @return String 格式化后的日期、时间字符串,如:20041001
  934. */
  935. public static String getDateFormat() {
  936. return getDateFormat(new Date(), "yyyyMMdd");
  937. }
  938. /**
  939. * 日期、时间格式化
  940. *
  941. * @param millis
  942. * long the number of milliseconds(毫秒) since January 1, 1970,
  943. * 00:00:00 GMT.
  944. * @param outFmt
  945. * String 返回样式,参照类说明,如:yyyy年MM月dd日
  946. * @return String 格式化后的日期、时间字符串
  947. */
  948. public static String getDateFormat(long millis, String outFmt) {
  949. Calendar calendar = Calendar.getInstance();
  950. calendar.setTimeInMillis(millis);
  951. String retu = getDateFormat(calendar.getTime(), outFmt);
  952. calendar = null;
  953. return retu;
  954. }
  955. /**
  956. * 日期、时间格式化
  957. *
  958. * @param datestr
  959. * String 存在一定格式的日期、时间字符串,如:20041001、200410011503
  960. * @param inFmt
  961. * String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
  962. * @param outFmt
  963. * String 返回样式,参照类说明,如:yyyy年MM月dd日
  964. * @throws ParseException
  965. * 当datestr不能格式化为inFmt格式时抛出此异常
  966. * @return String 格式化后的日期、时间字符串,如:2004年10月01日、2004年10月01日 <BR>
  967. * 输出样式outFmt非法时,使用yyyyMMdd格式输出
  968. */
  969. public static String getDateFormat(String datestr, String inFmt, String outFmt) throws ParseException {
  970. if (null == datestr || "".equals(datestr.trim())) {
  971. return datestr;
  972. }
  973. if (null == inFmt || "".equals(inFmt.trim())) {
  974. return datestr;
  975. }
  976. if (null == outFmt || "".equals(outFmt.trim())) { // 输出样式非法
  977. outFmt = "yyyyMMdd";
  978. }
  979. java.util.Date inDate = getDate(datestr, inFmt);
  980. if (null == inDate) { // 根据inFmt分析datestr时抛出异常
  981. return datestr;
  982. }
  983. String retu = getDateFormat(inDate, outFmt);
  984. inDate = null;
  985. return retu;
  986. }
  987. /**
  988. * 把日期时间字符串,按inFmt样式转化为日期对象,然后格式化为默认样式yyyyMMdd
  989. *
  990. * @param datestr
  991. * String 存在一定格式的日期、时间字符串,如:20041001、200410011503
  992. * @param inFmt
  993. * String 对datestr参数格式说明,参照类说明,如:yyyyMMdd、yyyyMMddHHmm
  994. * @throws ParseException
  995. * 当datestr不能格式化为inFmt格式时抛出此异常
  996. * @return String 格式化后的日期、时间字符串,如:20041001、20041001
  997. */
  998. public static String getDateFormat(String datestr, String inFmt) throws ParseException {
  999. return getDateFormat(datestr, inFmt, "yyyyMMdd");
  1000. }
  1001. /**
  1002. * 根据inFmt的样式,日期时间字符串转化为日期时间对象
  1003. *
  1004. * @param datestr
  1005. * String 日期时间字符串,如:20041001、2004年10月01日 15:03
  1006. * @param inFmt
  1007. * String 对datestr参数格式说明,参照类说明,如yyyyMMdd、yyyy年MM月dd日 HH:mm
  1008. * @return Date 日期时间对象,格式inFmt非法时,使用yyyyMMdd格式
  1009. */
  1010. public static Date getDate(String datestr, String inFmt) {
  1011. if (null == datestr || "".equals(datestr.trim())) {
  1012. return null;
  1013. }
  1014. if (null == inFmt || "".equals(inFmt.trim())) { // inFmt非法
  1015. inFmt = "yyyyMMdd";
  1016. }
  1017. java.util.Date inDate = null;
  1018. // 依据inFmt格式把日期字符串转化为日期对象
  1019. SimpleDateFormat inDateFormat = new SimpleDateFormat(inFmt);
  1020. inDateFormat.setLenient(true);
  1021. try {
  1022. inDate = inDateFormat.parse(datestr);
  1023. } catch (ParseException e) {
  1024. logger.error("日期转换出错:" + datestr,e);
  1025. }
  1026. return inDate;
  1027. }
  1028. /**
  1029. * 对日期时间对象进行调整,实现如昨天是几号,去年的今天星期几等. <BR>
  1030. * 例子:
  1031. *
  1032. * <pre>
  1033. * &lt;blockquote&gt;
  1034. * 计算去年今天星期几
  1035. * Date date = DateUtils.addDate(new Date(),Calendar.YEAR,-1);
  1036. * System.out.println(DateUtils.getDateFormat(date,&quot;E&quot;));
  1037. * 打印60天后是什么日期,并显示为 yyyy-MM-dd 星期
  1038. * Date date = DateUtils.addDate(new Date(),Calendar.DATE,60);
  1039. * System.out.println(DateUtils.getDateFormat(date,&quot;yyyy-MM-dd E&quot;));
  1040. * &lt;/blockquote&gt;
  1041. * </pre>
  1042. *
  1043. * @param date
  1044. * Date 需要调整的日期时间对象
  1045. * @param CALENDARFIELD
  1046. * int 对日期时间对象以什么单位进行调整:
  1047. *
  1048. * <pre>
  1049. * &lt;blockquote&gt;
  1050. * 年 Calendar.YEAR
  1051. * 月 Calendar.MONTH
  1052. * 日 Calendar.DATE
  1053. * 时 Calendar.HOUR
  1054. * 分 Calendar.MINUTE
  1055. * 秒 Calendar.SECOND
  1056. * &lt;/blockquote&gt;
  1057. * </pre>
  1058. *
  1059. * @param amount
  1060. * int 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
  1061. * @return Date 调整后的日期时间对象
  1062. */
  1063. public static Date addDate(Date date, int CALENDARFIELD, int amount) {
  1064. if (null == date) {
  1065. return date;
  1066. }
  1067. Calendar calendar = Calendar.getInstance();
  1068. calendar.setTime(date);
  1069. calendar.add(CALENDARFIELD, amount);
  1070. return calendar.getTime();
  1071. }
  1072. /**
  1073. * 对日期时间对象进行调整.
  1074. *
  1075. * @param datestr
  1076. * String 需要调整的日期时间字符串,它的格式为yyyyMMdd
  1077. * @param CALENDARFIELD
  1078. * int 对日期时间对象以什么单位进行调整
  1079. * @param amount
  1080. * int 调整数量
  1081. * @throws ParseException
  1082. * 当datestr不能格式化为yyyyMMdd格式时抛出此异常
  1083. * @return Date 调整后的日期时间对象
  1084. * @see #addDate(java.util.Date, int, int)
  1085. */
  1086. public static Date addDate(String datestr, int CALENDARFIELD, int amount) throws ParseException {
  1087. return addDate(getDate(datestr, "yyyyMMdd"), CALENDARFIELD, amount);
  1088. }
  1089. /**
  1090. * 对日期时间对象进行调整.
  1091. *
  1092. * @param datestr
  1093. * String 需要调整的日期时间字符串,它的格式为yyyyMMdd
  1094. * @param CALENDARFIELD
  1095. * int 对日期时间对象以什么单位进行调整
  1096. * @param amount
  1097. * int 调整数量
  1098. * @throws ParseException
  1099. * 当datestr不能格式化为yyyyMMdd格式时抛出此异常
  1100. * @return Date 调整后的日期时间对象
  1101. * @see #addDate(java.util.Date, int, int)
  1102. */
  1103. public static Date addDate(String datestr, String fmt, int CALENDARFIELD, int amount) throws ParseException {
  1104. return addDate(getDate(datestr, fmt), CALENDARFIELD, amount);
  1105. }
  1106. /**
  1107. * 根据出生日期,计算出在某一个日期的年龄
  1108. *
  1109. * @param birthday
  1110. * Date 出生日期时间对象
  1111. * @param date2
  1112. * Date 计算日期对象
  1113. * @return int 返回date2那一天出生日期为birthday的年龄,如果birthday大于date2则返回-1
  1114. */
  1115. public static int getAge(Date birthday, Date date2) {
  1116. if (null == birthday || null == date2) {
  1117. return -1;
  1118. }
  1119. if (birthday.after(date2)) { // birthday大于date2
  1120. return -1;
  1121. }
  1122. int ibdYear = StringUtils.getInt(getDateFormat(birthday, "yyyy"), -1);
  1123. int idate2Year = StringUtils.getInt(getDateFormat(date2, "yyyy"), -1);
  1124. if (ibdYear < 0 || idate2Year < 0) {
  1125. return -1;
  1126. }
  1127. if (ibdYear > idate2Year) {
  1128. return -1;
  1129. }
  1130. return idate2Year - ibdYear + 1;
  1131. }
  1132. /**
  1133. * 根据出生日期,计算出当前的年龄
  1134. *
  1135. * @param birthday
  1136. * Date 出生日期时间对象
  1137. * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
  1138. */
  1139. public static int getAge(Date birthday) {
  1140. return getAge(birthday, new Date());
  1141. }
  1142. /**
  1143. * 根据出生日期,计算出当前的年龄
  1144. *
  1145. * @param birthdaystr
  1146. * String 出生日期时间字符串,其格式一定为yyyyMMdd
  1147. * @throws ParseException
  1148. * 当datestr不能格式化为yyyyMMdd格式时抛出此异常
  1149. * @return int 返回出生日期为birthday的年龄,如果birthday大于当前系统日期则返回-1
  1150. */
  1151. public static int getAge(String birthdaystr) throws ParseException {
  1152. return getAge(getDate(birthdaystr, "yyyyMMdd"));
  1153. }
  1154. /**
  1155. * 得到14位的当前格式化时间
  1156. *
  1157. * @param formatType
  1158. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  1159. * {@link #CN_HTML_DISPLAY_DATE}
  1160. * @return java.lang.String
  1161. */
  1162. public static String getCurrTime(int formatType) {
  1163. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  1164. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  1165. } else {
  1166. String formatStr = null;
  1167. switch (formatType) {
  1168. case DB_STORE_DATE:
  1169. formatStr = "yyyyMMddHHmmss";
  1170. break;
  1171. case HYPHEN_DISPLAY_DATE:
  1172. formatStr = "yyyy'-'MM'-'dd HH:mm:ss";
  1173. break;
  1174. case DOT_DISPLAY_DATE:
  1175. formatStr = "yyyy.MM.dd HH:mm:ss";
  1176. break;
  1177. case CN_DISPLAY_DATE:
  1178. formatStr = "yyyy'年'MM'月'dd'日' HH时mm分ss秒";
  1179. break;
  1180. }
  1181. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  1182. return sdf.format(new Date());
  1183. }
  1184. }
  1185. /**
  1186. * 得到8位的当前格式化日期
  1187. *
  1188. * @param formatType
  1189. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  1190. * {@link #CN_HTML_DISPLAY_DATE}
  1191. * @return java.lang.String
  1192. */
  1193. public static String getCurrDate(int formatType) {
  1194. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  1195. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  1196. } else {
  1197. String formatStr = null;
  1198. switch (formatType) {
  1199. case DB_STORE_DATE:
  1200. formatStr = "yyyyMMdd";
  1201. break;
  1202. case HYPHEN_DISPLAY_DATE:
  1203. formatStr = "yyyy-MM-dd";
  1204. break;
  1205. case DOT_DISPLAY_DATE:
  1206. formatStr = "yyyy.MM.dd";
  1207. break;
  1208. case CN_DISPLAY_DATE:
  1209. formatStr = "yyyy'年'MM'月'dd'日'";
  1210. break;
  1211. }
  1212. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  1213. return sdf.format(new Date());
  1214. }
  1215. }
  1216. /**
  1217. * 得到6位的当前格式化年月
  1218. *
  1219. * @param formatType
  1220. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  1221. * {@link #CN_HTML_DISPLAY_DATE}
  1222. * @return java.lang.String
  1223. */
  1224. public static String getYearMonth(int formatType) {
  1225. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  1226. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  1227. } else {
  1228. String formatStr = null;
  1229. switch (formatType) {
  1230. case DB_STORE_DATE:
  1231. formatStr = "yyyyMM";
  1232. break;
  1233. case HYPHEN_DISPLAY_DATE:
  1234. formatStr = "yyyy-MM";
  1235. break;
  1236. case DOT_DISPLAY_DATE:
  1237. formatStr = "yyyy.MM";
  1238. break;
  1239. case CN_DISPLAY_DATE:
  1240. formatStr = "yyyy'年'MM'月'";
  1241. break;
  1242. }
  1243. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  1244. return sdf.format(new Date());
  1245. }
  1246. }
  1247. /**
  1248. * 得到6位的上月格式化年月
  1249. *
  1250. * @param formatType
  1251. * 时间格式的类型{@link #DB_STORE_DATE}, {@link #EN_HTML_DISPLAY_DATE},
  1252. * {@link #CN_HTML_DISPLAY_DATE}
  1253. * @return java.lang.String
  1254. */
  1255. public static String getLastYearMonth(int formatType) {
  1256. if (formatType < DB_STORE_DATE || formatType > CN_DISPLAY_DATE) {
  1257. throw new IllegalArgumentException("时间格式化类型不是合法的值。");
  1258. } else {
  1259. String formatStr = null;
  1260. switch (formatType) {
  1261. case DB_STORE_DATE:
  1262. formatStr = "yyyyMM";
  1263. break;
  1264. case HYPHEN_DISPLAY_DATE:
  1265. formatStr = "yyyy-MM";
  1266. break;
  1267. case DOT_DISPLAY_DATE:
  1268. formatStr = "yyyy.MM";
  1269. break;
  1270. case CN_DISPLAY_DATE:
  1271. formatStr = "yyyy'年'MM'月'";
  1272. break;
  1273. }
  1274. SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
  1275. Calendar cal = Calendar.getInstance();
  1276. Date date = new Date();
  1277. cal.setTime(date);
  1278. cal.set(Calendar.DATE, 1);
  1279. cal.add(Calendar.MONTH, -1);
  1280. return sdf.format(cal.getTime());
  1281. }
  1282. }
  1283. /**
  1284. * 获取时间所对应的毫秒
  1285. *
  1286. * @param datetime
  1287. * String 时间字符串
  1288. * @param inFmt
  1289. * String 时间格式化串
  1290. * @return long 毫秒数
  1291. * @throws ParseException
  1292. */
  1293. public static long getTimeValue(String datetime, String inFmt) throws ParseException {
  1294. return getDate(datetime, inFmt).getTime();
  1295. }
  1296. /**
  1297. * 获取时间字符串
  1298. *
  1299. * @param milliSeconds
  1300. * long 从1970年到现在的毫
  1301. * @param formatType
  1302. * int 时间类型
  1303. * @return String 时间字符串
  1304. */
  1305. public static String getTimeStr(long milliSeconds, int formatType) {
  1306. Date d = new Date(milliSeconds);
  1307. return getTimeStr(d, formatType);
  1308. }
  1309. /**
  1310. * 对日期时间对象进行调整
  1311. *
  1312. * @param date
  1313. * @param CALENDARFIELD
  1314. *
  1315. * <pre>
  1316. * 年 Calendar.YEAR
  1317. * 月 Calendar.MONTH
  1318. * 日 Calendar.DATE
  1319. * 时 Calendar.HOUR
  1320. * 分 Calendar.MINUTE
  1321. * 秒 Calendar.SECOND
  1322. * </pre>
  1323. *
  1324. * @param amount
  1325. * 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
  1326. * @return
  1327. */
  1328. public static Date transpositionDate(String dateStr, int CALENDARFIELD, int amount) {
  1329. if (null == dateStr) {
  1330. return null;
  1331. }
  1332. return transpositionDate(changeStrToDate(dateStr), CALENDARFIELD, amount);
  1333. }
  1334. /**
  1335. * 对日期时间对象进行调整
  1336. *
  1337. * @param date
  1338. * @param CALENDARFIELD
  1339. *
  1340. * <pre>
  1341. * 年 Calendar.YEAR
  1342. * 月 Calendar.MONTH
  1343. * 日 Calendar.DATE
  1344. * 时 Calendar.HOUR
  1345. * 分 Calendar.MINUTE
  1346. * 秒 Calendar.SECOND
  1347. * </pre>
  1348. *
  1349. * @param amount
  1350. * 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
  1351. * @return
  1352. */
  1353. public static Date transpositionDate(Date date, int CALENDARFIELD, int amount) {
  1354. if (null == date) {
  1355. return date;
  1356. }
  1357. Calendar calendar = Calendar.getInstance();
  1358. calendar.setTime(date);
  1359. calendar.add(CALENDARFIELD, amount);
  1360. return calendar.getTime();
  1361. }
  1362. /**
  1363. * 对日期时间对象进行调整
  1364. *
  1365. * @param date
  1366. * @param CALENDARFIELD
  1367. *
  1368. * <pre>
  1369. * 年 Calendar.YEAR
  1370. * 月 Calendar.MONTH
  1371. * 日 Calendar.DATE
  1372. * 时 Calendar.HOUR
  1373. * 分 Calendar.MINUTE
  1374. * 秒 Calendar.SECOND
  1375. * </pre>
  1376. *
  1377. * @param amount
  1378. * 调整数量,>0表向后调整(明天,明年),<0表向前调整(昨天,去年)
  1379. * @return
  1380. */
  1381. public static String transpositionDateStr(String dateStr, int CALENDARFIELD, int amount) {
  1382. if (null == dateStr) {
  1383. return dateStr;
  1384. }
  1385. return changeDateToStr(transpositionDate(dateStr, CALENDARFIELD, amount), DB_STORE_DATE_FULL);
  1386. }
  1387. /**
  1388. * 得到格式化时间串
  1389. *
  1390. * @param date
  1391. * 指定时间
  1392. * @param formatType
  1393. * 时间格式的类型
  1394. * @return 指定时间的格式化时间串
  1395. */
  1396. public static String changeDateToStr(Date date, String formatStr) {
  1397. SimpleDateFormat fomate = new SimpleDateFormat(formatStr);
  1398. return fomate.format(date);
  1399. }
  1400. /**
  1401. * 根据时间跨距,取得对应的行政月。例如:9-26到10-26 返回的月份为10月
  1402. *
  1403. * @day 从那天开始(例如:26)
  1404. * @format 时间格式的类型:YYMMDD、YYMMDDHHmMSS
  1405. * @return 指定的格式化时间
  1406. */
  1407. public static String getStrationTime(int day, String formatStr) {
  1408. Calendar cal = Calendar.getInstance();
  1409. Date date = cal.getTime();
  1410. Calendar g = new GregorianCalendar();
  1411. g.setTime(date);
  1412. if (cal.get(Calendar.DATE) > day) {
  1413. g.add(Calendar.MONTH, 1);
  1414. }
  1415. Date d = g.getTime();
  1416. return getDateStr(d, formatStr);
  1417. }
  1418. /**
  1419. * 计算两个任意日期间的相差的天数(自然天)
  1420. *
  1421. * @param fromDay
  1422. * 起始日期
  1423. * @param toDay
  1424. * 终止日期
  1425. * @return 两日期间的相差的天数 (NullPointerException if fromDay or toDay is null) <br>
  1426. * 注:fromDay 可以大于 toDay
  1427. */
  1428. public static int daysBetween(Date fromDay, Date toDay) {
  1429. int result = -1;
  1430. if (fromDay != null && toDay != null) {
  1431. fromDay = getFirstTime(fromDay);
  1432. toDay = getFirstTime(toDay);
  1433. long off = fromDay.getTime() - toDay.getTime(); // 毫秒数差值
  1434. result = (int) ((Math.abs(off)) / 1000 / 60 / 60 / 24); // 获取天数的差值
  1435. }
  1436. return result;
  1437. }
  1438. /**
  1439. * 取得当天0点整时刻的日期值
  1440. *
  1441. * @parm srcDate 日期
  1442. * @return 00:00:00:000时刻的日期 如: 输入日期 2009-5-7 11:29:56:101,则输出2009-5-7
  1443. * 00:00:00:000
  1444. */
  1445. public static Date getFirstTime(Date srcDate) {
  1446. Calendar calendar = Calendar.getInstance();
  1447. calendar.setTime(srcDate);
  1448. calendar.set(Calendar.HOUR_OF_DAY, 0);
  1449. calendar.set(Calendar.MINUTE, 0);
  1450. calendar.set(Calendar.SECOND, 0);
  1451. calendar.set(Calendar.MILLISECOND, 0);
  1452. return calendar.getTime();
  1453. }
  1454. /**
  1455. * 取得两个日期间的所有日期(不包括toDate)
  1456. *
  1457. * @param fromDate
  1458. * 起始日期
  1459. * @param toDate
  1460. * 终止日期
  1461. * @return 两个日期间的所有日期(0点时该的值),不包括终止日期,从小到大的顺序
  1462. */
  1463. public static Date[] datesBetween(Date fromDate, Date toDate) {
  1464. Date from = getFirstTime(fromDate);
  1465. Date to = getFirstTime(toDate);
  1466. int days = daysBetween(from, to);
  1467. Date[] dates = new Date[days];
  1468. for (int i = 0; i < days; i++) {
  1469. Calendar calendar = Calendar.getInstance();
  1470. calendar.setTime(from);
  1471. calendar.add(Calendar.DAY_OF_MONTH, i);
  1472. dates[i] = calendar.getTime();
  1473. }
  1474. return dates;
  1475. }
  1476. /**
  1477. * 根据当前日期,加上对应数(不包括toDate)
  1478. *
  1479. * @param type
  1480. * 小时,分钟,毫秒。Calendar中的常量
  1481. * @param addNmu
  1482. * 添加数 DateUtil中的常量
  1483. * @param formatStr
  1484. * 格式化返回的字符串
  1485. * @return 添加完后的时间
  1486. */
  1487. public static String getAddTime(int type, int addNum, String formatStr) {
  1488. Calendar calendar = Calendar.getInstance();
  1489. calendar.setTime(new Date());
  1490. calendar.add(type, addNum);
  1491. calendar.getTime();
  1492. return DateUtils.changeDateToStr(calendar.getTime(), formatStr);
  1493. }
  1494. /**
  1495. * 转成中文日期显示 yyyy年MM月dd日 HH时mm分ss秒
  1496. *
  1497. * @param dateStr
  1498. * @return
  1499. */
  1500. public static String converToChineseDate(String dateStr) {
  1501. if (StringUtils.isNullBlank(dateStr)) {
  1502. throw new RuntimeException("传入日期不能为空");
  1503. }
  1504. SimpleDateFormat sf = null;
  1505. if (dateStr.trim().length() == 8) {
  1506. sf = new SimpleDateFormat("yyyy年MM月dd日");
  1507. } else {
  1508. sf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
  1509. }
  1510. String dateFmt = "";
  1511. Date d = DateUtils.changeStrToDate(dateStr);
  1512. dateFmt = sf.format(d);
  1513. return dateFmt;
  1514. }
  1515. public static String getAddDate(String dateStr, int timeType, int number) {
  1516. String returnDate = "";
  1517. try {
  1518. Date date = DateUtils.addDate(dateStr, timeType, number);
  1519. returnDate = new SimpleDateFormat("yyyyMMdd").format(date);
  1520. } catch (ParseException e) {
  1521. e.printStackTrace();
  1522. }
  1523. return returnDate;
  1524. }
  1525. /**
  1526. * 根据时间日期字符串和格式,返回指定时间跨度的日期
  1527. *
  1528. * @param dateStr
  1529. * @param fmt
  1530. * @param timeType
  1531. * @param number
  1532. * @return
  1533. */
  1534. public static String getAddDate(String dateStr, String fmt, int timeType, int number) {
  1535. String returnDate = "";
  1536. try {
  1537. Date date = DateUtils.addDate(dateStr, fmt, timeType, number);
  1538. returnDate = new SimpleDateFormat(fmt).format(date);
  1539. } catch (ParseException e) {
  1540. e.printStackTrace();
  1541. }
  1542. return returnDate;
  1543. }
  1544. /**
  1545. * 计算两个日期内的天数
  1546. *
  1547. * @param startDay
  1548. * 20120101
  1549. * @param endDay
  1550. * 20121212
  1551. * @return 返回List<String>
  1552. */
  1553. public static List<String> getTwoDateDays(String startDay, String endDay) {
  1554. if (startDay == null)
  1555. return new ArrayList<String>();
  1556. if (endDay == null)
  1557. return new ArrayList<String>();
  1558. if (startDay.length() > 8) {
  1559. startDay = startDay.substring(0, 8);
  1560. }
  1561. if (endDay.length() > 8) {
  1562. endDay = endDay.substring(0, 8);
  1563. }
  1564. List<String> lDate = new ArrayList<String>();
  1565. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  1566. Date dBegin;
  1567. try {
  1568. dBegin = sdf.parse(startDay);
  1569. Date dEnd = sdf.parse(endDay);
  1570. lDate.add(startDay);
  1571. Calendar cal = Calendar.getInstance();
  1572. // 使用给定的 Date 设置此 Calendar 的时间
  1573. cal.setTime(dBegin);
  1574. boolean bContinue = true;
  1575. while (bContinue) {
  1576. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1577. cal.add(Calendar.DAY_OF_MONTH, 1);
  1578. // 测试此日期是否在指定日期之后
  1579. if (dEnd.after(cal.getTime())) {
  1580. lDate.add(DateUtils.getDateFormat(cal.getTime()));
  1581. } else {
  1582. break;
  1583. }
  1584. }
  1585. lDate.add(endDay);
  1586. return lDate;
  1587. } catch (ParseException e) {
  1588. e.printStackTrace();
  1589. }
  1590. return lDate;
  1591. }
  1592. /**
  1593. * 获得两个日期内的月份
  1594. *
  1595. * @param startDay
  1596. * 201201
  1597. * @param endDay
  1598. * 201203
  1599. * @return 返回List<String> {"201201","201202","201203"}
  1600. */
  1601. public static List<String> getTwoDateMonths(String startMonth, String endMonth) {
  1602. List<String> lDate = new ArrayList<String>();
  1603. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
  1604. Date dBegin;
  1605. try {
  1606. dBegin = sdf.parse(startMonth);
  1607. Date dEnd = sdf.parse(endMonth);
  1608. lDate.add(startMonth);
  1609. Calendar cal = Calendar.getInstance();
  1610. // 使用给定的 Date 设置此 Calendar 的时间
  1611. cal.setTime(dBegin);
  1612. boolean bContinue = true;
  1613. while (bContinue) {
  1614. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  1615. cal.add(Calendar.MONTH, 1);
  1616. // 测试此日期是否在指定日期之后
  1617. if (dEnd.after(cal.getTime())) {
  1618. lDate.add(DateUtils.getDateFormat(cal.getTime(), "yyyyMM"));
  1619. } else {
  1620. break;
  1621. }
  1622. }
  1623. lDate.add(endMonth);
  1624. return lDate;
  1625. } catch (ParseException e) {
  1626. e.printStackTrace();
  1627. }
  1628. return lDate;
  1629. }
  1630. /**
  1631. * 获取每个月的第一天和最后一天
  1632. *
  1633. * @param startMon
  1634. * @param endMon
  1635. * @return
  1636. */
  1637. public static List<String[]> getTwoDateMonth(String startMon, String endMon) {
  1638. Calendar scal = Calendar.getInstance();
  1639. Calendar ecal = Calendar.getInstance();
  1640. Date sde = DateUtils.getDate(startMon, "yyyyMM");
  1641. Date ede = DateUtils.getDate(endMon, "yyyyMM");
  1642. scal.setTime(sde);
  1643. ecal.setTime(ede);
  1644. List<Date> dlist = new ArrayList<Date>();
  1645. dlist.add(scal.getTime());
  1646. while (scal.getTime().before(ecal.getTime())) {
  1647. scal.add(Calendar.MONTH, 1);
  1648. dlist.add(scal.getTime());
  1649. }
  1650. List<String[]> sList = new ArrayList<String[]>();
  1651. for (int i = 0; i < dlist.size(); i++) {
  1652. Date d = dlist.get(i);
  1653. String[] mon = new String[2];
  1654. mon[0] = DateUtils.getDateFormat(d);
  1655. Calendar tpCal = Calendar.getInstance();
  1656. tpCal.setTime(d);
  1657. tpCal.add(Calendar.MONTH, 1);
  1658. tpCal.add(Calendar.DAY_OF_MONTH, -1);
  1659. mon[1] = DateUtils.getDateFormat(tpCal.getTime());
  1660. sList.add(mon);
  1661. }
  1662. return sList;
  1663. }
  1664. /**
  1665. * 获取一个月的所有天
  1666. *
  1667. * @param month
  1668. * @return
  1669. */
  1670. public static List<String> getMonthDays(String month) {
  1671. Calendar scal = Calendar.getInstance();
  1672. Date sde = DateUtils.getDate(month, "yyyyMM");
  1673. scal.setTime(sde);
  1674. Date d = scal.getTime();
  1675. String startDay = DateUtils.getDateFormat(d);
  1676. Calendar tpCal = Calendar.getInstance();
  1677. tpCal.setTime(d);
  1678. tpCal.add(Calendar.MONTH, 1);
  1679. tpCal.add(Calendar.DAY_OF_MONTH, -1);
  1680. String endDay = DateUtils.getDateFormat(tpCal.getTime());
  1681. return getTwoDateDays(startDay, endDay);
  1682. }
  1683. /**
  1684. * 获取日期时间0点
  1685. *
  1686. * @param date
  1687. * @return
  1688. */
  1689. public static String getFirstDateTime(String date) {
  1690. if (StringUtils.isNullBlank(date)) {
  1691. return "";
  1692. }
  1693. if (date.length() < 8) {
  1694. return "";
  1695. } else {
  1696. date = StringUtils.rightPad(date.substring(0, 8), 14, "0");
  1697. }
  1698. return date;
  1699. }
  1700. /**
  1701. * 获取日期时间23点59'59
  1702. *
  1703. * @param date
  1704. * @return
  1705. */
  1706. public static String getEndDateTime(String date) {
  1707. if (StringUtils.isNullBlank(date)) {
  1708. return "";
  1709. }
  1710. if (date.length() < 8) {
  1711. return "";
  1712. } else {
  1713. date = date.substring(0, 8) + "235959";
  1714. }
  1715. return date;
  1716. }
  1717. /**
  1718. * 根据日期计算属于第几周
  1719. *
  1720. * @param date
  1721. * 格式 yyyyMMdd
  1722. * @throws ParseException
  1723. */
  1724. public static int getWeekOfYear(String date) {
  1725. try {
  1726. SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
  1727. Calendar cal = Calendar.getInstance();
  1728. cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置每周的第一天为星期一
  1729. // cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
  1730. // cal.setMinimalDaysInFirstWeek(1); // 设置每周最少为1天
  1731. cal.setTime(df.parse(date));
  1732. int year = getWeekOfYear(cal);
  1733. String week = year + "" + cal.get(Calendar.WEEK_OF_YEAR);
  1734. if (cal.get(Calendar.WEEK_OF_YEAR) < 10) {
  1735. week = year + "0" + cal.get(Calendar.WEEK_OF_YEAR);
  1736. }
  1737. int weekInt = Integer.valueOf(week);
  1738. if (getWeekDayByDay2(date.substring(0, 4) + "0101") > 4) {
  1739. weekInt--;
  1740. }
  1741. if (weekInt % 100 == 0) {
  1742. year = year - 1;
  1743. cal.add(Calendar.DAY_OF_YEAR, -7);
  1744. week = year + "" + (cal.get(Calendar.WEEK_OF_YEAR) + 1);
  1745. weekInt = Integer.valueOf(week);
  1746. if (getWeekDayByDay2(date.substring(0, 4) + "0101") > 4) {
  1747. weekInt--;
  1748. }
  1749. }
  1750. return weekInt;
  1751. } catch (Exception e) {
  1752. e.printStackTrace();
  1753. }
  1754. return 0;
  1755. }
  1756. /**
  1757. * 两个时间段内的所有周
  1758. *
  1759. * @param startDate
  1760. * @param endDate
  1761. * @return
  1762. */
  1763. public static List<String> queryAllWeekInTwoDates(String startDate, String endDate) {
  1764. Calendar scal = Calendar.getInstance();
  1765. Calendar ecal = Calendar.getInstance();
  1766. Date sde = DateUtils.getDate(startDate, "yyyyMMdd");
  1767. Date ede = DateUtils.getDate(endDate, "yyyyMMdd");
  1768. scal.setTime(sde);
  1769. ecal.setTime(ede);
  1770. Map<String, Object> tempMap = new HashMap<String, Object>();
  1771. List<String> res = new ArrayList<String>();
  1772. while (scal.getTime().before(ecal.getTime())) {
  1773. scal.add(Calendar.DAY_OF_MONTH, 1);
  1774. int week = getWeekOfYear(DateUtils.changeDateToStr(scal.getTime(), "yyyyMMdd"));
  1775. if (!tempMap.containsKey(week + "")) {
  1776. tempMap.put(week + "", week);
  1777. res.add(week + "");
  1778. }
  1779. }
  1780. return res;
  1781. // List<String> list = new ArrayList<String>();
  1782. // try {
  1783. // if(StringUtils.isNotBlank(startDate)&&StringUtils.isNotBlank(endDate)){
  1784. // SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
  1785. // Calendar cal1 = Calendar.getInstance();
  1786. // cal1.setTime(df.parse(startDate));
  1787. // int syear = getWeekOfYear(cal1);
  1788. // cal1.setTime(df.parse(endDate));
  1789. // int eyear = getWeekOfYear(cal1);
  1790. //
  1791. // int sweek = getWeekOfYear(startDate);
  1792. // int eweek = getWeekOfYear(endDate);
  1793. // for (int i = syear; i <= eyear; i++) {
  1794. // int week = getWeekOfYear(i + "1231") % 100;
  1795. // int prevWeek = getWeekOfYear(i + "1224") % 100;
  1796. // if(week < prevWeek){
  1797. // week = prevWeek;
  1798. // }
  1799. // int nextWeek = getWeekOfYear(i + "0107") % 100;
  1800. // int start = 1;
  1801. // if(week > 50 && nextWeek == 2){
  1802. // start = 2;
  1803. // }
  1804. // for (int j = start; j <= week; j++) {
  1805. // int val = Integer.valueOf(i+""+j);
  1806. // if(j<10){
  1807. // val = Integer.valueOf(i+"0"+j);
  1808. // }
  1809. // if(val>=sweek&&val<=eweek){
  1810. // list.add(String.valueOf(val));
  1811. // }
  1812. // }
  1813. // }
  1814. // }
  1815. // } catch (Exception e) {
  1816. // e.printStackTrace();
  1817. // }
  1818. // return list;
  1819. }
  1820. /**
  1821. * 获取这个周是属于哪一年
  1822. *
  1823. * @param cl
  1824. * @return 返回年份 如:2012
  1825. */
  1826. public static int getWeekOfYear(Calendar cl) {
  1827. if (cl.get(Calendar.MONTH) == Calendar.JANUARY && cl.get(Calendar.WEEK_OF_YEAR) > 50) {
  1828. return cl.get(Calendar.YEAR) - 1;
  1829. } else {
  1830. if (cl.get(Calendar.MONTH) == Calendar.DECEMBER && cl.get(Calendar.WEEK_OF_YEAR) < 2) {
  1831. return cl.get(Calendar.YEAR) + 1;
  1832. } else {
  1833. return cl.get(Calendar.YEAR);
  1834. }
  1835. }
  1836. }
  1837. /**
  1838. * TODO需要测试 获得指定日期的去年同一周期的时间 如: 20110920000000 为
  1839. * 2011年38周星期二,则返回2010年38周星期二的日期
  1840. *
  1841. * @param dateStr
  1842. * yyyyMMddHHmmss
  1843. * @return
  1844. */
  1845. public static String getPrevYearWeekDate(String dateStr) {
  1846. Calendar calendar = Calendar.getInstance();
  1847. Date date = DateUtils.getDate(dateStr, "yyyyMMddHHmmss");
  1848. calendar.setTime(date);
  1849. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  1850. if (dayOfWeek == 1) {
  1851. dayOfWeek = 7;
  1852. } else {
  1853. dayOfWeek = dayOfWeek - 1;
  1854. }
  1855. int week = DateUtils.getWeekOfYear(dateStr.substring(0, 8));
  1856. week = week % 100;
  1857. calendar.add(Calendar.YEAR, -1);
  1858. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  1859. int prevWeek = DateUtils.getWeekOfYear(getDateFormat(calendar.getTime(), "yyyyMMdd"));// calendar.get(Calendar.WEEK_OF_YEAR);
  1860. prevWeek = prevWeek % 100;
  1861. int prevXq = calendar.get(Calendar.DAY_OF_WEEK);
  1862. if (prevXq == Calendar.SUNDAY) {
  1863. prevXq = 7;
  1864. } else {
  1865. prevXq = prevXq - 1;
  1866. }
  1867. // 如果是跨年,单独处理
  1868. if (Math.abs(prevWeek - week) > 10) {
  1869. if (prevWeek > week) {
  1870. prevWeek = week - 1;
  1871. } else {
  1872. prevWeek = week + 1;
  1873. }
  1874. }
  1875. int temp = (prevWeek - week) * 7 + (prevXq - dayOfWeek);
  1876. calendar.add(Calendar.DAY_OF_YEAR, -temp);
  1877. return DateUtils.getDateFormat(calendar.getTime(), "yyyyMMddHHmmss");
  1878. }
  1879. public static int getWeekDayByDay2(String day) {
  1880. Date d = changeStrToDate(day);
  1881. Calendar cal = Calendar.getInstance();
  1882. cal.setFirstDayOfWeek(Calendar.MONDAY);
  1883. cal.setTime(d);
  1884. int week = cal.get(Calendar.DAY_OF_WEEK);
  1885. switch (week) {
  1886. case 1:
  1887. return 7;
  1888. default:
  1889. return week - 1;
  1890. }
  1891. }
  1892. /**
  1893. * 根据指定日期,获得星期几的中文
  1894. *
  1895. * @param datestr
  1896. * yyyyMMdd格式
  1897. * @return
  1898. */
  1899. public static String getWeekDayString(String datestr) {
  1900. String weekString = "";
  1901. final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  1902. Calendar calendar = Calendar.getInstance();
  1903. Date date = DateUtils.getDate(datestr, "yyyyMMdd");
  1904. calendar.setTime(date);
  1905. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  1906. weekString = dayNames[dayOfWeek - 1];
  1907. return weekString;
  1908. }
  1909. /**
  1910. * 根据指定周和星期,获得对应的中文格式 如: 2011381 转为:2011年38周星期一 ,2011387 转为:2011年38周星期日
  1911. *
  1912. * @param datestr
  1913. * @return
  1914. */
  1915. public static String getWeekDayCnStr(String datestr) {
  1916. String weekString = datestr.substring(0, 4) + "年" + datestr.substring(4, 6) + "周";
  1917. final String dayNames[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
  1918. int dayOfWeek = Integer.parseInt(datestr.substring(6));
  1919. weekString += dayNames[dayOfWeek - 1];
  1920. return weekString;
  1921. }
  1922. /**
  1923. * 根据日期字符串获得是某年某周星期几,最后一位:星期一为1,星期日为7 如: 20110920 返回2011382
  1924. *
  1925. * @param datestr
  1926. * yyyyMMdd
  1927. * @return
  1928. */
  1929. public static String getYearWeekDay(String datestr) {
  1930. Calendar calendar = Calendar.getInstance();
  1931. Date date = DateUtils.getDate(datestr, "yyyyMMdd");
  1932. calendar.setTime(date);
  1933. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  1934. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
  1935. if (dayOfWeek == 1) {
  1936. dayOfWeek = 7;
  1937. } else {
  1938. dayOfWeek = dayOfWeek - 1;
  1939. }
  1940. int week = DateUtils.getWeekOfYear(datestr);
  1941. // week = week - 1;
  1942. return String.valueOf(week + "" + dayOfWeek);
  1943. }
  1944. /**
  1945. * 获得指定时间的年和周 如:20110920 返回2011年38周
  1946. *
  1947. * @param date
  1948. * @return
  1949. */
  1950. public static String getYearWeekStr(Date date) {
  1951. Calendar calendar = Calendar.getInstance();
  1952. calendar.setTime(date);
  1953. int week = DateUtils.getWeekOfYear(DateUtils.getDateStr(date, "yyyyMMdd"));
  1954. // week = week - 1;
  1955. String yy = String.valueOf(week / 100);
  1956. String ww = String.valueOf(week % 100);
  1957. int w = week % 100;
  1958. if (w < 10) {
  1959. ww = "0" + w;
  1960. }
  1961. return yy + "年" + ww + "周";
  1962. }
  1963. /**
  1964. * 计算输入日期是星期几
  1965. *
  1966. * @param day
  1967. * @return 返回 星期一
  1968. */
  1969. public static String getWeekDayByDay(String day) {
  1970. if (day == null)
  1971. return "";
  1972. Date d = changeStrToDate(day);
  1973. Calendar cal = Calendar.getInstance();
  1974. cal.setFirstDayOfWeek(Calendar.MONDAY);
  1975. cal.setTime(d);
  1976. int week = cal.get(Calendar.DAY_OF_WEEK);
  1977. switch (week) {
  1978. case 1:
  1979. return "星期日";
  1980. case 2:
  1981. return "星期一";
  1982. case 3:
  1983. return "星期二";
  1984. case 4:
  1985. return "星期三";
  1986. case 5:
  1987. return "星期四";
  1988. case 6:
  1989. return "星期五";
  1990. case 7:
  1991. return "星期六";
  1992. default:
  1993. return "";
  1994. }
  1995. }
  1996. public static List<String> getHoursBetweenTwoDayByMins(String sday, String eday, int mins) {
  1997. Calendar scal = Calendar.getInstance();
  1998. Calendar ecal = Calendar.getInstance();
  1999. Date sde = DateUtils.getDate(sday, "yyyyMMddHHmmss");
  2000. Date ede = DateUtils.getDate(eday, "yyyyMMddHHmmss");
  2001. scal.setTime(sde);
  2002. ecal.setTime(ede);
  2003. List<Date> dlist = new ArrayList<Date>();
  2004. List<String> resList = new ArrayList<String>();
  2005. dlist.add(scal.getTime());
  2006. while (scal.getTime().before(ecal.getTime())) {
  2007. scal.add(Calendar.MINUTE, mins);
  2008. dlist.add(scal.getTime());
  2009. }
  2010. for (int i = 0; i < dlist.size(); i++) {
  2011. Date d = dlist.get(i);
  2012. String hour = DateUtils.changeDateToStr(d, "yyyyMMddHH");
  2013. resList.add(hour);
  2014. }
  2015. return resList;
  2016. }
  2017. /**
  2018. * 获取当月第一天日期
  2019. *
  2020. * @return
  2021. */
  2022. public static Date getFirstDateOfMonth() {
  2023. Calendar cal = Calendar.getInstance();
  2024. cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
  2025. Date date = cal.getTime();
  2026. return date;
  2027. }
  2028. /**
  2029. * 获取输入日期所在周的第一天和最后一天
  2030. *
  2031. * @param date
  2032. * @return
  2033. */
  2034. public static String[] getFirstAndEndDayOfWeekByDate(String date) {
  2035. String[] twoDays = new String[2];
  2036. Calendar cal = Calendar.getInstance();
  2037. Date d = DateUtils.changeStrToDate(date);
  2038. cal.setTime(d);
  2039. int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
  2040. cal.add(Calendar.DATE, -day_of_week);
  2041. twoDays[0] = DateUtils.getDateFormat(cal.getTime(), "yyyyMMdd");
  2042. cal.add(Calendar.DATE, 6);
  2043. twoDays[1] = DateUtils.getDateFormat(cal.getTime(), "yyyyMMdd");
  2044. return twoDays;
  2045. }
  2046. /**
  2047. * 获取输入日期所在周的第一天和最后一天
  2048. *
  2049. * @param date
  2050. * @return
  2051. */
  2052. public static String getFirstDayOfWeekByDate(String date) {
  2053. Calendar cal = Calendar.getInstance();
  2054. Date d = DateUtils.changeStrToDate(date);
  2055. cal.setTime(d);
  2056. int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
  2057. if (day_of_week == -1) {
  2058. day_of_week = 6;
  2059. }
  2060. cal.add(Calendar.DATE, -day_of_week);
  2061. return DateUtils.getDateFormat(cal.getTime(), "yyyyMMdd");
  2062. }
  2063. /**
  2064. * 今天算起前几周是第几周
  2065. *
  2066. * @param amount
  2067. * @return
  2068. */
  2069. public static String getWeekBeforeWeek(int amount) {
  2070. int days = 7 * amount;
  2071. String day = DateUtils.add(DateUtils.getCurrentDate(), DateUtils.DB_STORE_DATE, days, Calendar.DAY_OF_MONTH);
  2072. String week = DateUtils.getWeekOfYear(day) + "";
  2073. return week;
  2074. }
  2075. /**
  2076. * 指定时间算起前几周是第几周
  2077. *
  2078. * @param amount
  2079. * @return
  2080. */
  2081. public static String getWeekBeforeDate(Date date, int amount) {
  2082. int days = 7 * amount;
  2083. String day = DateUtils.add(getDateStr(date, DB_STORE_DATE_FMT), DateUtils.DB_STORE_DATE, days, Calendar.DAY_OF_MONTH);
  2084. String week = DateUtils.getWeekOfYear(day) + "";
  2085. return week;
  2086. }
  2087. /**
  2088. * 两个时间段内的所有小时
  2089. *
  2090. * @param sday
  2091. * 格式yyyyMMddhhmmss
  2092. * @param eday
  2093. * 格式yyyyMMddhhmmss
  2094. * @return
  2095. */
  2096. public static List<String> getHoursBetweenTwoDay(String sday, String eday) {
  2097. Calendar scal = Calendar.getInstance();
  2098. Calendar ecal = Calendar.getInstance();
  2099. Date sde = DateUtils.getDate(sday, "yyyyMMddHHmmss");
  2100. Date ede = DateUtils.getDate(eday, "yyyyMMddHHmmss");
  2101. scal.setTime(sde);
  2102. ecal.setTime(ede);
  2103. List<Date> dlist = new ArrayList<Date>();
  2104. List<String> resList = new ArrayList<String>();
  2105. dlist.add(scal.getTime());
  2106. while (scal.getTime().before(ecal.getTime())) {
  2107. scal.add(Calendar.HOUR_OF_DAY, 1);
  2108. dlist.add(scal.getTime());
  2109. }
  2110. for (int i = 0; i < dlist.size(); i++) {
  2111. Date d = dlist.get(i);
  2112. String hour = DateUtils.changeDateToStr(d, "yyyyMMddHH");
  2113. resList.add(hour);
  2114. }
  2115. return resList;
  2116. }
  2117. /**
  2118. * 获取0-23点的小时点。
  2119. *
  2120. * @return
  2121. */
  2122. public static List<String> getHours(String sday, String eday) {
  2123. List<String> lst = getHoursBetweenTwoDay(sday, eday);
  2124. List<String> res = new ArrayList<String>();
  2125. if (lst.size() <= 24) {
  2126. for (int i = 0; i < lst.size(); i++) {
  2127. res.add(lst.get(i).substring(8, 10));
  2128. }
  2129. } else {
  2130. for (int i = 0; i <= 23; i++) {
  2131. res.add(lst.get(i).substring(8, 10));
  2132. }
  2133. }
  2134. return res;
  2135. }
  2136. /**
  2137. * 获取上一周的今天
  2138. *
  2139. * @return
  2140. */
  2141. public static String getPerWeekDateStr() {
  2142. Calendar curr = Calendar.getInstance();
  2143. curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) - 7);
  2144. Date perDate = curr.getTime();
  2145. return getDateStr(perDate, DB_STORE_DATE);
  2146. }
  2147. /**
  2148. * 取得当前日期是多少周
  2149. *
  2150. * @param date
  2151. * @return
  2152. */
  2153. public static int getWeekOfYear(Date date) {
  2154. Calendar c = new GregorianCalendar();
  2155. c.setFirstDayOfWeek(Calendar.MONDAY);
  2156. c.setMinimalDaysInFirstWeek(7);
  2157. c.setTime(date);
  2158. return c.get(Calendar.WEEK_OF_YEAR);
  2159. }
  2160. /**
  2161. * 取得当前日期是第几天
  2162. *
  2163. * @param date
  2164. * @return
  2165. */
  2166. public static int getDayOfYear() {
  2167. TimeZone zone = TimeZone.getTimeZone("Asia/Shanghai");
  2168. Calendar cal = Calendar.getInstance(zone);
  2169. int daynum = cal.get(cal.DAY_OF_YEAR);
  2170. return daynum;
  2171. }
  2172. /**
  2173. * 得到某一年周的总数
  2174. *
  2175. * @param year
  2176. * @return
  2177. */
  2178. public static int getMaxWeekNumOfYear(int year) {
  2179. Calendar c = new GregorianCalendar();
  2180. c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
  2181. return getWeekOfYear(c.getTime());
  2182. }
  2183. /**
  2184. * 得到某年某周的第一天
  2185. *
  2186. * @param year
  2187. * @param week
  2188. * @return
  2189. */
  2190. public static String getFirstDayOfWeek(int year, int week) {
  2191. Calendar c = new GregorianCalendar();
  2192. c.set(Calendar.YEAR, year);
  2193. c.set(Calendar.MONTH, Calendar.JANUARY);
  2194. c.set(Calendar.DATE, 1);
  2195. Calendar cal = (GregorianCalendar) c.clone();
  2196. cal.add(Calendar.DATE, week * 7);
  2197. return getFirstDayOfWeek(cal.getTime());
  2198. }
  2199. /**
  2200. * 得到某年某周的最后一天
  2201. *
  2202. * @param year
  2203. * @param week
  2204. * @return
  2205. */
  2206. public static String getLastDayOfWeek(int year, int week) {
  2207. Calendar c = new GregorianCalendar();
  2208. c.set(Calendar.YEAR, year);
  2209. c.set(Calendar.MONTH, Calendar.JANUARY);
  2210. c.set(Calendar.DATE, 1);
  2211. Calendar cal = (GregorianCalendar) c.clone();
  2212. cal.add(Calendar.DATE, week * 7);
  2213. return getLastDayOfWeek(cal.getTime());
  2214. }
  2215. /**
  2216. * 取得指定日期所在周的第一天
  2217. *
  2218. * @param date
  2219. * @return
  2220. */
  2221. public static String getFirstDayOfWeek(Date date) {
  2222. Calendar c = new GregorianCalendar();
  2223. c.setFirstDayOfWeek(Calendar.MONDAY);
  2224. c.setTime(date);
  2225. c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
  2226. return DateUtils.changeDateToStr(c.getTime(), DateUtils.DB_STORE_DATE_FULL);
  2227. }
  2228. /**
  2229. * 取得指定日期所在周的最后一天
  2230. *
  2231. * @param date
  2232. * @return
  2233. */
  2234. public static String getLastDayOfWeek(Date date) {
  2235. Calendar c = new GregorianCalendar();
  2236. c.setFirstDayOfWeek(Calendar.MONDAY);
  2237. c.setTime(date);
  2238. c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
  2239. return DateUtils.changeDateToStr(c.getTime(), DateUtils.DB_STORE_DATE_FULL);
  2240. }
  2241. /**
  2242. * 取得当前日期所在周的第一天
  2243. *
  2244. * @param date
  2245. * @return
  2246. */
  2247. public static String getFirstDayOfWeek() {
  2248. Calendar c = new GregorianCalendar();
  2249. c.setFirstDayOfWeek(Calendar.MONDAY);
  2250. c.setTime(new Date());
  2251. c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
  2252. return DateUtils.changeDateToStr(c.getTime(), DateUtils.DB_STORE_DATE_FULL);
  2253. }
  2254. /**
  2255. * 取得当前日期所在周的最后一天
  2256. *
  2257. * @param date
  2258. * @return
  2259. */
  2260. public static String getLastDayOfWeek() {
  2261. Calendar c = new GregorianCalendar();
  2262. c.setFirstDayOfWeek(Calendar.MONDAY);
  2263. c.setTime(new Date());
  2264. c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
  2265. return DateUtils.changeDateToStr(c.getTime(), DateUtils.DB_STORE_DATE_FULL);
  2266. }
  2267. /**
  2268. * 获取两日期时间差
  2269. *
  2270. * @param sday
  2271. * @param eday
  2272. * @return
  2273. */
  2274. public static Long getDiffBetweenTwoDay(String sday, String eday) {
  2275. Date sde = null;
  2276. Date ede = null;
  2277. try {
  2278. sde = DateUtils.getDate(sday, "yyyyMMddHHmmss");
  2279. ede = DateUtils.getDate(eday, "yyyyMMddHHmmss");
  2280. } catch (Exception e) {
  2281. e.printStackTrace();
  2282. }
  2283. // 获得两个时间的毫秒时间差异
  2284. long diff = ede.getTime() - sde.getTime();
  2285. return diff;
  2286. }
  2287. /**
  2288. * 获取当前年份
  2289. *
  2290. * @return 如2014
  2291. */
  2292. public static String getNowYear() {
  2293. Calendar ca = Calendar.getInstance();
  2294. ca.setTime(new java.util.Date());
  2295. String year = "" + ca.get(Calendar.YEAR);
  2296. return year;
  2297. }
  2298. /**
  2299. * 获取当前月份
  2300. *
  2301. * @return 如2014
  2302. */
  2303. public static String getNowMonth() {
  2304. Calendar ca = Calendar.getInstance();
  2305. ca.setTime(new java.util.Date());
  2306. String month = ca.get(Calendar.MONTH) + 1 + "";
  2307. return month;
  2308. }
  2309. /**
  2310. * 获取当前日期
  2311. *
  2312. * @return 如2014
  2313. */
  2314. public static String getNowDay() {
  2315. Calendar ca = Calendar.getInstance();
  2316. ca.setTime(new java.util.Date());
  2317. String year = "" + ca.get(Calendar.DAY_OF_MONTH);
  2318. return year;
  2319. }
  2320. public static boolean isLeapYear(String year) {
  2321. int yearInt = Integer.parseInt(year);
  2322. if (yearInt % 400 == 0 || (yearInt % 100 != 0) && (yearInt % 4 == 0)) {
  2323. return true;
  2324. }
  2325. return false;
  2326. }
  2327. /**
  2328. * 根据年龄推算出生日期
  2329. *
  2330. * @param age
  2331. * 年龄
  2332. * @return 出生日期
  2333. * @author
  2334. */
  2335. public static String Age2Birth(String age) {
  2336. // 获取当前日期
  2337. Calendar currCalendar = Calendar.getInstance();
  2338. // 获取当前年份
  2339. int currYear = currCalendar.get(Calendar.YEAR);
  2340. // 获取当前月份
  2341. int currMonth = currCalendar.get(Calendar.MONTH) + 1;
  2342. // 获取当日
  2343. int currDay = currCalendar.get(Calendar.DAY_OF_MONTH);
  2344. // 计算出生年份
  2345. int birthYear = currYear - Integer.parseInt(age);
  2346. // 返回出生日期
  2347. String realMonth = String.valueOf(currMonth).length() == 1 ? "0" + String.valueOf(currMonth) : String.valueOf(currMonth);
  2348. return String.valueOf(birthYear) + realMonth + String.valueOf(currDay);
  2349. }
  2350. public static void main(String[] args) throws ParseException {
  2351. // System.out.println(DateUtils.getAge("19500614"));
  2352. /*
  2353. * System.out.println(getWeekOfYear(new Date()));
  2354. * System.out.println(getDayOfYear());
  2355. * System.out.println(getWeekOfYear(new Date())); System.out.println(new
  2356. * Date().getMonth()+1); System.out.println(getMaxWeekNumOfYear(2016));
  2357. * System.out.println(getDayOfYear());
  2358. */
  2359. System.out.println(birthdayToAge("20151001"));
  2360. System.out.println(birthdayToAge("20151101"));
  2361. System.out.println(isLeapYear("2004"));
  2362. }
  2363. /**
  2364. * 如果是早上,在0-6点返回0点,6-12点返回6点,12-18点返回12点,18-24点返回18点
  2365. */
  2366. public static String getSearchTime() {
  2367. String dateStr = DateUtils.getCurrentTime();
  2368. String hour = dateStr.substring(8, 10);
  2369. int ihour = Integer.parseInt(hour);
  2370. if (0 < ihour && ihour <= 6) {
  2371. return DateUtils.getCurrentDate() + "000000";
  2372. }
  2373. if (6 < ihour && ihour <= 12) {
  2374. return DateUtils.getCurrentDate() + "060000";
  2375. }
  2376. if (12 < ihour && ihour <= 18) {
  2377. return DateUtils.getCurrentDate() + "120000";
  2378. }
  2379. if (18 < ihour && ihour <= 24) {
  2380. return DateUtils.getCurrentDate() + "180000";
  2381. }
  2382. return DateUtils.getCurrentDate() + "000000";
  2383. }
  2384. /**
  2385. * 获取两个时间相差的分钟
  2386. *
  2387. * @param stime
  2388. * @param etime
  2389. * @return
  2390. */
  2391. public static long getBetweenMinute(String stime, String etime) {
  2392. Date kssj = DateUtils.getDate(stime, DateUtils.DB_STORE_DATE_FULL);
  2393. Date jssj = DateUtils.getDate(etime, DateUtils.DB_STORE_DATE_FULL);
  2394. Calendar instance = Calendar.getInstance();
  2395. instance.setTime(kssj);
  2396. Calendar instance1 = Calendar.getInstance();
  2397. instance1.setTime(jssj);
  2398. long xgsj = instance1.getTimeInMillis() - instance.getTimeInMillis();
  2399. return xgsj / (1000 * 60);
  2400. }
  2401. public static boolean isBeforeTime(String stime, String etime) {
  2402. if (StringUtils.isBlank(stime) || StringUtils.isBlank(etime)) {
  2403. return true;
  2404. }
  2405. Date kssj = DateUtils.getDate(stime, DateUtils.DB_STORE_DATE_FULL);
  2406. Date jssj = DateUtils.getDate(etime, DateUtils.DB_STORE_DATE_FULL);
  2407. return kssj.before(jssj);
  2408. }
  2409. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值