Skip to content

Java 工具类集合

public class StringUtils {
public static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static String defaultIfEmpty(String str, String defaultValue) {
return isEmpty(str) ? defaultValue : str;
}
public static String truncate(String str, int maxLength) {
if (isEmpty(str) || str.length() <= maxLength) {
return str;
}
return str.substring(0, maxLength) + "...";
}
}
public class DateUtils {
private static final DateTimeFormatter DEFAULT_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static String format(LocalDateTime dateTime) {
return dateTime.format(DEFAULT_FORMATTER);
}
public static String format(LocalDateTime dateTime, String pattern) {
return dateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public static LocalDateTime parse(String dateStr) {
return LocalDateTime.parse(dateStr, DEFAULT_FORMATTER);
}
public static LocalDateTime parse(String dateStr, String pattern) {
return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
}
public static long toTimestamp(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static LocalDateTime fromTimestamp(long timestamp) {
return LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault()
);
}
}
public class CollectionUtils {
public static <T> boolean isEmpty(Collection<T> collection) {
return collection == null || collection.isEmpty();
}
public static <T> boolean isNotEmpty(Collection<T> collection) {
return !isEmpty(collection);
}
public static <T> List<T> emptyIfNull(List<T> list) {
return list == null ? Collections.emptyList() : list;
}
public static <T> List<T> distinct(List<T> list) {
return list.stream().distinct().collect(Collectors.toList());
}
public static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {
return list.stream().map(mapper).collect(Collectors.toList());
}
}
public class JsonUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new JavaTimeModule());
}
public static String toJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON serialization failed", e);
}
}
public static <T> T fromJson(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON deserialization failed", e);
}
}
public static <T> T fromJson(String json, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(json, typeReference);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON deserialization failed", e);
}
}
}