Commit 2cefbb03 by zhangxingmin

配置

parent a73bc14f
......@@ -59,5 +59,10 @@
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
</dependency>
</dependencies>
</project>
package com.yd.common.utils;
import com.ibm.icu.text.Transliterator;
/**
* 繁体字转简体中文工具类
* 使用 ICU4J 实现
*/
public class ChineseTextConverter {
private static final Transliterator TRADITIONAL_TO_SIMPLIFIED;
private static final Transliterator SIMPLIFIED_TO_TRADITIONAL;
static {
// 初始化转换器
TRADITIONAL_TO_SIMPLIFIED = Transliterator.getInstance("Traditional-Simplified");
SIMPLIFIED_TO_TRADITIONAL = Transliterator.getInstance("Simplified-Traditional");
}
/**
* 繁体转简体
* @param traditionalText
* @return
*/
public static String traditionalToSimplified(String traditionalText) {
if (traditionalText == null || traditionalText.trim().isEmpty()) {
return traditionalText;
}
try {
return TRADITIONAL_TO_SIMPLIFIED.transliterate(traditionalText);
} catch (Exception e) {
System.err.println("转换失败: " + e.getMessage());
return traditionalText;
}
}
/**
* 简体转繁体
* @param simplifiedText
* @return
*/
public static String simplifiedToTraditional(String simplifiedText) {
if (simplifiedText == null || simplifiedText.trim().isEmpty()) {
return simplifiedText;
}
try {
return SIMPLIFIED_TO_TRADITIONAL.transliterate(simplifiedText);
} catch (Exception e) {
System.err.println("转换失败: " + e.getMessage());
return simplifiedText;
}
}
public static void main(String[] args) {
System.out.println(traditionalToSimplified("大學或以上"));
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
......@@ -13,18 +14,22 @@ import java.util.Objects;
public class DateUtil {
public static String formatDate(Object date) {
if (date == null) return "";
if (date instanceof Date) {
return new SimpleDateFormat("yyyy/MM/dd").format((Date) date);
try {
if (date == null) return "";
if (date instanceof Date) {
return new SimpleDateFormat("yyyy/MM/dd").format((Date) date);
}
return date.toString();
}catch (Exception e) {
return "";
}
return date.toString();
}
public static Date parseDate(String dateStr) {
try {
return new SimpleDateFormat("yyyy/MM/dd").parse(dateStr);
} catch (ParseException e) {
return new Date();
} catch (Exception e) {
return null;
}
}
......@@ -34,12 +39,16 @@ public class DateUtil {
* @return
*/
public static String getyyyyMMdd(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
try {
if (Objects.isNull(localDateTime)) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
return localDateTime.format(formatter);
}catch (Exception e) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
return localDateTime.format(formatter);
}
/**
......@@ -48,12 +57,16 @@ public class DateUtil {
* @return
*/
public static String getHHmm(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
try {
if (Objects.isNull(localDateTime)) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return localDateTime.format(formatter);
}catch (Exception e) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return localDateTime.format(formatter);
}
/**
......@@ -62,12 +75,16 @@ public class DateUtil {
* @return
*/
public static String getyyyyMMddHHmm(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
try {
if (Objects.isNull(localDateTime)) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return localDateTime.format(formatter);
}catch (Exception e) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return localDateTime.format(formatter);
}
/**
......@@ -76,14 +93,40 @@ public class DateUtil {
* @return
*/
public static LocalDateTime getLocalDateTime(String timeStr) {
if (StringUtils.isBlank(timeStr)) {
try {
if (StringUtils.isBlank(timeStr)) {
return null;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
// 解析字符串为LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(timeStr, formatter);
return localDateTime;
}catch (Exception e) {
return null;
}
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
// 解析字符串为LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(timeStr, formatter);
return localDateTime;
/**
* yyyy/MM/dd 转成 LocalDateTime
* @param timeStr
* @return
*/
public static LocalDateTime getYMDLocalDateTime(String timeStr) {
try {
if (StringUtils.isBlank(timeStr)) {
return null;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.parse(timeStr, formatter);
return localDate.atStartOfDay(); // 转换为当天的开始时间 (00:00)
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
System.out.println(getHHmm(LocalDateTime.now()));
}
/**
......@@ -91,10 +134,14 @@ public class DateUtil {
* @return
*/
public static Date convertDateByLocalDateTime(LocalDateTime localDateTime){
//指定时区(如系统默认时区)
ZoneId zoneId = ZoneId.systemDefault();
//转换为 ZonedDateTime,再转为 Instant,最后生成 Date
Date date = Date.from(localDateTime.atZone(zoneId).toInstant());
return date;
try {
//指定时区(如系统默认时区)
ZoneId zoneId = ZoneId.systemDefault();
//转换为 ZonedDateTime,再转为 Instant,最后生成 Date
Date date = Date.from(localDateTime.atZone(zoneId).toInstant());
return date;
}catch (Exception e) {
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment