发送https请求工具类

  1. /**
  2. * 发送https请求
  3. *
  4. * @param requestUrl 请求地址
  5. * @param requestMethod 请求方式(GET、POST)
  6. * @param outputStr 提交的数据
  7. * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
  8. */
  9. public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
  10. JSONObject jsonObject = null;
  11. try {
  12. // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  13. TrustManager[] tm = {new MyX509TrustManager()};
  14. SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
  15. sslContext.init(null, tm, new java.security.SecureRandom());
  16. // 从上述SSLContext对象中得到SSLSocketFactory对象
  17. SSLSocketFactory ssf = sslContext.getSocketFactory();
  18. URL url = new URL(requestUrl);
  19. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  20. conn.setSSLSocketFactory(ssf);
  21. conn.setDoOutput(true);
  22. conn.setDoInput(true);
  23. conn.setUseCaches(false);
  24. // 设置请求方式(GET/POST)
  25. conn.setRequestMethod(requestMethod);
  26. // 当outputStr不为null时向输出流写数据
  27. if (null != outputStr) {
  28. OutputStream outputStream = conn.getOutputStream();
  29. // 注意编码格式
  30. outputStream.write(outputStr.getBytes("UTF-8"));
  31. outputStream.close();
  32. }
  33. // 从输入流读取返回内容
  34. InputStream inputStream = conn.getInputStream();
  35. InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
  36. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  37. String str = null;
  38. StringBuffer buffer = new StringBuffer();
  39. while ((str = bufferedReader.readLine()) != null) {
  40. buffer.append(str);
  41. }
  42. // 释放资源
  43. bufferedReader.close();
  44. inputStreamReader.close();
  45. inputStream.close();
  46. inputStream = null;
  47. conn.disconnect();
  48. jsonObject = JSONObject.parseObject(buffer.toString());
  49. } catch (ConnectException ce) {
  50. log.error("连接超时:{}", ce);
  51. } catch (Exception e) {
  52. log.error("https请求异常:{}", e);
  53. }
  54. return jsonObject;
  55. }

