55260757dbc9f383b9aa3e6bcabdb62fa3e101b8.svn-base 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. package org.jeecg.common.util;
  2. import java.beans.PropertyEditorSupport;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.GregorianCalendar;
  10. import org.springframework.util.StringUtils;
  11. /**
  12. * 类描述:时间操作定义类
  13. *
  14. * @Author: 张代浩
  15. * @Date:2012-12-8 12:15:03
  16. * @Version 1.0
  17. */
  18. public class DateUtils extends PropertyEditorSupport {
  19. public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
  20. @Override
  21. protected SimpleDateFormat initialValue() {
  22. return new SimpleDateFormat("yyyy-MM-dd");
  23. }
  24. };
  25. public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
  26. @Override
  27. protected SimpleDateFormat initialValue() {
  28. return new SimpleDateFormat("yyyyMMdd");
  29. }
  30. };
  31. public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() {
  32. @Override
  33. protected SimpleDateFormat initialValue() {
  34. return new SimpleDateFormat("yyyy年MM月dd日");
  35. }
  36. };
  37. public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() {
  38. @Override
  39. protected SimpleDateFormat initialValue() {
  40. return new SimpleDateFormat("yyyy-MM-dd HH:mm");
  41. }
  42. };
  43. public static ThreadLocal<SimpleDateFormat> yyyymmddhhmmss = new ThreadLocal<SimpleDateFormat>() {
  44. @Override
  45. protected SimpleDateFormat initialValue() {
  46. return new SimpleDateFormat("yyyyMMddHHmmss");
  47. }
  48. };
  49. public static ThreadLocal<SimpleDateFormat> short_time_sdf = new ThreadLocal<SimpleDateFormat>() {
  50. @Override
  51. protected SimpleDateFormat initialValue() {
  52. return new SimpleDateFormat("HH:mm");
  53. }
  54. };
  55. public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() {
  56. @Override
  57. protected SimpleDateFormat initialValue() {
  58. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  59. }
  60. };
  61. // 以毫秒表示的时间
  62. private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
  63. private static final long HOUR_IN_MILLIS = 3600 * 1000;
  64. private static final long MINUTE_IN_MILLIS = 60 * 1000;
  65. private static final long SECOND_IN_MILLIS = 1000;
  66. // 指定模式的时间格式
  67. private static SimpleDateFormat getSDFormat(String pattern) {
  68. return new SimpleDateFormat(pattern);
  69. }
  70. /**
  71. * 当前日历,这里用中国时间表示
  72. *
  73. * @return 以当地时区表示的系统当前日历
  74. */
  75. public static Calendar getCalendar() {
  76. return Calendar.getInstance();
  77. }
  78. /**
  79. * 指定毫秒数表示的日历
  80. *
  81. * @param millis 毫秒数
  82. * @return 指定毫秒数表示的日历
  83. */
  84. public static Calendar getCalendar(long millis) {
  85. Calendar cal = Calendar.getInstance();
  86. // --------------------cal.setTimeInMillis(millis);
  87. cal.setTime(new Date(millis));
  88. return cal;
  89. }
  90. // ////////////////////////////////////////////////////////////////////////////
  91. // getDate
  92. // 各种方式获取的Date
  93. // ////////////////////////////////////////////////////////////////////////////
  94. /**
  95. * 当前日期
  96. *
  97. * @return 系统当前时间
  98. */
  99. public static Date getDate() {
  100. return new Date();
  101. }
  102. /**
  103. * 指定毫秒数表示的日期
  104. *
  105. * @param millis 毫秒数
  106. * @return 指定毫秒数表示的日期
  107. */
  108. public static Date getDate(long millis) {
  109. return new Date(millis);
  110. }
  111. /**
  112. * 时间戳转换为字符串
  113. *
  114. * @param time
  115. * @return
  116. */
  117. public static String timestamptoStr(Timestamp time) {
  118. Date date = null;
  119. if (null != time) {
  120. date = new Date(time.getTime());
  121. }
  122. return date2Str(date_sdf.get());
  123. }
  124. /**
  125. * 字符串转换时间戳
  126. *
  127. * @param str
  128. * @return
  129. */
  130. public static Timestamp str2Timestamp(String str) {
  131. Date date = str2Date(str, date_sdf.get());
  132. return new Timestamp(date.getTime());
  133. }
  134. /**
  135. * 字符串转换成日期
  136. *
  137. * @param str
  138. * @param sdf
  139. * @return
  140. */
  141. public static Date str2Date(String str, SimpleDateFormat sdf) {
  142. if (null == str || "".equals(str)) {
  143. return null;
  144. }
  145. Date date = null;
  146. try {
  147. date = sdf.parse(str);
  148. return date;
  149. } catch (ParseException e) {
  150. e.printStackTrace();
  151. }
  152. return null;
  153. }
  154. /**
  155. * 日期转换为字符串
  156. *
  157. * @param date_sdf 日期格式
  158. * @return 字符串
  159. */
  160. public static String date2Str(SimpleDateFormat date_sdf) {
  161. Date date = getDate();
  162. if (null == date) {
  163. return null;
  164. }
  165. return date_sdf.format(date);
  166. }
  167. /**
  168. * 格式化时间
  169. *
  170. * @param date
  171. * @param format
  172. * @return
  173. */
  174. public static String dateformat(String date, String format) {
  175. SimpleDateFormat sformat = new SimpleDateFormat(format);
  176. Date _date = null;
  177. try {
  178. _date = sformat.parse(date);
  179. } catch (ParseException e) {
  180. // TODO Auto-generated catch block
  181. e.printStackTrace();
  182. }
  183. return sformat.format(_date);
  184. }
  185. /**
  186. * 日期转换为字符串
  187. *
  188. * @param date 日期
  189. * @param date_sdf 日期格式
  190. * @return 字符串
  191. */
  192. public static String date2Str(Date date, SimpleDateFormat date_sdf) {
  193. if (null == date) {
  194. return null;
  195. }
  196. return date_sdf.format(date);
  197. }
  198. /**
  199. * 日期转换为字符串
  200. *
  201. * @param format 日期格式
  202. * @return 字符串
  203. */
  204. public static String getDate(String format) {
  205. Date date = new Date();
  206. if (null == date) {
  207. return null;
  208. }
  209. SimpleDateFormat sdf = new SimpleDateFormat(format);
  210. return sdf.format(date);
  211. }
  212. /**
  213. * 指定毫秒数的时间戳
  214. *
  215. * @param millis 毫秒数
  216. * @return 指定毫秒数的时间戳
  217. */
  218. public static Timestamp getTimestamp(long millis) {
  219. return new Timestamp(millis);
  220. }
  221. /**
  222. * 以字符形式表示的时间戳
  223. *
  224. * @param time 毫秒数
  225. * @return 以字符形式表示的时间戳
  226. */
  227. public static Timestamp getTimestamp(String time) {
  228. return new Timestamp(Long.parseLong(time));
  229. }
  230. /**
  231. * 系统当前的时间戳
  232. *
  233. * @return 系统当前的时间戳
  234. */
  235. public static Timestamp getTimestamp() {
  236. return new Timestamp(System.currentTimeMillis());
  237. }
  238. /**
  239. * 当前时间,格式 yyyy-MM-dd HH:mm:ss
  240. *
  241. * @return 当前时间的标准形式字符串
  242. */
  243. public static String now() {
  244. return datetimeFormat.get().format(getCalendar().getTime());
  245. }
  246. /**
  247. * 指定日期的时间戳
  248. *
  249. * @param date 指定日期
  250. * @return 指定日期的时间戳
  251. */
  252. public static Timestamp getTimestamp(Date date) {
  253. return new Timestamp(date.getTime());
  254. }
  255. /**
  256. * 指定日历的时间戳
  257. *
  258. * @param cal 指定日历
  259. * @return 指定日历的时间戳
  260. */
  261. public static Timestamp getCalendarTimestamp(Calendar cal) {
  262. // ---------------------return new Timestamp(cal.getTimeInMillis());
  263. return new Timestamp(cal.getTime().getTime());
  264. }
  265. public static Timestamp gettimestamp() {
  266. Date dt = new Date();
  267. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  268. String nowTime = df.format(dt);
  269. java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
  270. return buydate;
  271. }
  272. // ////////////////////////////////////////////////////////////////////////////
  273. // getMillis
  274. // 各种方式获取的Millis
  275. // ////////////////////////////////////////////////////////////////////////////
  276. /**
  277. * 系统时间的毫秒数
  278. *
  279. * @return 系统时间的毫秒数
  280. */
  281. public static long getMillis() {
  282. return System.currentTimeMillis();
  283. }
  284. /**
  285. * 指定日历的毫秒数
  286. *
  287. * @param cal 指定日历
  288. * @return 指定日历的毫秒数
  289. */
  290. public static long getMillis(Calendar cal) {
  291. // --------------------return cal.getTimeInMillis();
  292. return cal.getTime().getTime();
  293. }
  294. /**
  295. * 指定日期的毫秒数
  296. *
  297. * @param date 指定日期
  298. * @return 指定日期的毫秒数
  299. */
  300. public static long getMillis(Date date) {
  301. return date.getTime();
  302. }
  303. /**
  304. * 指定时间戳的毫秒数
  305. *
  306. * @param ts 指定时间戳
  307. * @return 指定时间戳的毫秒数
  308. */
  309. public static long getMillis(Timestamp ts) {
  310. return ts.getTime();
  311. }
  312. // ////////////////////////////////////////////////////////////////////////////
  313. // formatDate
  314. // 将日期按照一定的格式转化为字符串
  315. // ////////////////////////////////////////////////////////////////////////////
  316. /**
  317. * 默认方式表示的系统当前日期,具体格式:年-月-日
  318. *
  319. * @return 默认日期按“年-月-日“格式显示
  320. */
  321. public static String formatDate() {
  322. return date_sdf.get().format(getCalendar().getTime());
  323. }
  324. /**
  325. * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
  326. *
  327. * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
  328. */
  329. public static String formatDateTime() {
  330. return datetimeFormat.get().format(getCalendar().getTime());
  331. }
  332. /**
  333. * 获取时间字符串
  334. */
  335. public static String getDataString(SimpleDateFormat formatstr) {
  336. return formatstr.format(getCalendar().getTime());
  337. }
  338. /**
  339. * 指定日期的默认显示,具体格式:年-月-日
  340. *
  341. * @param cal 指定的日期
  342. * @return 指定日期按“年-月-日“格式显示
  343. */
  344. public static String formatDate(Calendar cal) {
  345. return date_sdf.get().format(cal.getTime());
  346. }
  347. /**
  348. * 指定日期的默认显示,具体格式:年-月-日
  349. *
  350. * @param date 指定的日期
  351. * @return 指定日期按“年-月-日“格式显示
  352. */
  353. public static String formatDate(Date date) {
  354. return date_sdf.get().format(date);
  355. }
  356. /**
  357. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
  358. *
  359. * @param millis 指定的毫秒数
  360. * @return 指定毫秒数表示日期按“年-月-日“格式显示
  361. */
  362. public static String formatDate(long millis) {
  363. return date_sdf.get().format(new Date(millis));
  364. }
  365. /**
  366. * 默认日期按指定格式显示
  367. *
  368. * @param pattern 指定的格式
  369. * @return 默认日期按指定格式显示
  370. */
  371. public static String formatDate(String pattern) {
  372. return getSDFormat(pattern).format(getCalendar().getTime());
  373. }
  374. /**
  375. * 指定日期按指定格式显示
  376. *
  377. * @param cal 指定的日期
  378. * @param pattern 指定的格式
  379. * @return 指定日期按指定格式显示
  380. */
  381. public static String formatDate(Calendar cal, String pattern) {
  382. return getSDFormat(pattern).format(cal.getTime());
  383. }
  384. /**
  385. * 指定日期按指定格式显示
  386. *
  387. * @param date 指定的日期
  388. * @param pattern 指定的格式
  389. * @return 指定日期按指定格式显示
  390. */
  391. public static String formatDate(Date date, String pattern) {
  392. return getSDFormat(pattern).format(date);
  393. }
  394. // ////////////////////////////////////////////////////////////////////////////
  395. // formatTime
  396. // 将日期按照一定的格式转化为字符串
  397. // ////////////////////////////////////////////////////////////////////////////
  398. /**
  399. * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
  400. *
  401. * @return 默认日期按“年-月-日 时:分“格式显示
  402. */
  403. public static String formatTime() {
  404. return time_sdf.get().format(getCalendar().getTime());
  405. }
  406. /**
  407. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
  408. *
  409. * @param millis 指定的毫秒数
  410. * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
  411. */
  412. public static String formatTime(long millis) {
  413. return time_sdf.get().format(new Date(millis));
  414. }
  415. /**
  416. * 指定日期的默认显示,具体格式:年-月-日 时:分
  417. *
  418. * @param cal 指定的日期
  419. * @return 指定日期按“年-月-日 时:分“格式显示
  420. */
  421. public static String formatTime(Calendar cal) {
  422. return time_sdf.get().format(cal.getTime());
  423. }
  424. /**
  425. * 指定日期的默认显示,具体格式:年-月-日 时:分
  426. *
  427. * @param date 指定的日期
  428. * @return 指定日期按“年-月-日 时:分“格式显示
  429. */
  430. public static String formatTime(Date date) {
  431. return time_sdf.get().format(date);
  432. }
  433. // ////////////////////////////////////////////////////////////////////////////
  434. // formatShortTime
  435. // 将日期按照一定的格式转化为字符串
  436. // ////////////////////////////////////////////////////////////////////////////
  437. /**
  438. * 默认方式表示的系统当前日期,具体格式:时:分
  439. *
  440. * @return 默认日期按“时:分“格式显示
  441. */
  442. public static String formatShortTime() {
  443. return short_time_sdf.get().format(getCalendar().getTime());
  444. }
  445. /**
  446. * 指定毫秒数表示日期的默认显示,具体格式:时:分
  447. *
  448. * @param millis 指定的毫秒数
  449. * @return 指定毫秒数表示日期按“时:分“格式显示
  450. */
  451. public static String formatShortTime(long millis) {
  452. return short_time_sdf.get().format(new Date(millis));
  453. }
  454. /**
  455. * 指定日期的默认显示,具体格式:时:分
  456. *
  457. * @param cal 指定的日期
  458. * @return 指定日期按“时:分“格式显示
  459. */
  460. public static String formatShortTime(Calendar cal) {
  461. return short_time_sdf.get().format(cal.getTime());
  462. }
  463. /**
  464. * 指定日期的默认显示,具体格式:时:分
  465. *
  466. * @param date 指定的日期
  467. * @return 指定日期按“时:分“格式显示
  468. */
  469. public static String formatShortTime(Date date) {
  470. return short_time_sdf.get().format(date);
  471. }
  472. // ////////////////////////////////////////////////////////////////////////////
  473. // parseDate
  474. // parseCalendar
  475. // parseTimestamp
  476. // 将字符串按照一定的格式转化为日期或时间
  477. // ////////////////////////////////////////////////////////////////////////////
  478. /**
  479. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  480. *
  481. * @param src 将要转换的原始字符窜
  482. * @param pattern 转换的匹配格式
  483. * @return 如果转换成功则返回转换后的日期
  484. * @throws ParseException
  485. */
  486. public static Date parseDate(String src, String pattern) throws ParseException {
  487. return getSDFormat(pattern).parse(src);
  488. }
  489. /**
  490. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  491. *
  492. * @param src 将要转换的原始字符窜
  493. * @param pattern 转换的匹配格式
  494. * @return 如果转换成功则返回转换后的日期
  495. * @throws ParseException
  496. */
  497. public static Calendar parseCalendar(String src, String pattern) throws ParseException {
  498. Date date = parseDate(src, pattern);
  499. Calendar cal = Calendar.getInstance();
  500. cal.setTime(date);
  501. return cal;
  502. }
  503. public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
  504. Calendar cal;
  505. cal = parseCalendar(src, pattern);
  506. cal.add(Calendar.DATE, amount);
  507. return formatDate(cal);
  508. }
  509. /**
  510. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  511. *
  512. * @param src 将要转换的原始字符窜
  513. * @param pattern 转换的匹配格式
  514. * @return 如果转换成功则返回转换后的时间戳
  515. * @throws ParseException
  516. */
  517. public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
  518. Date date = parseDate(src, pattern);
  519. return new Timestamp(date.getTime());
  520. }
  521. // ////////////////////////////////////////////////////////////////////////////
  522. // dateDiff
  523. // 计算两个日期之间的差值
  524. // ////////////////////////////////////////////////////////////////////////////
  525. /**
  526. * 计算两个时间之间的差值,根据标志的不同而不同
  527. *
  528. * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
  529. * @param calSrc 减数
  530. * @param calDes 被减数
  531. * @return 两个日期之间的差值
  532. */
  533. public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
  534. long millisDiff = getMillis(calSrc) - getMillis(calDes);
  535. if (flag == 'y') {
  536. return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR));
  537. }
  538. if (flag == 'd') {
  539. return (int) (millisDiff / DAY_IN_MILLIS);
  540. }
  541. if (flag == 'h') {
  542. return (int) (millisDiff / HOUR_IN_MILLIS);
  543. }
  544. if (flag == 'm') {
  545. return (int) (millisDiff / MINUTE_IN_MILLIS);
  546. }
  547. if (flag == 's') {
  548. return (int) (millisDiff / SECOND_IN_MILLIS);
  549. }
  550. return 0;
  551. }
  552. public static Long getCurrentTimestamp() {
  553. return Long.valueOf(DateUtils.yyyymmddhhmmss.get().format(new Date()));
  554. }
  555. /**
  556. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  557. * HH:mm:ss“ * @param text String类型的时间值
  558. */
  559. @Override
  560. public void setAsText(String text) throws IllegalArgumentException {
  561. if (StringUtils.hasText(text)) {
  562. try {
  563. if (text.indexOf(":") == -1 && text.length() == 10) {
  564. setValue(DateUtils.date_sdf.get().parse(text));
  565. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  566. setValue(DateUtils.datetimeFormat.get().parse(text));
  567. } else {
  568. throw new IllegalArgumentException("Could not parse date, date format is error ");
  569. }
  570. } catch (ParseException ex) {
  571. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  572. iae.initCause(ex);
  573. throw iae;
  574. }
  575. } else {
  576. setValue(null);
  577. }
  578. }
  579. public static int getYear() {
  580. GregorianCalendar calendar = new GregorianCalendar();
  581. calendar.setTime(getDate());
  582. return calendar.get(Calendar.YEAR);
  583. }
  584. }