文章目录
正则匹配
String contactInfo = "12345678909";
private static final Pattern sqlPattern = Pattern.compile("^1[3|4|5|7|8|9][0-9]{9}$");
Matcher matcher = sqlPattern.matcher(contactInfo);
if (matcher.find()) {
return "匹配";
}
if (Pattern.matches("^1[3|4|5|7|8|9][0-9]{9}$", contactInfo)) {
return PHONE;
}
boolean b = contactInfo.matches("^1[3|4|5|7|8|9][0-9]{9}$");
// 匹配纯数字
String reg = "^(\d+)$";
// 匹配中文
String reg = "^[u4E00-u9FA5]$";
// sql防注入
String reg = "(?:')|(?:--)|(/\*(?:.|[\n\r])*?\*/)|"
+ "(\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\b)";
// 匹配邮箱
String reg = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
// 匹配手机号码
String reg = "^1[3|4|5|7|8|9][0-9]{9}$";
时间相关
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// 定义日期格式
private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyyMMdd");
// 自定义格式的系统当前时间
String currentTime = FMT.format(LocalDateTime.now());
String currentTime = LocalDateTime.now().format(FMT);
int currentHour = LocalDateTime.now().getHour();
// 系统当前时间的前一天和后一天
String beforeTime = FMT.format(LocalDateTime.now().minusDays(1))
String afterTime = FMT.format(LocalDateTime.now().plusDays(1)
// 年月日时分秒对应的minus或plus前缀的获取时间API,都是相同格式,返回值均为LocalDateTime类型
minusYears()、
plusMinutes()...
// 获取当天零点
LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN)
// 转换为毫秒时间戳
Long localTimestamp = localDateTime.toEpochSecond(ZoneOffset.of("+8")) * 1000;
// 获取当前时间的整点
LocalDateTime localDateTime = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth(), localDateTime.getHour(), 0, 0);
public String transferFormat(String date) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime tempDate = LocalDateTime.parse(date, oldFormat);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
String newDate = tempDate.format(newFormat);
return newDate;
}
更新中…