package com.zx.test.util;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author zx
* @date
**/
@Slf4j
public class DateUtils {
/**
* 日期正则 日期格式校验 年月日
* @param str
* @return
*/
public static boolean dateCheck(String str) {
String patt="^[0-9]{4}年([1-9]|0[1-9]|1[0-2])月([1-9]|0[1-9]|[12][0-9]|3[01])日$";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
/**
* 日期正则 日期格式校验 年月
* @param str
* @return
*/
public static boolean monCheck(String str) {
String patt="^[0-9]{4}年([1-9]|0[1-9]|1[0-2])月$";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
/**
* 日期正则
* @param str
* @return
*/
public static boolean yeCheck(String str) {
if(!str.contains("年")){
str = str + "年";
}
String patt="^[0-9]{4}年$";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
/**
* 日期正则 日期格式校验 年月日时分
* @param str
* @return
*/
public static boolean timeCheck(String str) {
String patt="^[0-9]{4}年([1-9]|0[1-9]|1[0-2])月([1-9]|0[1-9]|[12][0-9]|3[01])日((1|0?)[0-9]|2[0-3])时((1|0?)[0-9]|[0-5][0-9])分$";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
/**
*
* @param str
* @return
*/
public static boolean slotCheck(String str) {
String[] split = str.split("-");
if(split.length != 2){
//抛出自定义异常
throw new QrCodeParamException("数组参数错误");
}
boolean check1 = monCheck(split[split.length - 2]);
boolean check2 = monCheck(split[split.length - 1]);
if(check1 && check2){
return true;
}
return false;
}
/**
* 日期格式化 例:2022-08-09 ---> 2022年08月09日
* @param str
* @return
*/
public static String formatDate(String str) {
String[] split = str.split("-");
if(split.length<3){
//抛出自定义异常
throw new QrCodeParamException("数组参数错误");
}
return split[split.length - 3] + "年" + split[split.length - 2] + "月" + split[split.length - 1] + "日";
}
/**
* 日期格式化 例:20220809 ---> 2022年08月09日
* @param str
* @return
*/
public static String formatDateFunction(String str) {
String substringYear = str.substring(0,4);
String substringMon = str.substring(4,6);
String substringDate = str.substring(6,8);
if(StrUtil.isBlank(substringYear) || StrUtil.isBlank(substringMon) ||StrUtil.isBlank(substringDate)){
//抛出自定义异常
throw new QrCodeParamException("数组参数错误");
}
return substringYear+"年"+substringMon+"月"+substringDate+"日";
}
/**
* 日期格式化 2022-08 ---> 2022年08日
* @param str
* @return
*/
public static String formatMonDate(String str) {
String[] split = str.split("-");
if(split.length<2){
//抛出自定义异常
throw new QrCodeParamException("数组参数错误");
}
return split[split.length - 2] + "年" + split[split.length - 1] + "月" ;
}
/**
* 日期格式化 二〇一二年三月 ---> 2012年3月
*/
public static String convertChineseDateToArabic(String chineseDateStr) {
try {
String[] chineseDigits = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", "o", "O", "廿", "卅"};
String[] arabicDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "0", "2", "3"};
for (int i = 0; i < chineseDigits.length; i++) {
chineseDateStr = chineseDateStr.replace(chineseDigits[i], arabicDigits[i]);
}
String[] strs = chineseDateStr.split("年");
String[] yr = strs[1].split("月");
String year = strs[0];
String mon = yr[0];
String date = "";
if (yr.length > 1) {
date = yr[1].replace("日", "");
}
if (mon.equals("十")) {
mon = "10";
} else if (mon.equals("十1") || mon.equals("十2")) {
mon = mon.replace("十", "1");
}
if (date.length() == 3) {
date = date.replace("十", "");
} else if ("十".equals(date)) {
date = date.replace("十", "10");
} else if ("2十".equals(date)) {
date = date.replace("十", "0");
} else if ("3十".equals(date)) {
date = date.replace("十", "0");
} else if (date.length() == 2) {
date = date.replace("十", "1");
}
mon = mon.length() == 1 ? 0 + mon : mon;
String str = "";
if (date.length() != 0) {
date = date.length() == 1 ? 0 + date : date;
str = year + "年" + mon + "月" + date + "日";
} else {
str = year + "年" + mon + "月";
}
return str;
}catch (Exception e){
e.printStackTrace();
return "";
}
}
/**
* 单个时间类型转换
* @param date
* @param formatOld
* @param formatNew
* @return
*/
public static String dateFormat(String date,String formatOld,String formatNew){
String word = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatOld);
SimpleDateFormat sdf2 = new SimpleDateFormat(formatNew);
try {
Date parse = sdf.parse(date);
return sdf2.format(parse);
} catch (ParseException e) {
return word;
}
}
/**
* 有效日期等类型转换
* 例:2020.01.01至2022.02.02
* @param date
* @param splitKey
* @param formatOld
* @param formatNew
* @return
*/
public static String dateFormat(String date,String splitKey,String formatOld,String formatNew){
String word = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatOld);
SimpleDateFormat sdf2 = new SimpleDateFormat(formatNew);
String[] split = date.split(splitKey);
if (split.length == 2){
for (int i=0;i<split.length;i++){
try {
Date parse = sdf.parse(split[i]);
split[i] = sdf2.format(parse);
} catch (ParseException e) {
split[i] = "";
}
}
word = split[0]+"-"+split[1];
}
return word;
}
/**
* 大写数字转小写
* @param dateStr
* @return
*/
public static String stringToNumberSmall(String dateStr) {
// 传参数的格式为: 二〇二三
String resultStr = "";
for (int i = 0; i < dateStr.length(); i++) {
switch (dateStr.charAt(i)) {
case '〇':
resultStr += "0";
break;
case '一':
resultStr += "1";
break;
case '二':
resultStr += "2";
break;
case '三':
resultStr += "3";
break;
case '四':
resultStr += "4";
break;
case '五':
resultStr += "5";
break;
case '六':
resultStr += "6";
break;
case '七':
resultStr += "7";
break;
case '八':
resultStr += "8";
break;
case '九':
resultStr += "9";
break;
default:
resultStr += dateStr.charAt(i);
}
}
return dateStr.length() == 1 ? "0" + resultStr : resultStr;
}
public static void main(String[] args) {
String date = "六月卅1日";
System.out.println(convertChineseDateToArabic(date));
//System.out.println(timeCheck("2017年3月1日13时60分"));
}
}
日期格式化工具类
最新推荐文章于 2024-07-18 16:29:57 发布