目前我知道的
需要支持如下时间格式
2024/3/6 9:04
2024/3/6 9:04:00
26 Mar 2024 3:34
4/18/2024 10:03
4/18/2024 10:03:00
String SampleDateRegEx = "\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}(:\\d{2})?|\\s*\\d{1,2}\\s+[A-Za-z]{3}\\s+\\d{4}\\s+\\d{1,2}:\\d{2}\\s*|\\d{1,2}/\\d{1,2}/\\d{4} \\d{1,2}:\\d{1,2}(:\\d{2})?";
// 26 Mar 2024 3:34 8 Mar 2024 14:49 去空格,返回 2024/3/6 15:04:00
private static String preprocessDate(String dateStr) {
// 移除一到多个空格为单一空格
String cleanedDateStr = dateStr.trim().replaceAll(" +", " ");
// 将字符串转换为日期对象
Locale locale = Locale.ENGLISH;
LocalDateTime date = null;
try {
DateTimeFormatter inputFormatterSingleDigitHour = DateTimeFormatter.ofPattern("d MMM yyyy H:mm", locale);
date = LocalDateTime.parse(cleanedDateStr, inputFormatterSingleDigitHour);
} catch (DateTimeParseException e) {
DateTimeFormatter inputFormatterDoubleDigitHour = DateTimeFormatter.ofPattern("d MMM yyyy HH:mm", locale);
date = LocalDateTime.parse(cleanedDateStr, inputFormatterDoubleDigitHour);
}
// 将日期对象转换回字符串的形式
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
// 将日期对象转换回字符串的形式
// DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd
// HH:mm:ss");
return date.format(outputFormatter);
}
// 2024/3/6 9:04:00
private static String preprocessNumberDate(String fileDate) throws ParseException {
SimpleDateFormat originalFormat = null;
String formatDate = null;
Date date = null ;
try {
originalFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// 使用原始格式器解析日期字符串得到Date对象
date = originalFormat.parse(fileDate);
formatDate = originalFormat.format(date);
}catch (ParseException e) {
originalFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// 使用原始格式器解析日期字符串得到Date对象
date = originalFormat.parse(fileDate);
formatDate = originalFormat.format(date);
}
return formatDate;
}
public static String convertToOracleDateFormat(String originalDateString) throws Exception {
Date date ;
// 创建一个用于将 Date 对象格式化为 Oracle 日期字符串的格式器
SimpleDateFormat oracleFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// 判断日期格式并解析
if (originalDateString.matches("\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}")) {
// 时间格式为 "yyyy/MM/dd HH:mm:ss"
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
date = originalFormat.parse(originalDateString);
} else if (originalDateString.matches("\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}")) {
// 时间格式为 "yyyy/MM/dd HH:mm"
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
date = originalFormat.parse(originalDateString);
} else if (originalDateString.matches("\\d{1,2}/\\d{1,2}/\\d{4} \\d{1,2}:\\d{1,2}:\\d{1,2}")) {
// 时间格式为 "MM/dd/yyyy HH:mm:ss"
SimpleDateFormat originalFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
date = originalFormat.parse(originalDateString);
} else if (originalDateString.matches("\\d{1,2}/\\d{1,2}/\\d{4} \\d{1,2}:\\d{1,2}")) {
// 时间格式为 "MM/dd/yyyy HH:mm"
SimpleDateFormat originalFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
date = originalFormat.parse(originalDateString);
} else {
throw new Exception("Invalid date format.");
}
return oracleFormat.format(date);
}