日期工具类

  1. package com.utils;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.constants.Constants;
  6. import com.exception.CrmebException;
  7. import com.utils.vo.dateLimitUtilVo;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Calendar;
  15. import java.util.Date;
  16. import java.util.List;
  17. public final class DateUtil {
  18. private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
  19. private DateUtil() {
  20. throw new IllegalStateException("Utility class");
  21. }
  22. /**
  23. * 获取当前日期,指定格式
  24. * 描述:<描述函数实现的功能>.
  25. *
  26. * @return
  27. */
  28. public static String nowDate() {
  29. return nowDate(Constants.DATE_FORMAT_NUM);
  30. }
  31. /**
  32. * 获取当前年,指定格式
  33. * 描述:<描述函数实现的功能>.
  34. *
  35. * @return
  36. */
  37. public static String nowYear() {
  38. Calendar cal = Calendar.getInstance();
  39. return cal.get(Calendar.YEAR) + "";
  40. }
  41. /**
  42. * 获取上一年,指定格式
  43. * 描述:<描述函数实现的功能>.
  44. *
  45. * @return
  46. */
  47. public static String lastYear() {
  48. Calendar cal = Calendar.getInstance();
  49. cal.add(Calendar.YEAR, -1);
  50. return cal.get(Calendar.YEAR) + "";
  51. }
  52. /**
  53. * 获取当前日期,指定格式
  54. * 描述:<描述函数实现的功能>.
  55. *
  56. * @return
  57. */
  58. public static Date nowDateTime() {
  59. return strToDate(nowDateTimeStr(), Constants.DATE_FORMAT);
  60. }
  61. /**
  62. * 获取当前日期,指定格式
  63. * 描述:<描述函数实现的功能>.
  64. *
  65. * @return
  66. */
  67. public static String nowDateTimeStr() {
  68. return nowDate(Constants.DATE_FORMAT);
  69. }
  70. /**
  71. * 获取当前日期,指定格式
  72. * 描述:<描述函数实现的功能>.
  73. *
  74. * @return
  75. */
  76. public static String nowDate(String DATE_FORMAT) {
  77. SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT);
  78. return dft.format(new Date());
  79. }
  80. /**
  81. * 获取当前日期,指定格式
  82. * 描述:<描述函数实现的功能>.
  83. *
  84. * @return
  85. */
  86. public static String nowDateTime(String DATE_FORMAT) {
  87. SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT);
  88. return dft.format(new Date());
  89. }
  90. /**
  91. * 获取当前日期,指定格式
  92. * 描述:<描述函数实现的功能>.
  93. *
  94. * @return
  95. */
  96. public static Integer getNowTime() {
  97. long t = (System.currentTimeMillis()/1000L);
  98. return Integer.parseInt(String.valueOf(t));
  99. }
  100. /**
  101. * 获取当前时间戳(秒级)
  102. * @return
  103. */
  104. public static Long getTime() {
  105. return (System.currentTimeMillis()/1000L);
  106. }
  107. /**
  108. * 获取当前日期,指定格式
  109. * 描述:<描述函数实现的功能>.
  110. *
  111. * @return
  112. */
  113. public static Date nowDateTimeReturnDate(String DATE_FORMAT) {
  114. SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT);
  115. return strToDate(dft.format(new Date()), DATE_FORMAT);
  116. }
  117. /**
  118. * convert a date to string in a specifies fromat.
  119. *
  120. * @param date
  121. * @param DATE_FORMAT
  122. * @return
  123. */
  124. public static String dateToStr(Date date, String DATE_FORMAT) {
  125. if (date == null) {
  126. return null;
  127. }
  128. SimpleDateFormat myFormat = new SimpleDateFormat(DATE_FORMAT);
  129. return myFormat.format(date);
  130. }
  131. /**
  132. * parse a String to Date in a specifies fromat.
  133. *
  134. * @param dateStr
  135. * @param DATE_FORMAT
  136. * @return
  137. * @throws ParseException
  138. */
  139. public static Date strToDate(String dateStr, String DATE_FORMAT) {
  140. SimpleDateFormat myFormat = new SimpleDateFormat(DATE_FORMAT);
  141. try {
  142. return myFormat.parse(dateStr);
  143. } catch (ParseException e) {
  144. return null;
  145. }
  146. }
  147. /**
  148. * date add Second
  149. *
  150. * @param date
  151. * @param num
  152. * @return
  153. */
  154. public static Date addSecond(Date date, int num) {
  155. Calendar calendar = Calendar.getInstance();
  156. calendar.setTime(date);
  157. calendar.add(Calendar.SECOND, num);
  158. return calendar.getTime();
  159. }
  160. /**
  161. * date add Second return String
  162. *
  163. * @param date
  164. * @param num
  165. * @return
  166. */
  167. public static String addSecond(Date date, int num, String DATE_FORMAT) {
  168. Calendar calendar = Calendar.getInstance();
  169. calendar.setTime(date);
  170. calendar.add(Calendar.SECOND, num);
  171. return dateToStr(calendar.getTime(), DATE_FORMAT);
  172. }
  173. /**
  174. * 指定日期加上天数后的日期
  175. *
  176. * @param num 为增加的天数
  177. * @param newDate 创建时间
  178. * @return
  179. */
  180. public static final String addDay(String newDate, int num, String pattern) {
  181. SimpleDateFormat format = new SimpleDateFormat(pattern);
  182. try {
  183. Date currdate = format.parse(newDate);
  184. Calendar ca = Calendar.getInstance();
  185. ca.setTime(currdate);
  186. ca.add(Calendar.DATE, num);
  187. return format.format(ca.getTime());
  188. } catch (ParseException e) {
  189. LOGGER.error("转化时间出错,", e);
  190. return null;
  191. }
  192. }
  193. /**
  194. * 指定日期加上天数后的日期
  195. *
  196. * @param num 为增加的天数
  197. * @param newDate 创建时间
  198. * @return
  199. */
  200. public static final String addDay(Date newDate, int num, String pattern) {
  201. SimpleDateFormat format = new SimpleDateFormat(pattern);
  202. Calendar ca = Calendar.getInstance();
  203. ca.setTime(newDate);
  204. ca.add(Calendar.DATE, num);
  205. return format.format(ca.getTime());
  206. }
  207. /**
  208. * convert long to date
  209. *
  210. * @param date 待转换时间戳
  211. * @return 转换后时间
  212. */
  213. public static Date timeStamp11ToDate(Integer date) {
  214. return new Date(date);
  215. }
  216. /**
  217. * convert long to date string
  218. *
  219. * @param date 待转换时间戳
  220. * @param DATE_FORMAT 格式化时间
  221. * @return 格式化后的时间
  222. */
  223. public static String timeStamp11ToDate(Integer date, String DATE_FORMAT) {
  224. return dateToStr(new Date(date), DATE_FORMAT);
  225. }
  226. /**
  227. * compare two date String with a pattern
  228. *
  229. * @param date1
  230. * @param date2
  231. * @param pattern
  232. * @return
  233. */
  234. public static int compareDate(String date1, String date2, String pattern) {
  235. SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(pattern);
  236. try {
  237. Date dt1 = DATE_FORMAT.parse(date1);
  238. Date dt2 = DATE_FORMAT.parse(date2);
  239. if (dt1.getTime() > dt2.getTime()) {
  240. return 1;
  241. } else if (dt1.getTime() < dt2.getTime()) {
  242. return -1;
  243. } else {
  244. return 0;
  245. }
  246. } catch (ParseException e) {
  247. e.printStackTrace();
  248. return 0;
  249. }
  250. }
  251. /**
  252. * 检查日期格式是否合法
  253. * @param date
  254. * @param style
  255. * @return
  256. */
  257. public static boolean checkDateFormat(String date, String style) {
  258. SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(style);
  259. try {
  260. // 设置lenient为false.
  261. // 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
  262. DATE_FORMAT.setLenient(false);
  263. DATE_FORMAT.parse(date);
  264. return true;
  265. } catch (ParseException e) {
  266. return false;
  267. }
  268. }
  269. /**
  270. * 计算两个时间之间的天数差
  271. * @param beforeDay 开始时间
  272. * @param afterDay 结束时间
  273. * @return 相差天数
  274. */
  275. public static long getTwoDateDays(Date beforeDay, Date afterDay) {
  276. SimpleDateFormat sm = new SimpleDateFormat(Constants.DATE_FORMAT_NUM);
  277. long days = -1;
  278. try {
  279. days = (sm.parse(sm.format(afterDay)).getTime() - sm.parse(sm.format(beforeDay)).getTime()) / (1000 * 3600 * 24);
  280. } catch (Exception e) {
  281. e.printStackTrace();
  282. }
  283. return days;
  284. }
  285. //获取时间戳11位
  286. public static int getSecondTimestamp(Date date){
  287. if (null == date) {
  288. return 0;
  289. }
  290. String timestamp = String.valueOf(date.getTime()/1000);
  291. return Integer.parseInt(timestamp);
  292. }
  293. //获取时间戳11位
  294. public static int getSecondTimestamp(String date){
  295. if (null == date) {
  296. return 0;
  297. }
  298. Date date1 = strToDate(date, Constants.DATE_FORMAT);
  299. if(date1 == null){
  300. return 0;
  301. }
  302. String timestamp = String.valueOf(date1.getTime()/1000);
  303. return Integer.parseInt(timestamp);
  304. }
  305. //获取时间戳10位
  306. public static int getSecondTimestamp(Long timeMillis){
  307. if (null == timeMillis) {
  308. return 0;
  309. }
  310. String timestamp = String.valueOf(timeMillis / 1000);
  311. return Integer.parseInt(timestamp);
  312. }
  313. //获取时间戳11位
  314. public static int getSecondTimestamp(){
  315. Date date = strToDate(nowDateTime(Constants.DATE_FORMAT), Constants.DATE_FORMAT);
  316. if (null == date) {
  317. return 0;
  318. }
  319. String timestamp = String.valueOf(date.getTime()/1000);
  320. return Integer.parseInt(timestamp);
  321. }
  322. /** 获得昨天日期:yyyy-MM-dd HH:mm:ss */
  323. public static String getYesterdayStr() {
  324. Calendar c = Calendar.getInstance();
  325. c.add(Calendar.DATE, -1);
  326. SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  327. return startSdf.format(c.getTime());
  328. }
  329. /** 获得本周第一天:yyyy-MM-dd HH:mm:ss */
  330. public static String getWeekStartDay() {
  331. Calendar c = Calendar.getInstance();
  332. c.add(Calendar.WEEK_OF_MONTH, 0);
  333. c.set(Calendar.DAY_OF_WEEK, 2);
  334. SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  335. return startSdf.format(c.getTime());
  336. }
  337. /** 获得本周最后一天:yyyy-MM-dd HH:mm:ss */
  338. public static String getWeekEndDay() {
  339. return addDay(getWeekStartDay(), 7, Constants.DATE_FORMAT);
  340. }
  341. /** 获得上周第一天:yyyy-MM-dd HH:mm:ss */
  342. public static String getLastWeekStartDay() {
  343. return addDay(getWeekStartDay(), -7, Constants.DATE_FORMAT);
  344. }
  345. /** 获得上周最后一天:yyyy-MM-dd HH:mm:ss */
  346. public static String getLastWeekEndDay() {
  347. return addDay(getLastWeekStartDay(), 7, Constants.DATE_FORMAT);
  348. }
  349. /** 获得本月最后一天:yyyy-MM-dd HH:mm:ss */
  350. public static String getMonthEndDay() {
  351. Calendar c = Calendar.getInstance();
  352. c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
  353. SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  354. return endSdf.format(c.getTime());
  355. }
  356. /** 获得上月第一天:yyyy-MM-dd HH:mm:ss */
  357. public static String getLastMonthStartDay() {
  358. Calendar c = Calendar.getInstance();
  359. c.add(Calendar.MONTH, -1);
  360. c.set(Calendar.DAY_OF_MONTH, 1);
  361. SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-01 00:00:00");
  362. return startSdf.format(c.getTime());
  363. }
  364. /** 获得上月最后一天:yyyy-MM-dd HH:mm:ss */
  365. public static String getLastMonthEndDay() {
  366. Calendar c = Calendar.getInstance();
  367. c.add(Calendar.MONTH, -1);
  368. c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
  369. SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  370. return endSdf.format(c.getTime());
  371. }
  372. /** 获得上年第一天:yyyy-MM-dd HH:mm:ss */
  373. public static String getLastYearStartDay() {
  374. Calendar c = Calendar.getInstance();
  375. c.add(Calendar.YEAR, -1);
  376. SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
  377. return startSdf.format(c.getTime());
  378. }
  379. /** 获得上年最后一天:yyyy-MM-dd HH:mm:ss */
  380. public static String getLastYearEndDay() {
  381. Calendar c = Calendar.getInstance();
  382. c.add(Calendar.YEAR, -1);
  383. SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
  384. return endSdf.format(c.getTime());
  385. }
  386. /**
  387. * 计算时间范围
  388. * @param data string 类型
  389. * @author Mr.Zhang
  390. * @since 2020-05-06
  391. * @return dateLimitUtilVo
  392. */
  393. public static dateLimitUtilVo getDateLimit(String data){
  394. //时间计算
  395. String startTime = null;
  396. String endTime = DateUtil.nowDateTime(Constants.DATE_FORMAT);
  397. String day = DateUtil.nowDateTime(Constants.DATE_FORMAT_START);
  398. String end = DateUtil.nowDateTime(Constants.DATE_FORMAT_END);
  399. if(!StringUtils.isBlank(data)){
  400. switch (data){
  401. case Constants.SEARCH_DATE_DAY:
  402. startTime = day;
  403. break;
  404. case Constants.SEARCH_DATE_YESTERDAY:
  405. startTime = DateUtil.addDay(day, -1, Constants.DATE_FORMAT_START);
  406. endTime = DateUtil.addDay(end, -1, Constants.DATE_FORMAT_END);
  407. break;
  408. case Constants.SEARCH_DATE_LATELY_7:
  409. startTime = DateUtil.addDay(day, -7, Constants.DATE_FORMAT_START);
  410. break;
  411. case Constants.SEARCH_DATE_WEEK:
  412. startTime = getWeekStartDay();
  413. endTime = getWeekEndDay();
  414. break;
  415. case Constants.SEARCH_DATE_PRE_WEEK:
  416. startTime = getLastWeekStartDay();
  417. endTime = getLastWeekEndDay();
  418. break;
  419. case Constants.SEARCH_DATE_LATELY_30:
  420. startTime = DateUtil.addDay(day, -30, Constants.DATE_FORMAT_START);
  421. break;
  422. case Constants.SEARCH_DATE_MONTH:
  423. startTime = DateUtil.nowDateTime(Constants.DATE_FORMAT_MONTH_START);
  424. endTime = getMonthEndDay();
  425. break;
  426. case Constants.SEARCH_DATE_PRE_MONTH:
  427. startTime = getLastMonthStartDay();
  428. endTime = getLastMonthEndDay();
  429. break;
  430. case Constants.SEARCH_DATE_YEAR:
  431. startTime = DateUtil.nowDateTime(Constants.DATE_FORMAT_YEAR_START);
  432. endTime = DateUtil.nowDateTime(Constants.DATE_FORMAT_YEAR_END);
  433. break;
  434. case Constants.SEARCH_DATE_PRE_YEAR:
  435. startTime = getLastYearStartDay();
  436. endTime = getLastYearEndDay();
  437. break;
  438. default:
  439. List<String> list = CrmebUtil.stringToArrayStr(data);
  440. if(list.size() == 1){
  441. throw new CrmebException("选择时间参数格式错误,请在 " +
  442. Constants.SEARCH_DATE_DAY + "|" +
  443. Constants.SEARCH_DATE_YESTERDAY + "|" +
  444. Constants.SEARCH_DATE_LATELY_7 + "|" +
  445. Constants.SEARCH_DATE_LATELY_30 + "|" +
  446. Constants.SEARCH_DATE_MONTH + "|" +
  447. Constants.SEARCH_DATE_YEAR + "|自定义时间范围(格式:yyyy-MM-dd HH:mm:ss,两个时间范围用逗号分割)");
  448. }
  449. startTime = list.get(0);
  450. endTime = list.get(1);
  451. // if (startTime.equals(endTime)) {
  452. startTime = DateUtil.appointedDayStrToFormatStr(startTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_START);
  453. endTime = DateUtil.appointedDayStrToFormatStr(endTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_END);
  454. // }
  455. break;
  456. }
  457. }
  458. return new dateLimitUtilVo(startTime, endTime);
  459. }
  460. /**
  461. * 获取某一时间段内的时间集合
  462. * @param data string 类型
  463. * @author Mr.Zhang
  464. * @since 2020-05-06
  465. * @return List<Date>
  466. */
  467. public static List<String> getListDate(String data) {
  468. //获取30天的开始结束日期
  469. dateLimitUtilVo dateLimit = DateUtil.getDateLimit(data);
  470. //定义日期集合
  471. List<String> date = new ArrayList<>();
  472. //开始日期
  473. Date startDate = DateUtil.strToDate(dateLimit.getStartTime(), Constants.DATE_FORMAT);
  474. //结束日期
  475. Date endDate = DateUtil.strToDate(dateLimit.getEndTime(), Constants.DATE_FORMAT);
  476. while (endDate.after(startDate)){
  477. date.add(DateUtil.dateToStr(startDate, Constants.DATE_FORMAT_DATE)); // 放入集合
  478. startDate = DateUtil.strToDate(DateUtil.addDay(startDate, 1, Constants.DATE_FORMAT), Constants.DATE_FORMAT); //循环一次 加一天
  479. }
  480. return date;
  481. }
  482. /**
  483. * 两个日期之前的相差天数
  484. * @param starDate 开始日期
  485. * @param endDate 结束日期
  486. * @return 相差天数
  487. */
  488. public static int daysBetween(Date starDate,Date endDate){
  489. Calendar cal = Calendar.getInstance();
  490. cal.setTime(starDate);
  491. long time1 = cal.getTimeInMillis();
  492. cal.setTime(endDate);
  493. long time2 = cal.getTimeInMillis();
  494. long between_days=(time2-time1)/(1000*3600*24);
  495. return Integer.parseInt(String.valueOf(between_days));
  496. }
  497. /**
  498. * 获取指定日期指定格式字符串
  499. *
  500. * @param dateStr
  501. * @param DATE_FORMAT
  502. * @return
  503. * @throws ParseException
  504. */
  505. public static String appointedDayStrToFormatStr(String dateStr, String STR_DATE_FORMAT, String DATE_FORMAT) {
  506. Date date = DateUtil.strToDate(dateStr, STR_DATE_FORMAT);
  507. return DateUtil.dateToStr(date, DATE_FORMAT);
  508. }
  509. /**
  510. * 获取当前时间小时
  511. * @return 当前时间小时 默认24小时
  512. */
  513. public static int getCurrentHour(){
  514. int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
  515. return hour;
  516. }
  517. /**
  518. * 13位时间戳转字符串
  519. * @param timestamp
  520. * @param dateFormat
  521. * @return
  522. */
  523. public static String timestamp2DateStr(Long timestamp, String dateFormat) {
  524. if (ObjectUtil.isNull(timestamp)) {
  525. return "";
  526. }
  527. if (StrUtil.isBlank(dateFormat)) {
  528. dateFormat = Constants.DATE_FORMAT;
  529. }
  530. Date date = new Date(timestamp);
  531. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
  532. return simpleDateFormat.format(date);
  533. }
  534. /**
  535. * 字符串转13位时间戳
  536. */
  537. public static Long dateStr2Timestamp(String dateStr, String type) {
  538. DateTime parse = cn.hutool.core.date.DateUtil.parse(dateStr);
  539. if (StrUtil.isNotBlank(type)) {
  540. if (type.equals(Constants.DATE_TIME_TYPE_BEGIN)) {
  541. parse = cn.hutool.core.date.DateUtil.beginOfDay(parse);
  542. }
  543. if (type.equals(Constants.DATE_TIME_TYPE_END)) {
  544. parse = cn.hutool.core.date.DateUtil.endOfDay(parse);
  545. }
  546. }
  547. return parse.getTime();
  548. }
  549. public static void main(String[] args) {
  550. System.out.println(StringUtils.strip("fsfsdf", "f"));
  551. }
  552. }