DateUtils.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.iden.common.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5. /**
  6. * @description 时间控件
  7. * @author make Java
  8. */
  9. public class DateUtils {
  10. private static final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
  11. private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
  12. private static final SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
  13. private static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  14. private static final SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
  15. /**
  16. * 一天毫秒数
  17. */
  18. private static final long ONE_DAY = 1000 *60* 60 * 24L;
  19. private DateUtils(){}
  20. /**
  21. * 俩个时间间隔天数
  22. * @param beginDate
  23. * @param endDate
  24. * @return
  25. */
  26. public static int getDaysGap(Date beginDate, Date endDate) {
  27. long beginTimeMillis = beginDate.getTime();
  28. long endTimeMills = endDate.getTime();
  29. long time = endTimeMills - beginTimeMillis;
  30. double day = (time *1.0D / ONE_DAY);
  31. return (int)Math.floor(day);
  32. }
  33. /**
  34.    * 俩个时间间隔天数字符串的日期格式的计算
  35.    */
  36. public static int daysBetween(String smdate, String bdate) {
  37. try {
  38. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  39. Calendar cal = Calendar.getInstance();
  40. cal.setTime(sdf.parse(smdate));
  41. long time1 = cal.getTimeInMillis();
  42. cal.setTime(sdf.parse(bdate));
  43. long time2 = cal.getTimeInMillis();
  44. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  45. return Integer.parseInt(String.valueOf(between_days));
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. return 0;
  50. }
  51. /**
  52. * date日期过num天的日期
  53. * @param date
  54. * @param num
  55. * @return
  56. */
  57. public static Date dateGapDays(Date date, int num) {
  58. Calendar cal = Calendar.getInstance();
  59. cal.setTime(date);
  60. cal.add(5, num);
  61. return cal.getTime();
  62. }
  63. /**
  64. * 字符串格式日期转日期格式日期
  65. * @param date
  66. * @param pattern
  67. * @return
  68. */
  69. public static Date strToDate(String date,String pattern){
  70. if(isEmpty(date) || isEmpty(pattern)){
  71. return null ;
  72. }
  73. SimpleDateFormat format = new SimpleDateFormat(pattern);
  74. try {
  75. return format.parse(date) ;
  76. } catch (ParseException e) {
  77. e.printStackTrace();
  78. return null ;
  79. }
  80. }
  81. /**
  82. * 日期转日期格式日期
  83. * @param date
  84. * @param pattern
  85. * @return
  86. */
  87. public static String formatToDateStr(Date date,String pattern) {
  88. if(date==null || isEmpty(pattern)){
  89. return null ;
  90. }else{
  91. SimpleDateFormat sd = new SimpleDateFormat(pattern);
  92. return sd.format(date);
  93. }
  94. }
  95. /**
  96. * 检查字符串
  97. * @param str
  98. * @return
  99. */
  100. private static boolean isEmpty(String str) {
  101. return null == str ? true : "".equals(str.replaceAll(" +", ""));
  102. }
  103. /**
  104. * 日期的间隔分钟
  105. *
  106. * @param startDate
  107. * @param endDate
  108. * @return
  109. */
  110. public static Long betweenDate(Date startDate, Date endDate) {
  111. return betweenDateSSS(startDate, endDate) / 1000/60;
  112. }
  113. /**
  114. * 日期的间隔毫秒数
  115. *
  116. * @param startDate
  117. * @param endDate
  118. * @return
  119. */
  120. public static long betweenDateSSS(Date startDate, Date endDate) {
  121. return null == startDate || null == endDate ? 0 : endDate.getTime() - startDate.getTime();
  122. }
  123. /**
  124. * 俩时间段之间的间隔秒数
  125. *
  126. * @param startTime(HH:mm:ss)
  127. * @param enTime(HH:mm:ss)
  128. * @return
  129. */
  130. public static long betweenTimeSS(String startTime, String enTime) {
  131. String dateStr = getCurrYyyy_MM_ddDate();
  132. String start = dateStr + " " + startTime;
  133. String end = dateStr + " " + enTime;
  134. Date startDate = strToDate(start,"yyyy-MM-dd HH:mm:ss");
  135. Date endDate = strToDate(end,"yyyy-MM-dd HH:mm:ss");
  136. return (endDate.getTime() - startDate.getTime()) /1000;
  137. }
  138. /**
  139. * 判断一个字符串是不是一个合法的日期格式
  140. *
  141. * @param str
  142. * @param pattern
  143. * @return
  144. */
  145. public static boolean isValidDate(String str,String pattern) throws ParseException{
  146. SimpleDateFormat format = new SimpleDateFormat(pattern);
  147. try {
  148. format.setLenient(false);
  149. format.parse(str);
  150. } catch (ParseException e) {
  151. throw new java.text.ParseException("不是一个合法的日期格式",0);
  152. }
  153. return true;
  154. }
  155. /**
  156. * 根据毫秒数获取时间字符串
  157. * yyyy-MM-dd HH:mm:ss
  158. * @param millisecondTime
  159. * @return
  160. */
  161. public static String formatMillisecondTime(String millisecondTime){
  162. String time = String.valueOf(Long.valueOf(millisecondTime));
  163. return yyyy_MM_dd_HH_mm_ss.format(new Date(Long.valueOf(time)));
  164. }
  165. /**
  166. * 获取当时间并格式化为:yyyyMMddHHmmss格式
  167. *
  168. * @return 返回yyyyMMddHHmmss格式的时间字符串
  169. * @throws Exception
  170. */
  171. public static String getCurrYyyyMMddHHmmssDate() {
  172. return yyyyMMddHHmmss.format(new Date());
  173. }
  174. /**
  175. * 获取当时间并格式化为:yyyyMMdd格式
  176. *
  177. * @return 返回yyyyMMdd格式的时间字符串
  178. * @throws Exception
  179. */
  180. public static String getCurrYyyyMMddDate() {
  181. return yyyyMMdd.format(new Date());
  182. }
  183. /**
  184. * 获取昨天并格式化为:yyyyMMdd格式
  185. *
  186. * @return 返回yyyyMMdd格式的时间字符串
  187. * @throws Exception
  188. */
  189. public static String getYestodayYyyyMMddDate() {
  190. Calendar cal = Calendar.getInstance();
  191. cal.add(Calendar.DATE,-1);
  192. String yesterday = yyyyMMdd.format(cal.getTime());
  193. return yesterday;
  194. }
  195. /**
  196. * 把字符串时间段转成日期时间
  197. * @param startTime
  198. * @param endTime
  199. * @return
  200. */
  201. public static Map<String,Date> convertDate(String startTime, String endTime) {
  202. Map<String,Date> map = new HashMap<>();
  203. try {
  204. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  205. Calendar start = Calendar.getInstance();
  206. start.setTime(sdf.parse(startTime));
  207. Calendar end = Calendar.getInstance();
  208. end.setTime(sdf.parse(endTime));
  209. if (start.after(end)) { //跨天了
  210. end.add(Calendar.DATE,1);
  211. }
  212. map.put("startDate", start.getTime());
  213. map.put("endDate", end.getTime());
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. return map;
  218. }
  219. /**
  220. * 获取num天前date
  221. *
  222. * @return 返回yyyyMMdd格式的时间字符串
  223. * @throws Exception
  224. */
  225. public static Date getBeforeDates(int num) {
  226. Calendar cal = Calendar.getInstance();
  227. cal.add(Calendar.DATE,-num);
  228. cal.getTime();
  229. return cal.getTime();
  230. }
  231. /**
  232. * 获取当时间并格式化为:yyyy-MM-dd格式
  233. *
  234. * @return 返回yyyy-MM-dd格式的时间字符串
  235. * @throws Exception
  236. */
  237. public static String getCurrYyyy_MM_ddDate() {
  238. return yyyy_MM_dd.format(new Date());
  239. }
  240. /**
  241. * 计算时间格式为:yyyy-MM-dd HH:mm:ss的时间差值
  242. *
  243. * @param startTime 开始时间
  244. * @param endTime 结束时间
  245. * @return 返回时间,单位秒
  246. * @throws Exception
  247. */
  248. public static long compute_yyyy_MM_ddHHmmssTime(String startTime, String endTime) throws Exception {
  249. Date startDate = yyyy_MM_dd_HH_mm_ss.parse(startTime);
  250. Date endDate = yyyy_MM_dd_HH_mm_ss.parse(endTime);
  251. return (endDate.getTime() - startDate.getTime()) / 1000L;
  252. }
  253. /**
  254. * 获取两个日期之间的所有日期(yyyyMMdd,yyyyMMdd)
  255. * @Description TODO
  256. * @param begin
  257. * @param end
  258. * @return
  259. * @author XuJD
  260. * @date 2017-4-1
  261. */
  262. public static List<String> getBetweenDates(String begin, String end) {
  263. List<String> result = new ArrayList<>();
  264. try {
  265. Date beginDate = yyyyMMdd.parse(begin);
  266. Date endDate = yyyyMMdd.parse(end);
  267. Calendar tempStart = Calendar.getInstance();
  268. tempStart.setTime(beginDate);
  269. while (beginDate.getTime() <= endDate.getTime()) {
  270. result.add(yyyyMMdd.format(tempStart.getTime()));
  271. tempStart.add(Calendar.DAY_OF_YEAR, 1);
  272. beginDate = tempStart.getTime();
  273. }
  274. } catch (Exception e){
  275. e.printStackTrace();
  276. }
  277. return result;
  278. }
  279. public static void main(String[] args) {
  280. Map map = convertDate("20:12:00","23:00:00");
  281. System.out.println(map);
  282. }
  283. }