parent
6272b6b540
commit
5c35c12675
16 changed files with 398 additions and 416 deletions
@ -0,0 +1,84 @@ |
|||||||
|
package com.ynxbd.common.helper.common; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
import java.time.Duration; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.time.format.DateTimeFormatter; |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
public class LocalDateHelper { |
||||||
|
|
||||||
|
/** |
||||||
|
* 字符串转LocalDateTime |
||||||
|
* |
||||||
|
* @param dateStr 字符串 |
||||||
|
* @return LocalDateTime |
||||||
|
*/ |
||||||
|
public static LocalDateTime strToLocalDateTime(String dateStr) { |
||||||
|
try { |
||||||
|
return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(DateHelper.DateEnum.yyyy_MM_dd_HH_mm_ss.TYPE)); |
||||||
|
} catch (Exception e) { |
||||||
|
log.error(e.getMessage()); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* [判断]当前时间 > 目标时间[minutes]分钟 |
||||||
|
* |
||||||
|
* @param target 待比较时间 |
||||||
|
* @return 大于当前时间6分钟 ? true: false |
||||||
|
*/ |
||||||
|
public static boolean isNowGreaterThanMinute(LocalDateTime target, int minutes) { |
||||||
|
if (target == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
Duration diff = Duration.between(target, LocalDateTime.now()); |
||||||
|
return diff.toMillis() > (minutes * 60 * 1000L); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* [判断]当前时间 < 目标时间[minutes]分钟 |
||||||
|
*/ |
||||||
|
public static boolean isNowLessThanMinute(LocalDateTime target, int minutes) { |
||||||
|
if (target == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
Duration diff = Duration.between(LocalDateTime.now(), target); |
||||||
|
return diff.toMillis() > (minutes * 60 * 1000L); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 减少时间 |
||||||
|
* |
||||||
|
* @param target 目标时间 |
||||||
|
* @param minutes 减少分钟 |
||||||
|
*/ |
||||||
|
public static LocalDateTime minusMinutes(LocalDateTime target, int minutes) { |
||||||
|
return target.minusMinutes(minutes); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 增加时间 |
||||||
|
* |
||||||
|
* @param target 目标时间 |
||||||
|
* @param minutes 减少分钟 |
||||||
|
*/ |
||||||
|
public static LocalDateTime plusMinutes(LocalDateTime target, int minutes) { |
||||||
|
return target.plusMinutes(minutes); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
LocalDateTime target = LocalDateTime.parse("2026-06-19T17:10:00"); |
||||||
|
System.out.println(target); |
||||||
|
boolean overNowMinute = isNowGreaterThanMinute(target, 6); |
||||||
|
System.out.println(overNowMinute); |
||||||
|
|
||||||
|
boolean overNowMinute2 = isNowLessThanMinute(target, 3); |
||||||
|
System.out.println(overNowMinute2); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,225 +0,0 @@ |
|||||||
//package com.ynxbd.common.helper.common;
|
|
||||||
//
|
|
||||||
//import com.alibaba.fastjson.JSON;
|
|
||||||
//import com.ynxbd.common.bean.sms.SmsTemplate;
|
|
||||||
//import com.ynxbd.common.config.EhcacheConfig;
|
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
|
||||||
//import org.ehcache.Cache;
|
|
||||||
//
|
|
||||||
//import java.util.Date;
|
|
||||||
//import java.util.Properties;
|
|
||||||
//
|
|
||||||
//@Slf4j
|
|
||||||
//public class SmsNewHelper {
|
|
||||||
// private SmsNewHelper() {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// final private static String OK = "OK";
|
|
||||||
//
|
|
||||||
// private static String ACCESS_KEY_ID;
|
|
||||||
// private static String ACCESS_KEY_SECRET;
|
|
||||||
// private static String SIGN_NAME;
|
|
||||||
// // 缓存
|
|
||||||
// private static Cache<String, SmsTemplate> CACHE;
|
|
||||||
//
|
|
||||||
// static {
|
|
||||||
// Properties properties = FileHelper.readProperties("sms.properties");
|
|
||||||
// // 读取配置文件...\wx\web\WEB-INF\classes\sms.properties
|
|
||||||
// if (properties != null) {
|
|
||||||
// ACCESS_KEY_ID = FileHelper.propertiesGetString(properties, "sms.accessKeyId");
|
|
||||||
// ACCESS_KEY_SECRET = FileHelper.propertiesGetString(properties, "sms.accessKeySecret");
|
|
||||||
// SIGN_NAME = FileHelper.propertiesGetString(properties, "sms.signName");
|
|
||||||
// }
|
|
||||||
// createCache();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private synchronized static void createCache() {
|
|
||||||
// if (CACHE == null) {
|
|
||||||
// // 创建一个缓存实例(5分钟最大存活时间)
|
|
||||||
// CACHE = EhcacheConfig.createCacheTTL(String.class, SmsTemplate.class, "sms_cache", (60L * 5));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 使用AK&SK初始化账号Client
|
|
||||||
// *
|
|
||||||
// * @param accessKeyId accessKeyId
|
|
||||||
// * @param accessKeySecret accessKeySecret
|
|
||||||
// * @return Client
|
|
||||||
// */
|
|
||||||
// public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) {
|
|
||||||
// try {
|
|
||||||
// com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
||||||
// // 您的AccessKey ID
|
|
||||||
// .setAccessKeyId(accessKeyId)
|
|
||||||
// // 您的AccessKey Secret
|
|
||||||
// .setAccessKeySecret(accessKeySecret);
|
|
||||||
// // 访问的域名
|
|
||||||
// config.endpoint = "dysmsapi.aliyuncs.com";
|
|
||||||
// return new com.aliyun.dysmsapi20170525.Client(config);
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// public static boolean sendAli(String template, String tel, String signName, Object smsTemplate) {
|
|
||||||
// try {
|
|
||||||
// com.aliyun.dysmsapi20170525.Client client = createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
|
||||||
// if (client == null) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
|
||||||
// .setPhoneNumbers(tel)
|
|
||||||
// .setSignName(signName)
|
|
||||||
// .setTemplateCode(template)
|
|
||||||
// .setTemplateParam(JSON.toJSONString(smsTemplate));
|
|
||||||
//
|
|
||||||
// com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
|
||||||
// // 复制代码运行请自行打印 API 的返回值
|
|
||||||
// com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
|
||||||
// if (resp == null) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// com.aliyun.dysmsapi20170525.models.SendSmsResponseBody body = resp.body;
|
|
||||||
// if (body == null) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (OK.equals(body.code) && OK.equals(body.message)) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// log.error("[短信]发送失败:{}", body.message);
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// log.error("[短信]发送失败:{}", e.getMessage());
|
|
||||||
// }
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 是否重复发送
|
|
||||||
// *
|
|
||||||
// * @param tel 储电话号码的集合
|
|
||||||
// * @return 验证码 | null = 发送异常
|
|
||||||
// */
|
|
||||||
// public static String isRepeat(String tel) {
|
|
||||||
// if (CACHE == null) {
|
|
||||||
// createCache();
|
|
||||||
// }
|
|
||||||
// if (CACHE != null) {
|
|
||||||
// SmsTemplate smsTemplate = CACHE.get(tel);
|
|
||||||
// if (smsTemplate == null) {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Long countDown = smsTemplate.getCountDown();
|
|
||||||
// if (countDown == null) {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// countDown = new Date().getTime() - countDown; // 计算倒计时
|
|
||||||
//
|
|
||||||
// if (countDown < 60000) { // 60s内请求,返回重复提示
|
|
||||||
// countDown = ((60000 - countDown) / 1000);
|
|
||||||
// String message = "重复请求" + countDown + "s";
|
|
||||||
// log.info(message);
|
|
||||||
// return message;
|
|
||||||
// }
|
|
||||||
// CACHE.remove(tel); // 超过60s,如果还存在则移除
|
|
||||||
// }
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 验证码验证
|
|
||||||
// *
|
|
||||||
// * @param phone 手机号码
|
|
||||||
// * @param code 验证码
|
|
||||||
// */
|
|
||||||
// public static boolean codeVerify(String phone, String code) {
|
|
||||||
// if (CACHE == null) {
|
|
||||||
// createCache();
|
|
||||||
// }
|
|
||||||
// if (CACHE == null) return false;
|
|
||||||
//
|
|
||||||
// SmsTemplate smsTemplate = CACHE.get(phone);
|
|
||||||
//
|
|
||||||
// if (smsTemplate == null) return false;
|
|
||||||
//
|
|
||||||
// return code.equals(smsTemplate.getCode());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 通用-发送短信
|
|
||||||
// *
|
|
||||||
// * @param template 模板
|
|
||||||
// * @param tel 电话号码
|
|
||||||
// * @param smsObj 模板内容
|
|
||||||
// */
|
|
||||||
// public static boolean send(String template, String tel, Object smsObj) {
|
|
||||||
// return send(template, tel, null, smsObj);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 通用-发送短信
|
|
||||||
// *
|
|
||||||
// * @param template 模板
|
|
||||||
// * @param tel 电话号码
|
|
||||||
// * @param signName 签名
|
|
||||||
// * @param smsObj 模板内容
|
|
||||||
// */
|
|
||||||
// public static boolean send(String template, String tel, String signName, Object smsObj) {
|
|
||||||
// boolean status = sendAli(template, tel, signName, smsObj);
|
|
||||||
// log.info("发送短信 tel={} {}", tel, status ? "成功" : "失败");
|
|
||||||
// return status;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 发送短信验证码
|
|
||||||
// *
|
|
||||||
// * @param template 短信模板
|
|
||||||
// * @param tel 电话号码
|
|
||||||
// * @return 是否发送成功
|
|
||||||
// * @code code 验证码
|
|
||||||
// */
|
|
||||||
// public static boolean sendCode(String template, String tel, String code) {
|
|
||||||
// return sendCode(template, tel, null, code);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 发送短信验证码
|
|
||||||
// *
|
|
||||||
// * @param template 短信模板
|
|
||||||
// * @param tel 电话号码
|
|
||||||
// * @param signName 签名
|
|
||||||
// * @return 是否发送成功
|
|
||||||
// * @code code 验证码
|
|
||||||
// */
|
|
||||||
// public static boolean sendCode(String template, String tel, String signName, String code) {
|
|
||||||
// SmsTemplate smsTemplate = new SmsTemplate();
|
|
||||||
// smsTemplate.setCode(code);
|
|
||||||
// smsTemplate.setTel(tel);
|
|
||||||
// smsTemplate.setCountDown(new Date().getTime());
|
|
||||||
//
|
|
||||||
// boolean status = send(template, tel, signName, smsTemplate);
|
|
||||||
//
|
|
||||||
// if (CACHE == null) {
|
|
||||||
// createCache();
|
|
||||||
// }
|
|
||||||
// if (status && CACHE != null) {
|
|
||||||
// CACHE.put(tel, smsTemplate); //添加缓存值
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
@ -1,4 +0,0 @@ |
|||||||
|
|
||||||
package com.ynxbd.common.helper.common; |
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in new issue