DateUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package com.hcks.cmfds.commons.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. *
  8. *
  9. * 项目名称:nxfd 类名称:DateUtils 类描述: 创建人:石林 创建时间:2015-3-30 上午11:42:54 修改人:shilinno1
  10. * 修改时间:2015-3-30 上午11:42:54 修改备注:
  11. *
  12. * @version
  13. *
  14. */
  15. public class DateUtils {
  16. private static final String format = "yyyy-MM-dd";
  17. private static final String format1 = "yyyy-MM-dd HH:mm:ss";
  18. private static final String format2 = "MM/dd/yyyy HH:mm:ss";
  19. // 第一次调用get将返回null
  20. private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>();
  21. // 获取线程的变量副本,如果不覆盖initialValue,第一次get返回null,故需要初始化一个SimpleDateFormat,并set到threadLocal中
  22. public static SimpleDateFormat getFormat() {
  23. SimpleDateFormat df = (SimpleDateFormat) threadLocal.get();
  24. if (df == null) {
  25. df = new SimpleDateFormat(format);
  26. threadLocal.set(df);
  27. }
  28. return df;
  29. }
  30. public static SimpleDateFormat getFormat1() {
  31. SimpleDateFormat df1 = (SimpleDateFormat) threadLocal.get();
  32. if (df1 == null) {
  33. df1 = new SimpleDateFormat(format1);
  34. threadLocal.set(df1);
  35. }
  36. return df1;
  37. }
  38. public static SimpleDateFormat getFormat2() {
  39. SimpleDateFormat df2 = (SimpleDateFormat) threadLocal.get();
  40. if (df2 == null) {
  41. df2 = new SimpleDateFormat(format2);
  42. threadLocal.set(df2);
  43. }
  44. return df2;
  45. }
  46. private DateUtils() {
  47. }
  48. /**
  49. * 获取系统日期(无时分秒毫秒)
  50. *
  51. * @return
  52. */
  53. public static Date today() {
  54. return truncate(now());
  55. }
  56. /**
  57. * 获取系统时间
  58. *
  59. * @return
  60. */
  61. public static Date now() {
  62. return new Date();
  63. }
  64. /**
  65. * 根据年月日生成日期对象
  66. *
  67. * @param y
  68. * @param m
  69. * @param d
  70. * @return
  71. */
  72. public static Date cons(int y, int m, int d) {
  73. Calendar cal = Calendar.getInstance();
  74. cal.set(y, m, d, 0, 0, 0);
  75. return cal.getTime();
  76. }
  77. public static String toDate(Date date) {
  78. return getFormat1().format(date);
  79. }
  80. public static String toDate2(Date date) {
  81. return getFormat2().format(date);
  82. }
  83. public static String toDate1(Date date) {
  84. return getFormat().format(date);
  85. }
  86. /**
  87. * 根据年月日时分秒生成日期对象
  88. *
  89. * @param y
  90. * @param m
  91. * @param d
  92. * @param h
  93. * @param mi
  94. * @param s
  95. * @return
  96. */
  97. public static Date cons(int y, int m, int d, int h, int mi, int s) {
  98. Calendar cal = Calendar.getInstance();
  99. cal.set(y, m, d, h, mi, s);
  100. return cal.getTime();
  101. }
  102. /**
  103. * 将指定时间转化为 Calendar
  104. *
  105. * @param date
  106. * @return
  107. */
  108. public static Calendar getCal(Date date) {
  109. Calendar cal = Calendar.getInstance();
  110. cal.setTime(date);
  111. return cal;
  112. }
  113. /**
  114. * 将时间的时分秒毫秒字段去掉
  115. *
  116. * @param date
  117. * @return
  118. */
  119. public static Date truncate(Date date) {
  120. Calendar cal = Calendar.getInstance();
  121. cal.setTime(date);
  122. cal.set(Calendar.HOUR_OF_DAY, 0);
  123. cal.set(Calendar.MINUTE, 0);
  124. cal.set(Calendar.SECOND, 0);
  125. cal.set(Calendar.MILLISECOND, 0);
  126. return cal.getTime();
  127. }
  128. /**
  129. * 去掉日期中日及下级字段
  130. *
  131. * @param date
  132. * @return
  133. */
  134. public static Date truncDay(Date date) {
  135. Calendar cal = Calendar.getInstance();
  136. cal.setTime(date);
  137. cal.set(Calendar.DAY_OF_MONTH, 1);
  138. cal.set(Calendar.HOUR_OF_DAY, 0);
  139. cal.set(Calendar.MINUTE, 0);
  140. cal.set(Calendar.SECOND, 0);
  141. cal.set(Calendar.MILLISECOND, 0);
  142. return cal.getTime();
  143. }
  144. /**
  145. * 去掉日期中的月及下级字段
  146. *
  147. * @param date
  148. * @return
  149. */
  150. public static Date truncMonth(Date date) {
  151. Calendar cal = Calendar.getInstance();
  152. cal.setTime(date);
  153. cal.set(Calendar.MONTH, 0);
  154. cal.set(Calendar.DAY_OF_MONTH, 1);
  155. cal.set(Calendar.HOUR_OF_DAY, 0);
  156. cal.set(Calendar.MINUTE, 0);
  157. cal.set(Calendar.SECOND, 0);
  158. cal.set(Calendar.MILLISECOND, 0);
  159. return cal.getTime();
  160. }
  161. /**
  162. * 在指定时间上加指定的天数
  163. *
  164. * @param date
  165. * @param day
  166. * @return
  167. */
  168. public static Date addDays(Date date, int day) {
  169. Calendar cal = Calendar.getInstance();
  170. cal.setTime(date);
  171. cal.add(Calendar.DAY_OF_MONTH, day);
  172. return cal.getTime();
  173. }
  174. /**
  175. * 在指定的时间上加指定的月数
  176. *
  177. * @param date
  178. * @param month
  179. * @return
  180. */
  181. public static Date addMonths(Date date, int month) {
  182. Calendar cal = Calendar.getInstance();
  183. cal.setTime(date);
  184. cal.add(Calendar.MONTH, month);
  185. return cal.getTime();
  186. }
  187. /**
  188. * 在指定的时间上加指定的月数
  189. *
  190. * @param date
  191. * @param month
  192. * @return
  193. */
  194. public static Date addYears(Date date, int year) {
  195. Calendar cal = Calendar.getInstance();
  196. cal.setTime(date);
  197. cal.add(Calendar.YEAR, year);
  198. return cal.getTime();
  199. }
  200. /**
  201. * 在指定时间上加指定的小时
  202. *
  203. * @param date
  204. * @param day
  205. * @return
  206. */
  207. public static Date addHours(Date date, int hour) {
  208. return new Date(date.getTime() + hour * 3600 * 1000);
  209. }
  210. /**
  211. * 在指定时间上加指定的分钟
  212. *
  213. * @param date
  214. * @param day
  215. * @return
  216. */
  217. public static Date addMinutes(Date date, int m) {
  218. return new Date(date.getTime() + m * 60 * 1000);
  219. }
  220. /**
  221. * 在指定时间上加指定的秒
  222. *
  223. * @param date
  224. * @param day
  225. * @return
  226. */
  227. public static Date addSeconds(Date date, int s) {
  228. return new Date(date.getTime() + s * 1000);
  229. }
  230. /**
  231. * 计算两个时间之间差的天数(取整后)
  232. *
  233. * @param d1
  234. * @param d2
  235. * @return
  236. */
  237. public static int daysDiff(Date d1, Date d2) {
  238. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
  239. }
  240. /**
  241. * 计算两个时间之间差的小时数(取整后)
  242. *
  243. * @param d1
  244. * @param d2
  245. * @return
  246. */
  247. public static int hoursDiff(Date d1, Date d2) {
  248. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
  249. }
  250. public static double hoursDiff1(Date d1, Date d2) {
  251. return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
  252. }
  253. public static double hoursDiff2(Date d1, Date d2) {
  254. return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
  255. }
  256. /**
  257. * 计算两个时间之间差的分钟数(取整后)
  258. *
  259. * @param d1
  260. * @param d2
  261. * @return
  262. */
  263. public static int minutesDiff(Date d1, Date d2) {
  264. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
  265. }
  266. /**
  267. * 计算两个时间之间差的分钟数(取整后)
  268. *
  269. * @param d1
  270. * @param d2
  271. * @return
  272. */
  273. public static double minutesDiff2(Date d1, Date d2) {
  274. return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
  275. }
  276. /**
  277. * 计算两个时间之间差的毫秒数(取整后)
  278. *
  279. * @param d1
  280. * @param d2
  281. * @return
  282. */
  283. public static long millisecondDiff(Date d1, Date d2) {
  284. return Math.abs(d1.getTime() - d2.getTime());
  285. }
  286. /**
  287. * 计算两个时间之间差的秒数(取整后)
  288. *
  289. * @param d1
  290. * @param d2
  291. * @return
  292. */
  293. public static int secondsDiff(Date d1, Date d2) {
  294. return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (1000));
  295. }
  296. /**
  297. * 计算两个时间之间的月差
  298. *
  299. * @param d1
  300. * @param d2
  301. * @return
  302. */
  303. public static int monthsDiff(Date d1, Date d2) {
  304. Calendar cal1 = Calendar.getInstance();
  305. Calendar cal2 = Calendar.getInstance();
  306. cal1.setTime(d1);
  307. cal2.setTime(d2);
  308. return (int) Math.abs((cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH));
  309. }
  310. /**
  311. * 获得指定时间的月数
  312. *
  313. * @param date
  314. * @return
  315. */
  316. public static int getMonth(Date date) {
  317. Calendar cd = Calendar.getInstance();
  318. cd.setTime(date);
  319. return cd.get(Calendar.MONTH);
  320. }
  321. /**
  322. * 获得指定时间的年数
  323. *
  324. * @param date
  325. * @return
  326. */
  327. public static int getYear(Date date) {
  328. Calendar cd = Calendar.getInstance();
  329. cd.setTime(date);
  330. return cd.get(Calendar.YEAR);
  331. }
  332. /**
  333. * 获取指定时间的天数
  334. *
  335. * @param date
  336. * @return
  337. */
  338. public static int getDay(Date date) {
  339. Calendar cd = Calendar.getInstance();
  340. cd.setTime(date);
  341. return cd.get(Calendar.DAY_OF_MONTH);
  342. }
  343. public static int getCurrentMonthLastDay() {
  344. Calendar a = Calendar.getInstance();
  345. a.set(Calendar.DATE, 1);
  346. a.roll(Calendar.DATE, -1);
  347. int maxDate = a.get(Calendar.DATE);
  348. return maxDate;
  349. }
  350. public static int getMonthDays(Date date) {
  351. Calendar cal = Calendar.getInstance();
  352. cal.set(DateUtils.getYear(date), DateUtils.getMonth(date), DateUtils.getDay(date));
  353. int dayst = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  354. return dayst;
  355. }
  356. /**
  357. * 获取当前月的第一天
  358. *
  359. * @return
  360. */
  361. public static String getCurrtenFirstDay() {
  362. Calendar c = Calendar.getInstance();
  363. // c.add(Calendar.MONTH, 0);
  364. c.set(Calendar.DAY_OF_MONTH, 1);
  365. return getFormat().format(c.getTime());
  366. }
  367. /**
  368. * 获取当前月的最后一天
  369. *
  370. * @return
  371. */
  372. public static String getCurrtenLastDay() {
  373. Calendar ca = Calendar.getInstance();
  374. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  375. return getFormat().format(ca.getTime());
  376. }
  377. /**
  378. * 获取当前月的第一天
  379. *
  380. * @return
  381. */
  382. public static Date getCurrtenFirstDate() {
  383. Calendar c = Calendar.getInstance();
  384. c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
  385. c.set(Calendar.HOUR_OF_DAY, 0);
  386. c.set(Calendar.MINUTE, 0);
  387. c.set(Calendar.SECOND, 1);
  388. return c.getTime();
  389. }
  390. /**
  391. * 获取当前月的最后一天
  392. *
  393. * @return
  394. */
  395. public static Date getCurrtenLastDate() {
  396. Calendar c = Calendar.getInstance();
  397. c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
  398. c.set(Calendar.HOUR_OF_DAY, 23);
  399. c.set(Calendar.MINUTE, 59);
  400. c.set(Calendar.SECOND, 59);
  401. return c.getTime();
  402. }
  403. public static Date parseDate(String date) {
  404. try {
  405. return getFormat().parse(date);
  406. } catch (ParseException e) {
  407. e.printStackTrace();
  408. }
  409. return null;
  410. }
  411. public static Date parseDate1(String date) {
  412. try {
  413. return getFormat1().parse(date);
  414. } catch (Exception e) {
  415. e.printStackTrace();
  416. }
  417. return null;
  418. }
  419. public static Date parseDate2(String date) {
  420. try {
  421. return getFormat2().parse(date);
  422. } catch (ParseException e) {
  423. e.printStackTrace();
  424. }
  425. return null;
  426. }
  427. public static Date parseLongToDate(long time) {
  428. return new Date(time);
  429. }
  430. /**
  431. * 转换Edna时间格式为标准格式
  432. *
  433. * @param pointTime
  434. * @return
  435. */
  436. public static String convertEdnaTime2(String pointTime, Boolean isNoSec) {
  437. StringBuffer sb = new StringBuffer();
  438. String[] dt = pointTime.split(" ");
  439. String[] ymd = dt[0].split("-");
  440. String[] hms = dt[1].split(":");
  441. sb.append(ymd[0]).append("-");
  442. if (ymd[1].length() == 1) {
  443. sb.append("0").append(ymd[1]);
  444. } else {
  445. sb.append(ymd[1]);
  446. }
  447. if (ymd[2].length() == 1) {
  448. sb.append("-").append("0").append(ymd[2]);
  449. } else {
  450. sb.append("-").append(ymd[2]);
  451. }
  452. if (hms[0].length() == 1) {
  453. sb.append(" ").append("0").append(hms[0]);
  454. } else {
  455. sb.append(" ").append(hms[0]);
  456. }
  457. if (hms[1].length() == 1) {
  458. sb.append(":").append("0").append(hms[1]);
  459. } else {
  460. sb.append(":").append(hms[1]);
  461. }
  462. if (isNoSec) {
  463. sb.append(":").append("00");
  464. } else {
  465. if (hms[2].length() == 1) {
  466. sb.append(":").append("0").append(hms[2]);
  467. } else {
  468. sb.append(":").append(hms[2]);
  469. }
  470. }
  471. return sb.toString();
  472. }
  473. /**
  474. * 转换Edna时间格式为标准格式
  475. *
  476. * @param pointTime
  477. * @return
  478. */
  479. public static String convertEdnaTime(String pointTime, Boolean isNoSec) {
  480. String date = getFormat().format(new Date());
  481. StringBuffer sb = new StringBuffer();
  482. String[] dt = pointTime.split(" ");
  483. String[] ymd = dt[0].split("/");
  484. String[] hms = dt[1].split(":");
  485. if (ymd[2].length() == 2) {
  486. sb.append(date.substring(0, 2)).append(ymd[2]).append("-");
  487. }
  488. if (ymd[0].length() == 1) {
  489. sb.append("0").append(ymd[0]);
  490. } else {
  491. sb.append(ymd[0]);
  492. }
  493. if (ymd[1].length() == 1) {
  494. sb.append("-").append("0").append(ymd[1]);
  495. } else {
  496. sb.append("-").append(ymd[1]);
  497. }
  498. if (hms[0].length() == 1) {
  499. sb.append(" ").append("0").append(hms[0]);
  500. } else {
  501. sb.append(" ").append(hms[0]);
  502. }
  503. if (hms[1].length() == 1) {
  504. sb.append(":").append("0").append(hms[1]);
  505. } else {
  506. sb.append(":").append(hms[1]);
  507. }
  508. if (isNoSec) {
  509. sb.append(":").append("00");
  510. } else {
  511. if (hms[2].length() == 1) {
  512. sb.append(":").append("0").append(hms[2]);
  513. } else {
  514. sb.append(":").append(hms[2]);
  515. }
  516. }
  517. return sb.toString();
  518. }
  519. public static String convertEdnaTime(String pointTime) {
  520. return convertEdnaTime2(pointTime, false);
  521. }
  522. public static void main(String[] args) {
  523. System.out.println(DateUtils.getMonthDays(DateUtils.today()));
  524. }
  525. }