web123456

Formatting time with DateTimeFormatter

package hrkj; import java.time.LocalDateTime; import java.time.MonthDay; import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Arrays; /** * :: DateTime implementation class, using DateTimeFormatter implementation * * * * * * * * * @author Zhang Linchen<br>; * @date 2020/01/10 08:36:37 * @version 1.0 */ public class DateTimeTool { /** * Months with 31 days */ static int[] bigMonth = { 1, 3, 5, 7, 8, 10, 12 }; /** * :: Months with 30 days */ static int[] tinyMonth = { 4, 6, 9, 11 }; /** * :: Processing date and time * * @param str */ public static void dateTimeFormatter(String str) { // Determine the length of the date and time if (str.length() < 9 || str.length() > 16) { Hint.LENGTH_ILLEGAL.print(); return; } // Determine if the input date and time are separated by spaces. if (str.contains(" ")) { // Create an array to receive the split datetime String[] datetime = str.split(" "); // Get the date String[] date = splitDate(datetime[0]); // Determine the length of the date if (date.length != 3) { Hint.HINT_DATE.print(); return; } // Get year Integer y = Integer.valueOf(date[0]); // Get Months Integer M = Integer.valueOf(date[1]); // Get Day Integer d = Integer.valueOf(date[2]); // Determine if it is a leap year if (!handleDate(y, M, d)) { // If the month is greater than 12 or less than 1 if (M > 12 || M < 1) { Hint.HINT_MONTH.print(); return; // If the number of days in a big month is more than 31 or less than 1 } else if (Arrays.binarySearch(bigMonth, M) > -1 && (d <= 0 || d > 31)) { Hint.HINT_BIGMONTH.print(); return; // If the number of days in a small month is more than 30 or less than 1 } else if (Arrays.binarySearch(tinyMonth, M) > -1 && (d <= 0 || d > 30)) { Hint.HINT_TINYMONTH.print(); return; // If the number of February days in a normal year exceeds 28 or is less than 1 } else if (y % 4 != 0 && y % 100 != 0 && M == 2 && (d <= 0 || d > 28)) { Hint.HINT_TINY_TWOMONTH.print(); return; // If the number of February days in a normal year exceeds 28 or is less than 1 } else if (y % 400 != 0 && M == 2 && (d <= 0 || d > 28)) { Hint.HINT_TINY_TWOMONTH.print(); return; // If the number of days in February in a leap year exceeds 29 or is less than 1 } else if (y % 4 == 0 && y % 100 != 0 && M == 2 && (d <= 0 || d > 29)) { Hint.HINT_BIG_TWOMONTH.print(); return; // If the number of days in February in a leap year exceeds 29 or is less than 1 } else if (y % 400 == 0 && M == 2 && (d <= 0 || d > 29)) { Hint.HINT_BIG_TWOMONTH.print(); return; } else { return; } } // Getting the time String time = datetime[1]; // Determine if the split is regular boolean b = spiltTime(time); // If not split by regular if (!b) { Hint.HINT_TIME.print(); return; } else { // Perform date and time splicing String dateTime = y + "-" + M + "-" + d + " " + time; DateTimeFormatter ofPattern1 = DateTimeFormatter.ofPattern("y-M-d H:m"); LocalDateTime parse1 = LocalDateTime.parse(dateTime, ofPattern1); // (parse1); // Determine if it is the current year if (y == Year.now().getValue()) { // Determine if it's the current month if (M == MonthDay.now().getMonthValue()) { // Determine if it's today if (d == MonthDay.now().getDayOfMonth()) { printMessage("Today a H:m", parse1); // Determine if it's yesterday } else if (d - MonthDay.now().getDayOfMonth() == -1) { printMessage("Today a H:m", parse1); // Determine if it's tomorrow } else if (d - MonthDay.now().getDayOfMonth() == 1) { printMessage("Tomorrow a H:m", parse1); // Determine the day of the week } else if (d - MonthDay.now().getDayOfMonth() >= -7 && d - MonthDay.now().getDayOfMonth() <= -2) { printMessage("E a H:m", parse1); // In the current month, but not in the current week } else { printMessage("M-d a H:m", parse1); } // Other months of the current year } else { printMessage("M-d H:m", parse1); } // In different years } else { DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); System.out.println(parse1.format(dtf) + "Points."); } } } else { return; } } /** * Get time formatter and parse, print time. * * @param info pattern string * @param localDateTime LocalDateTime object */ private static void printMessage(String info, LocalDateTime localDateTime) { //Pass in the pattern string to get the DateTimeFormatter object. DateTimeFormatter ofPattern2 = DateTimeFormatter.ofPattern(info); //Parses the obtained DateTimeFormatter object with LocalDateTime object. System.out.println(localDateTime.format(ofPattern2)); } /** * :: Methods for determining large and small leap years * which determines the number of days in the big and small months in a flat and leap year * @param y year * @param m month * @param d day * @return true for a leap year, false for a flat year */ private static boolean handleDate(int y, int m, int d) { // In case of leap year February if (y % 4 == 0 && y % 100 != 0 && m == 2 && (d > 0 && d <= 29)) { return true; // In case of leap year February } else if (y % 400 == 0 && m == 2 && (d > 0 && d <= 29)) { return true; // Not a leap year, but in the case of February } else if (y % 4 != 0 && y % 400 != 0 && m == 2 && (d > 0 && d <= 28)) { // Not a leap year, 28 days in February. return true; // If it's not February, determine if it's a big month. } else if (Arrays.binarySearch(bigMonth, m) > -1 && (d > 0 && d <= 31)) { return true; // If it's not February, determine if it's a small month. } else if (Arrays.binarySearch(tinyMonth, m) > -1 && (d > 0 && d <= 30)) { return true; } return false; } /** * Use regular expressions to qualify time * * @param time The time to split * @return The result of the split */ private static boolean spiltTime(String time) { String t = "([01]?[0-9]{1}|[2][0-3]):[0-5]?[0-9]"; return time.matches(t) ? true : false; } /** * Use regular expressions to qualify dates * * @param date The date to split * @return Split date */ private static String[] splitDate(String date) { // Segmentation year and month String r = "[\\./-]{1}"; // Number of digits occurring in the month String s = "\\d{1,2}"; return date.matches("\\d+" + "(" + r + s + "){2}") ? date.split(r) : new String[0]; } }