DateUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package platform.common.util;
  2. import java.sql.Timestamp;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.TimeZone;
  8. import org.apache.commons.lang3.StringUtils;
  9. /**
  10. * Created by luohaifeng on 2017/8/9.
  11. */
  12. public class DateUtil {
  13. public static String YYYY_MM_DD = "yyyy-MM-dd";
  14. public static String YYYY_MM = "yyyy-MM";
  15. public static String getCurrentDateString(String fmt) {
  16. SimpleDateFormat sdf = new SimpleDateFormat(fmt);
  17. return sdf.format(new Date());
  18. }
  19. public static String getCurrentDateString() {
  20. return getCurrentDateString(YYYY_MM_DD);
  21. }
  22. /**
  23. * 日期转字符串
  24. *
  25. * @param date
  26. * @return
  27. */
  28. public static String getTimeString(Date date) {
  29. if (null == date) {
  30. return "";
  31. } else {
  32. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  33. return simpleDateFormat.format(date);
  34. }
  35. }
  36. // 获取当前时间
  37. public static Date getNowDate() {
  38. TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
  39. TimeZone.setDefault(time);
  40. Date now = new Date();
  41. return now;
  42. }
  43. public static String getTime(Date date) {
  44. if (null == date) {
  45. return "";
  46. } else {
  47. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
  48. return simpleDateFormat.format(date);
  49. }
  50. }
  51. /**
  52. * 日期转字符串 yyyy-MM-dd
  53. *
  54. * @param date
  55. * @return
  56. */
  57. public static String getTimeStringShort(Date date) {
  58. if (null == date) {
  59. return "";
  60. } else {
  61. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  62. return simpleDateFormat.format(date);
  63. }
  64. }
  65. /**
  66. * 字符串转时间转换日期格式
  67. *
  68. * @param date 时间字符串
  69. * @param pattern 字符串时间格式
  70. * @return
  71. * @throws ParseException
  72. */
  73. public static Date parseTimeStringToDate(String date, String pattern) throws ParseException {
  74. if (null == date) {
  75. return null;
  76. } else {
  77. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
  78. return simpleDateFormat.parse(date);
  79. }
  80. }
  81. public static long getDay(String pay_time,Integer cycle) throws ParseException {
  82. Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
  83. Calendar calendar = Calendar.getInstance();
  84. calendar.setTime(date);
  85. calendar.add(Calendar.MONTH, -cycle);
  86. return ( date.getTime()-calendar.getTimeInMillis()) / (24 * 60 * 60 * 1000);
  87. }
  88. public static Boolean isGenerate(String pay_time,Integer remind) throws ParseException {
  89. if(null == remind) {
  90. remind = 5;
  91. }
  92. Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
  93. Calendar calendar = Calendar.getInstance();
  94. calendar.setTime(date);
  95. calendar.add(Calendar.DATE, -remind);
  96. if(calendar.getTimeInMillis()<=new Date().getTime()){
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * 2017-08-19 20:30
  103. *
  104. * @return
  105. */
  106. public static String[] getDate(String time) {
  107. String[] strs = time.split(":");
  108. return strs;
  109. }
  110. public static String getPayTime(Integer payment_cycle, String contract_start_date) throws ParseException {
  111. Calendar plan = Calendar.getInstance();
  112. plan.setTime(parseTimeStringToDate(contract_start_date, YYYY_MM_DD));
  113. plan.add(Calendar.MONTH, payment_cycle);
  114. return getTimeStringShort(plan.getTime());
  115. }
  116. public static String AddMonth(Integer month, String date) throws ParseException {
  117. Calendar plan = Calendar.getInstance();
  118. plan.setTime(parseTimeStringToDate(date, YYYY_MM_DD));
  119. plan.add(Calendar.MONTH, month);
  120. return getTimeStringShort(plan.getTime());
  121. }
  122. public static String getCurrentMonth( ) throws ParseException {
  123. Calendar plan = Calendar.getInstance();
  124. plan.setTime(new Date());
  125. return String.valueOf(plan.get(Calendar.MONTH)+1);
  126. }
  127. public static String getCurrentYear( ) throws ParseException {
  128. Calendar plan = Calendar.getInstance();
  129. plan.setTime(new Date());
  130. return String.valueOf(plan.get(Calendar.YEAR));
  131. }
  132. public static String getMonthStart( String date) throws ParseException {
  133. Calendar plan = Calendar.getInstance();
  134. plan.setTime(parseTimeStringToDate(date, YYYY_MM));
  135. plan.add(Calendar.MONTH, 0);
  136. plan.set(Calendar.DAY_OF_MONTH, 1);
  137. return getTime(plan.getTime());
  138. }
  139. public static String getMonthEnd( String date) throws ParseException {
  140. Calendar plan = Calendar.getInstance();
  141. plan.setTime(parseTimeStringToDate(date, YYYY_MM));
  142. plan.add(Calendar.MONTH, 1);
  143. plan.set(Calendar.DAY_OF_MONTH, 0);
  144. return getTime(plan.getTime());
  145. }
  146. public static Boolean compareTo(String time1,String time2) throws ParseException {
  147. Date date1 = DateUtil.parseTimeStringToDate(time1, YYYY_MM_DD);
  148. Date date2 = DateUtil.parseTimeStringToDate(time2, YYYY_MM_DD);
  149. if (date1.getTime() - date2.getTime() >= 0) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. public static void main(String args[]){
  156. try {
  157. System.out.println(getCurrentMonth());
  158. System.out.println(getCurrentYear());
  159. } catch (ParseException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. /**
  164. * 两个日期之间的天数
  165. * @param date1
  166. * @param date2
  167. * @return
  168. * @throws ParseException
  169. */
  170. public static int getDateSpace(String date1, String date2)
  171. throws ParseException {
  172. Calendar calst = Calendar.getInstance();;
  173. Calendar caled = Calendar.getInstance();
  174. calst.setTime(DateUtil.parseTimeStringToDate(date1, YYYY_MM_DD));
  175. caled.setTime(DateUtil.parseTimeStringToDate(date2, YYYY_MM_DD));
  176. //设置时间为0时
  177. calst.set(Calendar.HOUR_OF_DAY, 0);
  178. calst.set(Calendar.MINUTE, 0);
  179. calst.set(Calendar.SECOND, 0);
  180. caled.set(Calendar.HOUR_OF_DAY, 0);
  181. caled.set(Calendar.MINUTE, 0);
  182. caled.set(Calendar.SECOND, 0);
  183. //得到两个日期相差的天数
  184. int days = ((int)(caled.getTime().getTime()/1000)-(int)(calst.getTime().getTime()/1000))/3600/24;
  185. return days;
  186. }
  187. //获取审核剩余时间 审核时间5天
  188. public static String getOverTimeStr(String create_time,Integer overDay) throws ParseException {
  189. String overTimeStr = "";
  190. if(null == overDay) {
  191. overDay = 5;
  192. }
  193. if(StringUtils.isNotBlank(create_time)) {
  194. Calendar overTime = Calendar.getInstance();
  195. Calendar now = Calendar.getInstance();
  196. overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
  197. overTime.add(Calendar.DATE, overDay);
  198. now.setTime(new Date());
  199. overTime.set(Calendar.MINUTE, 0);
  200. overTime.set(Calendar.SECOND, 0);
  201. now.set(Calendar.MINUTE, 0);
  202. now.set(Calendar.SECOND, 0);
  203. //得到两个日期相差的小时数
  204. int hours = ((int)(overTime.getTime().getTime()/1000)-(int)(now.getTime().getTime()/1000))/3600;
  205. if( hours/24 > 0) {
  206. overTimeStr = "剩余"+String.valueOf((int)Math.floor(hours/24))+"个工作日" ;//+(hours%24)+"小时";
  207. }else {
  208. overTimeStr = "超出"+String.valueOf((-1)*(int)Math.floor(hours/24))+"个工作日" ;//+(hours%24)+"小时";
  209. }
  210. }
  211. return overTimeStr;
  212. }
  213. //获取审核处理时间 审核时间5天
  214. public static String getDealTimeStr(String create_time,String deal_time,Integer overDay) throws ParseException {
  215. String overTimeStr = "";
  216. if(null == overDay) {
  217. overDay = 5;
  218. }
  219. if(StringUtils.isNotBlank(create_time) && StringUtils.isNotBlank(deal_time)) {
  220. Calendar overTime = Calendar.getInstance();
  221. Calendar dealTime = Calendar.getInstance();
  222. overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
  223. overTime.add(Calendar.DATE, overDay);
  224. dealTime.setTime(DateUtil.parseTimeStringToDate(deal_time, "yyyy-MM-dd HH:mm:ss"));
  225. overTime.set(Calendar.MINUTE, 0);
  226. overTime.set(Calendar.SECOND, 0);
  227. dealTime.set(Calendar.MINUTE, 0);
  228. dealTime.set(Calendar.SECOND, 0);
  229. //得到两个日期相差的小时数
  230. int hours = ((int)(overTime.getTime().getTime()/1000)-(int)(dealTime.getTime().getTime()/1000))/3600;
  231. if( hours/24 > 0) {
  232. overTimeStr = "提前"+String.valueOf((int)Math.floor(hours/24))+"个工作日完成";//+(hours%24)+"小时完成";
  233. }else {
  234. overTimeStr = "超出"+String.valueOf((-1)*(int)Math.floor(hours/24))+"个工作日完成";//+(hours%24)+"小时完成";
  235. }
  236. }
  237. return overTimeStr;
  238. }
  239. //5秒内不能重复同一请求
  240. public static boolean isFrequent(String time) throws ParseException {
  241. Date preTime = DateUtil.parseTimeStringToDate(time,"yyyy-MM-dd HH:mm:ss");
  242. Date currTime = new Date();
  243. if(currTime.getTime()-preTime.getTime()<5000) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. // 获得当天0点时间
  249. public static Date getTimesmorning() {
  250. Calendar cal = Calendar.getInstance();
  251. cal.set(Calendar.HOUR_OF_DAY, 0);
  252. cal.set(Calendar.SECOND, 0);
  253. cal.set(Calendar.MINUTE, 0);
  254. cal.set(Calendar.MILLISECOND, 0);
  255. return cal.getTime();
  256. }
  257. // 获得当天24点时间
  258. public static Date getTimesnight() {
  259. Calendar cal = Calendar.getInstance();
  260. cal.set(Calendar.HOUR_OF_DAY, 24);
  261. cal.set(Calendar.SECOND, 0);
  262. cal.set(Calendar.MINUTE, 0);
  263. cal.set(Calendar.MILLISECOND, 0);
  264. return cal.getTime();
  265. }
  266. // 获得本周一0点时间
  267. public static Date getTimesWeekmorning() {
  268. Calendar cal = Calendar.getInstance();
  269. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  270. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  271. return cal.getTime();
  272. }
  273. // 获得本周日24点时间
  274. public static Date getTimesWeeknight() {
  275. Calendar cal = Calendar.getInstance();
  276. cal.setTime(getTimesWeekmorning());
  277. cal.add(Calendar.DAY_OF_WEEK, 7);
  278. return cal.getTime();
  279. }
  280. // 获得本月第一天0点时间
  281. public static Date getTimesMonthmorning() {
  282. Calendar cal = Calendar.getInstance();
  283. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  284. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  285. return cal.getTime();
  286. }
  287. // 获得本月最后一天24点时间
  288. public static Date getTimesMonthnight() {
  289. Calendar cal = Calendar.getInstance();
  290. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  291. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  292. cal.set(Calendar.HOUR_OF_DAY, 24);
  293. return cal.getTime();
  294. }
  295. /**
  296. * 获取两个日期相差的月数
  297. */
  298. public static int getMonthDiff(Date d1, Date d2) {
  299. Calendar c1 = Calendar.getInstance();
  300. Calendar c2 = Calendar.getInstance();
  301. c1.setTime(d1);
  302. c2.setTime(d2);
  303. int year1 = c1.get(Calendar.YEAR);
  304. int year2 = c2.get(Calendar.YEAR);
  305. int month1 = c1.get(Calendar.MONTH);
  306. int month2 = c2.get(Calendar.MONTH);
  307. int day1 = c1.get(Calendar.DAY_OF_MONTH);
  308. int day2 = c2.get(Calendar.DAY_OF_MONTH);
  309. // 获取年的差值 
  310. int yearInterval = year1 - year2;
  311. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
  312. if (month1 < month2 || month1 == month2 && day1 < day2) {
  313. yearInterval--;
  314. }
  315. // 获取月数差值
  316. int monthInterval = (month1 + 12) - month2;
  317. if (day1 < day2) {
  318. monthInterval--;
  319. }
  320. monthInterval %= 12;
  321. int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
  322. return monthsDiff;
  323. }
  324. }