使用DateTimeFormatter格式化输入的日期时间

时间:2025-04-01 08:11:17
package hrkj.chapter7.dateTimeFormatter.Test1; import java.time.LocalDateTime; import java.time.MonthDay; import java.time.Year; import java.time.format.DateTimeFormatter; import java.util.Arrays; /** * 对输入的日期时间进行处理 <br> * 2020年1月9日下午8:08:45 * * @author wcf * @version 1.0 */ public class DateTimeFormatterTools { /** * 年月日 时分 */ private static final String YEAR_MONTH_DAY_TIME = Pattern.YEAR_MONTH_DAY_TIME.getStr(); /** * 月日 时分 */ private static final String MONTH_DAY_TIME = Pattern.MONTH_DAY_TIME.getStr(); /** * 星期 时分 */ private static final String WEEK_TIME = Pattern.WEEK_TIME.getStr(); /** * 上下午 时分 */ private static final String TIME = Pattern.TIME.getStr(); /** * 昨天 时分 */ private static final String YESTERDAY = Pattern.YESTERDAY.getStr(); /** * 明天 时分 */ private static final String TOMORROW = Pattern.TOMORROW.getStr(); /** * 当前年 */ private static int currentYear = Year.now().getValue(); /** * 当前月 */ private static int currentMonth = MonthDay.now().getMonthValue(); /** * 当前日 */ private static int currentDay = MonthDay.now().getDayOfMonth(); /** * 大月 */ private static int[] bigMonth = { 1, 3, 5, 7, 8, 10, 12 }; /** * 小月 */ private static int[] smallMonth = { 4, 6, 9, 11 }; /** * 私有构造器 */ private DateTimeFormatterTools() { // 私有构造器,无法实例化 } /** * 处理输入的日期时间 * * @param str 输入的日期时间 */ public static void format(String str) { // 将日期和时间用空格进行分割 String[] datetime = str.split(" "); // 分割成的日期 String date = datetime[0]; // 分割成的时间 String time = datetime[1]; // 日期分割方式 String splitter = ""; // 日期可以用- . / 进行分割 // 如果包含了-./这三种中的一种,则用这些进行分割 if (date.contains(".")) { splitter = "\\."; } else if (date.contains("-")) { splitter = "-"; } else if (date.contains("/")) { splitter = "/"; } // 使用日期的分割方式对日期进行分割 String[] dateString = date.split(splitter); // 使用:对时间进行分割,时间只能用:进行分割 String[] timeString = time.split(":"); // 时间分割后的数组长度不是2则错误,因为输入的的时间只有时和分 if (timeString.length != 2) { // 时间错误 Hint.INVOKE_TIME.println(); return; } // 日期分割后的数组长度不是3则错误,因为输入的日期要有年,月和日 if (dateString.length != 3) { // 日期错误 Hint.INVOKE_DATE.println(); return; } // 输入的年 int year = Integer.valueOf(dateString[0]); // 输入的月 int month = Integer.valueOf(dateString[1]); // 输入的日 int day = Integer.valueOf(dateString[2]); // 输入的时 int hour = Integer.valueOf(timeString[0]); // 输入的分 int minute = Integer.valueOf(timeString[1]); // 对拆解判断过的字符串进行重新组合 String str1 = year + splitter + month + splitter + day + " " + hour + ":" + minute; // 对组合后的字符串进行解析 DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("y" + splitter + "M" + splitter + "d" + " H:m"); // 将字符串解析成日期时间对象 LocalDateTime parse = LocalDateTime.parse(str1, ofPattern); // 同一年 if (year == currentYear) { // 同一月 if (month == currentMonth) { // 同一天 if (day == currentDay) { // 今天 printDateTime(TIME, parse); } else if (day - currentDay == 1) { // 明天 printDateTime(TOMORROW, parse); } else if (day - currentDay == -1) { // 昨天 printDateTime(YESTERDAY, parse); } else if (day - currentDay >= -7 && day - currentDay <= -2) { // 向前一周以星期来表示 printDateTime(WEEK_TIME, parse); } else { // 不同天 printDateTime(MONTH_DAY_TIME, parse); } // 下个月 } else if (month - currentMonth == 1) { // 如果输入的日是1,则判断当前月和天 if (day == 1) { // 判断是大月小月还是二月,如果当前天数是月份最后一天,则输出明天 if (Arrays.binarySearch(bigMonth, currentMonth) >= 0 && currentDay == 31) { // 明天 printDateTime(TOMORROW, parse); return; } else if (Arrays.binarySearch(smallMonth, currentMonth) >= 0 && currentDay == 30) { // 明天 printDateTime(TOMORROW, parse); return; } else if (currentMonth == 2) { // 判断输入的是闰年还是平年 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (currentDay == 29) { // 明天 printDateTime(TOMORROW, parse); return; } } else { if (currentDay == 28) { // 明天 printDateTime(TOMORROW, parse); return; } } } else { // 使用月日进行输出 printDateTime(MONTH_DAY_TIME, parse); } } else { // 输入的日不是1,这输出月日时分 printDateTime(MONTH_DAY_TIME, parse); } // 上一月 } else if (month - currentMonth == -1) { // 如果当前日是1,则判断输入日是否是上月最后一天 if (currentDay == 1) { // 判断是大月小月还是二月,输入的天数是不是月份的最后一天,是则是昨天 if (Arrays.binarySearch(bigMonth, month) >= 0 && day == 31) { // 昨天 printDateTime(YESTERDAY, parse); return; } else if (Arrays.binarySearch(smallMonth, month) >= 0 && day == 30) { // 昨天 printDateTime(YESTERDAY, parse); return; } else if (month == 2) { // 判断是闰年还是平年 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (day == 29) { // 昨天 printDateTime(YESTERDAY, parse); return; } } else { if (day == 28) { // 昨天 printDateTime(YESTERDAY, parse); return; } } } } // 如果当前日不小于7,则输入月日时分,小于7则从当前天往前一周转换为星期 if (currentDay >= 7) { // 输出月日时分 printDateTime(MONTH_DAY_TIME, parse); // 如果当前天小于7,则当前天向前一周转换为星期 } else if (Arrays.binarySearch(bigMonth, month) >= 0 && 31 - day + currentDay < 7) { // 年月日转换为星期 printDateTime(WEEK_TIME, parse); } else if (Arrays.binarySearch(smallMonth, month) >= 0 && 30 - day + currentDay < 7) { // 年月日转换为星期 printDateTime(WEEK_TIME, parse); } else if (month == 2) { // 判断是闰年还是平年 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (29 - day + currentDay < 7) { // 年月日转换为星期 printDateTime(WEEK_TIME, parse); } else { // 如果向前超出了一周输出月日时分 printDateTime(MONTH_DAY_TIME, parse); } } else { if (28 - day + currentDay < 7) { // 年月日转换为星期 printDateTime(WEEK_TIME, parse); } else { // 如果向前超出了一周输出月日时分 printDateTime(MONTH_DAY_TIME, parse); } } } else { // 当前天向前超出了一周输出月日时分 printDateTime(MONTH_DAY_TIME, parse); } } else { // 不同月,输出月日时分 printDateTime(MONTH_DAY_TIME, parse); } } else { // 不同年,输出年月日时分 printDateTime(YEAR_MONTH_DAY_TIME, parse); } } /** * 格式化结果 * * @param pattern 模式字符串 * @param datetime 时间 */ private static void printDateTime(String pattern, LocalDateTime datetime) { // 通过模式字符串对时间进行格式化 DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(pattern); // 打印格式化后的时间 System.out.println("格式化结果:\n\t" + ofPattern.format(datetime)); } }