DateUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package com.java110.utils.util;
  2. import java.sql.Timestamp;
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. /**
  11. * Created by wuxw on 2017/7/24.
  12. */
  13. public class DateUtil {
  14. private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
  15. public static final String LAST_TIME = "2038-01-01 00:00:00";
  16. private static Map<String, SimpleDateFormat> formats = new HashMap();
  17. public static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
  18. public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
  19. public static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
  20. public static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
  21. public static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
  22. public static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  23. public static final String DATE_FORMATE_STRING_F = "yyyy-MM-dd'T'HH:mm:ssZ";
  24. public static final String DATE_FORMATE_STRING_G = "yyyy-MM-dd'T'HH:mm:ssz";
  25. public static final String DATE_FORMATE_STRING_H = "yyyyMMdd";
  26. public static final String DATE_FORMATE_STRING_I = "yyyy-MM-dd HH:mm:ss.SSS";
  27. public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";
  28. public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS";
  29. static {
  30. formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
  31. formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  32. formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
  33. formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"));
  34. formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"));
  35. formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
  36. formats.put("yyyy-MM-dd'T'HH:mm:ssZ", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
  37. formats.put("yyyy-MM-dd'T'HH:mm:ssz", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
  38. formats.put("yyyyMMdd", new SimpleDateFormat("yyyyMMdd"));
  39. formats.put("yyyy-MM-dd HH:mm:ss.SSS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
  40. formats.put("yyyyMMddHHmmss.SSS", new SimpleDateFormat("yyyyMMddHHmmss.SSS"));
  41. formats.put("yyyyMMddHHmmssSSS", new SimpleDateFormat("yyyyMMddHHmmssSSS"));
  42. }
  43. /**
  44. * 返回 yyyyMMddhhmmss 格式的日期串
  45. *
  46. * @return
  47. */
  48. public static String getyyyyMMddhhmmssDateString() {
  49. return dateFormat.format(new Date());
  50. }
  51. /**
  52. * 获取当前时间
  53. *
  54. * @return
  55. */
  56. public static Date getCurrentDate() {
  57. Calendar calendar = Calendar.getInstance();
  58. return calendar.getTime();
  59. }
  60. /**
  61. * 获取当前月
  62. *
  63. * @return
  64. */
  65. public static int getCurrentMonth() {
  66. Calendar calendar = Calendar.getInstance();
  67. return calendar.get(Calendar.MONTH) + 1;
  68. }
  69. public static Date getLastDate() throws ParseException {
  70. return getDateFromString("2037-12-01", DATE_FORMATE_STRING_B);
  71. }
  72. /**
  73. * 转TimeStamp
  74. *
  75. * @param date
  76. * @return
  77. */
  78. public static Timestamp getTimestamp(Date date) {
  79. Timestamp timestamp = new Timestamp(date.getTime());
  80. return timestamp;
  81. }
  82. /**
  83. * 获取未来时间
  84. *
  85. * @param second 秒
  86. * @return
  87. */
  88. public static Date getFutureDate(int second) {
  89. Calendar calendar = Calendar.getInstance();
  90. calendar.add(Calendar.SECOND, second);
  91. return calendar.getTime();
  92. }
  93. public static String getFormatTimeString(Date date, String pattern) {
  94. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  95. synchronized (sDateFormat) {
  96. return sDateFormat.format(date);
  97. }
  98. }
  99. public static String getDefaultFormateTimeString(Date date) {
  100. return getFormatTimeString(date, "yyyyMMddHHmmss");
  101. }
  102. public static SimpleDateFormat getDateFormat(String pattern) {
  103. SimpleDateFormat sDateFormat = (SimpleDateFormat) formats.get(pattern);
  104. if (sDateFormat == null) {
  105. sDateFormat = new SimpleDateFormat(pattern);
  106. formats.put(pattern, sDateFormat);
  107. }
  108. return sDateFormat;
  109. }
  110. public static Date getDateFromString(String date, String pattern)
  111. throws ParseException {
  112. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  113. synchronized (sDateFormat) {
  114. return sDateFormat.parse(date);
  115. }
  116. }
  117. public static Date getDefaultDateFromString(String date)
  118. throws ParseException {
  119. return getDateFromString(date, "yyyyMMddHHmmss");
  120. }
  121. public static String getNowDefault() {
  122. return getNow("yyyyMMddHHmmss");
  123. }
  124. public static String getNow(String pattern) {
  125. return getFormatTimeString(new Date(), pattern);
  126. }
  127. public static String getLastTime(){
  128. return LAST_TIME;
  129. }
  130. public static String getNowII() {
  131. return getFormatTimeString(new Date(), "yyyyMMdd");
  132. }
  133. public static long dateString2Long(String str, String pattern)
  134. throws ParseException {
  135. return getDateFromString(str, pattern).getTime();
  136. }
  137. /**
  138. * 校验字符串是否可以格式化为时间
  139. *
  140. * @param str
  141. * @param pattern
  142. * @return
  143. */
  144. public static boolean judgeDate(String str, String pattern) {
  145. try {
  146. dateString2Long(str, pattern);
  147. } catch (Exception e) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. public static String longToDateStringDefault(long time) {
  153. return getFormatTimeString(new Date(time), "yyyyMMddHHmmss");
  154. }
  155. public static String longToDateString(long time, String pattern) {
  156. return getFormatTimeString(new Date(time), pattern);
  157. }
  158. public static long date2Long(Date date) {
  159. return date.getTime();
  160. }
  161. public static Date longToDate(long time) {
  162. return new Date(time);
  163. }
  164. public static Date getDateFromStringAdaptTwoPattern(String date)
  165. throws ParseException {
  166. try {
  167. return getDateFromString(date, "yyyy-MM-dd HH:mm:ss");
  168. } catch (ParseException e) {
  169. }
  170. return getDateFromString(date, "yyyy-MM-dd");
  171. }
  172. public static String changeNumDateToDate(String numdate, String inFormat, String outFormat)
  173. throws ParseException {
  174. Date date = getDateFromString(numdate, inFormat);
  175. return getFormatTimeString(date, outFormat);
  176. }
  177. public static String getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  178. throws ParseException {
  179. Date date = getDateFromString(nowdate, inFormat);
  180. Calendar cl = Calendar.getInstance();
  181. cl.setTime(date);
  182. cl.set(2, cl.get(2) + 1);
  183. cl.set(5, 1);
  184. date = cl.getTime();
  185. return getFormatTimeString(date, outFormat);
  186. }
  187. public static boolean isLeapYear(int year) {
  188. if (year % 400 == 0)
  189. return true;
  190. if (year % 4 == 0) {
  191. return (year % 100 != 0);
  192. }
  193. return false;
  194. }
  195. public static String getLastDay(String nowdate, String inFormat, String outFormat)
  196. throws ParseException {
  197. String returndate = "";
  198. Date date = getDateFromString(nowdate, inFormat);
  199. Calendar cl = Calendar.getInstance();
  200. cl.setTime(date);
  201. switch (cl.get(2)) {
  202. case 0:
  203. cl.set(5, 31);
  204. break;
  205. case 1:
  206. int year = cl.get(1);
  207. if (isLeapYear(year))
  208. cl.set(5, 29);
  209. else {
  210. cl.set(5, 28);
  211. }
  212. break;
  213. case 2:
  214. cl.set(5, 31);
  215. break;
  216. case 3:
  217. cl.set(5, 30);
  218. break;
  219. case 4:
  220. cl.set(5, 31);
  221. break;
  222. case 5:
  223. cl.set(5, 30);
  224. break;
  225. case 6:
  226. cl.set(5, 31);
  227. break;
  228. case 7:
  229. cl.set(5, 31);
  230. break;
  231. case 8:
  232. cl.set(5, 30);
  233. break;
  234. case 9:
  235. cl.set(5, 31);
  236. break;
  237. case 10:
  238. cl.set(5, 30);
  239. break;
  240. case 11:
  241. cl.set(5, 31);
  242. }
  243. date = cl.getTime();
  244. returndate = getFormatTimeString(date, outFormat);
  245. return returndate;
  246. }
  247. public static String getMonthLastDay(String fmt) {
  248. String returndate = "";
  249. Date date = null;
  250. Calendar cl = Calendar.getInstance();
  251. switch (cl.get(2)) {
  252. case 0:
  253. cl.set(5, 31);
  254. break;
  255. case 1:
  256. int year = cl.get(1);
  257. if (isLeapYear(year))
  258. cl.set(5, 29);
  259. else {
  260. cl.set(5, 28);
  261. }
  262. break;
  263. case 2:
  264. cl.set(5, 31);
  265. break;
  266. case 3:
  267. cl.set(5, 30);
  268. break;
  269. case 4:
  270. cl.set(5, 31);
  271. break;
  272. case 5:
  273. cl.set(5, 30);
  274. break;
  275. case 6:
  276. cl.set(5, 31);
  277. break;
  278. case 7:
  279. cl.set(5, 31);
  280. break;
  281. case 8:
  282. cl.set(5, 30);
  283. break;
  284. case 9:
  285. cl.set(5, 31);
  286. break;
  287. case 10:
  288. cl.set(5, 30);
  289. break;
  290. case 11:
  291. cl.set(5, 31);
  292. }
  293. date = cl.getTime();
  294. returndate = getFormatTimeString(date, fmt);
  295. return returndate;
  296. }
  297. public static String getNextMonthFirstDay(String fmt) {
  298. String returndate = "";
  299. Date date = null;
  300. Calendar cl = Calendar.getInstance();
  301. cl.set(2, cl.get(2) + 1);
  302. cl.set(5, 1);
  303. date = cl.getTime();
  304. returndate = getFormatTimeString(date, fmt);
  305. return returndate;
  306. }
  307. public static boolean compareDate(String fistDate, String secondDate, String format)
  308. throws ParseException {
  309. boolean flag = false;
  310. Date fist = null;
  311. Date second = null;
  312. fist = getDateFromString(fistDate, format);
  313. second = getDateFromString(secondDate, format);
  314. if (fist.before(second)) {
  315. flag = true;
  316. }
  317. return flag;
  318. }
  319. public static boolean isRightDate(String value, String varValue) {
  320. try {
  321. SimpleDateFormat format = new SimpleDateFormat(varValue);
  322. format.setLenient(false);
  323. format.parse(value);
  324. } catch (ParseException e) {
  325. return false;
  326. }
  327. return true;
  328. }
  329. public static int getCurrentMonthDay() {
  330. Calendar a = Calendar.getInstance();
  331. a.set(Calendar.DATE, 1);
  332. a.roll(Calendar.DATE, -1);
  333. int maxDate = a.get(Calendar.DATE);
  334. return maxDate;
  335. }
  336. /**
  337. * 在给定的日期加上或减去指定月份后的日期
  338. *
  339. * @param sourceDate 原始时间
  340. * @param month 要调整的月份,向前为负数,向后为正数
  341. * @return
  342. */
  343. public static Date stepMonth(Date sourceDate, int month) {
  344. Calendar c = Calendar.getInstance();
  345. c.setTime(sourceDate);
  346. c.add(Calendar.MONTH, month);
  347. return c.getTime();
  348. }
  349. }