DateUtils.java 18 KB

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