DateUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * FileName:DateUtil.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.*;
  23. /**
  24. * 时间工具类
  25. *
  26. * @author hermit
  27. * @date 2017 -06-10 14:04:41
  28. */
  29. public class DateUtil {
  30. private static final long ONE_DAY_MILL_SECS=1000*3600*24;
  31. /**
  32. * 时间格式满足yyyy-MM-dd
  33. *
  34. * @param start 开始时间
  35. * @param end 结束时间
  36. * @return
  37. * @throws ParseException
  38. */
  39. public static int dayDiff(String start, String end) throws ParseException {
  40. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  41. long from = sdf.parse(start).getTime();
  42. long to = sdf.parse(end).getTime();
  43. //获取天数差
  44. int days = (int) ((to - from)/ONE_DAY_MILL_SECS);
  45. return days;
  46. }
  47. // 将字符串日期 转换成 yyyy-MM-dd HH:mm:ss
  48. public static Date changeStrToDate(String dateStr) {
  49. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  50. Date date;
  51. try {
  52. date = sdf.parse(dateStr);
  53. return date;
  54. } catch (ParseException e) {
  55. e.printStackTrace();
  56. return null;
  57. }
  58. }
  59. // 将字符串日期 转换成 yyyy-MM-dd
  60. public static Date changeStrToDate2(String dateStr) {
  61. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  62. Date date;
  63. try {
  64. date = sdf.parse(dateStr);
  65. return date;
  66. } catch (ParseException e) {
  67. e.printStackTrace();
  68. return null;
  69. }
  70. }
  71. // 将字符串日期 转换成 yyyy-MM-dd
  72. public static Date changeStrToDate3(String dateStr, String pattern) {
  73. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  74. Date date;
  75. try {
  76. date = sdf.parse(dateStr);
  77. return date;
  78. } catch (ParseException e) {
  79. e.printStackTrace();
  80. return null;
  81. }
  82. }
  83. private static String getDateToStr(Date date, SimpleDateFormat sdf) {
  84. String dateStr = null;
  85. try {
  86. dateStr = sdf.format(date);
  87. return dateStr;
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. return null;
  91. }
  92. }
  93. // 将date类型转换为字符串yyyy-MM-dd HH:mm:ss
  94. public static String changeDateTOStr(Date date) {
  95. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  96. return getDateToStr(date, sdf);
  97. }
  98. // 将date类型转换为字符串yyyy-MM-dd
  99. public static String changeDateTOStr3(Date date) {
  100. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  101. return getDateToStr(date, sdf);
  102. }
  103. // 将date类型转换为字符串yyyy年MM月dd日
  104. public static String changeDateTOStr4(Date date) {
  105. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  106. return getDateToStr(date, sdf);
  107. }
  108. // 将date类型转换为字符串yyyy年MM月dd日
  109. public static String changeDateTOStr5(Date date, String pattern) {
  110. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  111. return getDateToStr(date, sdf);
  112. }
  113. // 将date类型转换为字符串yyyyMMddHHmmss
  114. public static String changeDateToYmdhms(Date date) {
  115. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  116. return getDateToStr(date, sdf);
  117. }
  118. // 将date类型转换为字符串yyyyMMdd--20100302
  119. public static String changeDateTOStr2(Date date) {
  120. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  121. return getDateToStr(date, sdf);
  122. }
  123. // 得到前一天的日期
  124. public static String getFormerDate() {
  125. Calendar calendar = Calendar.getInstance();
  126. calendar.add(Calendar.DATE, -1); // 得到前一天
  127. return changeDateTOStr2(calendar.getTime());
  128. }
  129. // 得到下一天的日期
  130. public static String getAfterOneDayDate() {
  131. Calendar calendar = Calendar.getInstance();
  132. calendar.add(Calendar.DATE, 1); // 得到前一天
  133. return changeDateTOStr2(calendar.getTime());
  134. }
  135. public static String getAfterOneDayDate2() {
  136. Calendar calendar = Calendar.getInstance();
  137. calendar.add(Calendar.DATE, 1); // 得到前一天
  138. return changeDateTOStr3(calendar.getTime());
  139. }
  140. // 得到 mountday前的日期,mountday为天数
  141. public static String getFormerMonth(int mountday) {
  142. Calendar calendar = Calendar.getInstance();
  143. calendar.add(Calendar.DATE, -mountday);
  144. return changeDateTOStr(calendar.getTime());
  145. }
  146. /**
  147. * 获得当前天(yyyy-MM-dd)
  148. *
  149. * @Description
  150. * @author hermit
  151. * @date 2015年3月6日 上午8:44:01
  152. * @return
  153. */
  154. public static String getNowDate() {
  155. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  156. return getDateToStr(new Date(), sdf);
  157. }
  158. /**
  159. * 获得当时间(yyyy-MM-dd HH:mm:ss)
  160. *
  161. * @Description
  162. * @author hermit
  163. * @date 2015年3月6日 上午8:44:45
  164. * @return
  165. */
  166. public static String getNowTime() {
  167. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  168. return getDateToStr(new Date(), sdf);
  169. }
  170. /**
  171. * 获取当前时间戳
  172. *
  173. * @return
  174. * @author hermit
  175. * @date 2015年10月
  176. */
  177. public static String getTimeStamp() {
  178. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  179. return getDateToStr(new Date(), sdf);
  180. }
  181. /**
  182. *
  183. * @Description 获取yyyyMM年月
  184. * @author hermit
  185. * @date 2015年3月19日 下午2:23:38
  186. * @param date
  187. * @return
  188. */
  189. public static String getDateToStr(Date date) {
  190. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
  191. return getDateToStr(date, sdf);
  192. }
  193. /**
  194. *
  195. * @Description 获取任意时间
  196. * @author hermit
  197. * @date 2015年4月14日 下午12:28:26
  198. * @param year 0为 当年,1为下一年
  199. * @param month 0为 当月,1为下一月
  200. * @param day 0为 当日,1为下一天
  201. * @param hour 0为 00点,1为1点
  202. * @param minute 0为 00分,1为1分
  203. * @param second 0为 00秒,1为1秒
  204. * @return
  205. */
  206. public static Date getSysDate(int year, int month, int day, int hour, int minute, int second) {
  207. Calendar cal = Calendar.getInstance();
  208. cal.add(Calendar.YEAR, year);
  209. cal.add(Calendar.MONTH, month);
  210. cal.add(Calendar.DAY_OF_MONTH, day);
  211. cal.add(Calendar.HOUR_OF_DAY, hour);
  212. cal.add(Calendar.MINUTE, minute);
  213. cal.add(Calendar.SECOND, second);
  214. return cal.getTime();
  215. }
  216. /**
  217. *
  218. * @Description 加num个月
  219. * @author hermit
  220. * @date 2015年4月14日 上午11:23:23
  221. * @param date 当前时间
  222. * @param num 延长的月数
  223. * @return
  224. */
  225. public static Date delayMonth(Date date, int num) {
  226. Calendar cal = Calendar.getInstance();
  227. cal.setTime(date);
  228. cal.add(Calendar.MONTH, num);
  229. date = cal.getTime();
  230. return date;
  231. }
  232. /**
  233. * 当前时间延期num个月数
  234. *
  235. * @author hermit
  236. * @date 2015年4月14日 下午5:24:06
  237. * @param num
  238. * @return
  239. */
  240. public static Date delayMonth(int num) {
  241. Calendar cal = Calendar.getInstance();
  242. cal.setTime(new Date());
  243. cal.add(Calendar.MONTH, num);
  244. return cal.getTime();
  245. }
  246. /**
  247. * 计算两个日期之间相隔的天数,不到一天不算
  248. * 如果开始日期大于截止日期,返回负数
  249. * @param s 开始日期
  250. * @param e 截止日期
  251. * @return
  252. */
  253. public static int diffDays(Date s, Date e){
  254. long difMill=e.getTime()-s.getTime();
  255. long days= difMill/ONE_DAY_MILL_SECS;
  256. return (int)days;
  257. }
  258. /**
  259. * 计算两个日期之间相隔的天数,不到一天不算
  260. * 如果开始日期大于截止日期,返回负数
  261. * @param s 开始日期
  262. * @param e 截止日期
  263. * @return
  264. */
  265. public static String diffYears(Date s, Date e){
  266. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  267. String s1=sdf.format(s);
  268. String e1=sdf.format(e);
  269. String[] arg1 = s1.split("-");
  270. String[] arg2 = e1.split("-");
  271. int year1 = Integer.valueOf(arg1[0]);
  272. int year2 = Integer.valueOf(arg2[0]);
  273. int month1 = Integer.valueOf(arg1[1]);
  274. int month2 = Integer.valueOf(arg2[1]);
  275. int day1 = Integer.valueOf(arg1[2]);
  276. int day2 = Integer.valueOf(arg2[2]);
  277. int md = 0 ;
  278. if(year1!=year2){
  279. md = day2>day1?0:-1;
  280. }
  281. int diffMonth = (year2*12+month2)-(year1*12+month1)+md;
  282. int yearNum = diffMonth/12;
  283. int monthNum = diffMonth % 12;
  284. String year = Integer.toString(yearNum);
  285. String month = Integer.toString(monthNum);
  286. String workdays=year+"年"+month+"月";
  287. return workdays;
  288. }
  289. /**
  290. * 获取过去的天数
  291. * @param date
  292. * @return
  293. */
  294. public static long pastDays(Date date) {
  295. long t = new Date().getTime()-date.getTime();
  296. return t/(24*60*60*1000);
  297. }
  298. /**
  299. * 获取过去的小时
  300. * @param date
  301. * @return
  302. */
  303. public static long pastHour(Date date) {
  304. long t = new Date().getTime()-date.getTime();
  305. return t/(60*60*1000);
  306. }
  307. /**
  308. * 获取过去的分钟
  309. * @param date
  310. * @return
  311. */
  312. public static long pastMinutes(Date date) {
  313. long t = new Date().getTime()-date.getTime();
  314. return t/(60*1000);
  315. }
  316. /**
  317. * 获取过去第几天的日期
  318. *
  319. * @param past
  320. * @return
  321. */
  322. public static String getPastDate(int past) {
  323. Calendar calendar = Calendar.getInstance();
  324. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
  325. Date today = calendar.getTime();
  326. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  327. String result = format.format(today);
  328. return result;
  329. }
  330. /**
  331. * 获取过去第几天的日期
  332. *
  333. * @param past
  334. * @return
  335. */
  336. public static Date getPastDate2(int past) {
  337. Calendar calendar = Calendar.getInstance();
  338. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
  339. Date today = calendar.getTime();
  340. return today;
  341. }
  342. public static Date timestampToDate(String str_num) {
  343. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  344. if (str_num.length() == 13) {
  345. String date = format.format(new Date(Long.parseLong(str_num)));
  346. return changeStrToDate(date);
  347. } else {
  348. String date = format.format(new Date(Integer.parseInt(str_num) * 1000L));
  349. return changeStrToDate(date);
  350. }
  351. }
  352. /**
  353. * 获取两个时间中的每一天
  354. * @param bigtimeStr 开始时间 yyyy-MM-dd HH:mm:ss
  355. * @param endTimeStr 结束时间 yyyy-MM-dd HH:mm:ss
  356. * @return
  357. * @throws ParseException
  358. */
  359. public static List<String> getDays(String bigtimeStr, String endTimeStr) throws ParseException {
  360. Date bigtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(bigtimeStr);
  361. Date endtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTimeStr);
  362. //定义一个接受时间的集合
  363. List<Date> lDate = new ArrayList<>();
  364. lDate.add(bigtime);
  365. Calendar calBegin = Calendar.getInstance();
  366. // 使用给定的 Date 设置此 Calendar 的时间
  367. calBegin.setTime(bigtime);
  368. Calendar calEnd = Calendar.getInstance();
  369. // 使用给定的 Date 设置此 Calendar 的时间
  370. calEnd.setTime(endtime);
  371. // 测试此日期是否在指定日期之后
  372. while (endtime.after(calBegin.getTime())) {
  373. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  374. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  375. lDate.add(calBegin.getTime());
  376. }
  377. List<String> datas = new LinkedList<>();
  378. for (Date date : lDate) {
  379. datas.add(new SimpleDateFormat("yyyy-MM-dd").format(date));
  380. }
  381. return datas;
  382. }
  383. public static void main(String[] args) throws Exception {
  384. String start="2018-01-04";
  385. String end="2018-01-03";
  386. System.out.println(dayDiff(start, end));
  387. // System.out.println(timestamp2Date("1480405849"));
  388. }
  389. }