/** * The newly added formatted time class, which standardizes the time format, is suitable for formatting the date passed in from the foreground into an actual and feasible date * For example, format 20050600 to 20050601, or format 20050631 to 20050630 * @param _dateTime The original time string passed in * @param _format format character, YYYYMMDDHH24MISS, YYYYMMDDHH12MISS * @return String * @throws Exception */ public static String formatDateTime(String _dateTime, String _format) throws Exception { String returnValue = ""; String formatString = _format.toUpperCase(); String strYear = ""; String strMonth = ""; String strDay = ""; String strHour = ""; String strMinu = ""; String strSec = ""; int hourType = 12; //12-hour format, 24-hour format int yearType = 1; //1 is normal year, 2 is leap year try { if (formatString.indexOf("YYYY") >= 0) { int tempBeginPlace = formatString.indexOf("YYYY"); int tempEndPlace = tempBeginPlace + 4; strYear = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("MM") >= 0) { int tempBeginPlace = formatString.indexOf("MM"); int tempEndPlace = tempBeginPlace + 2; strMonth = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("DD") >= 0) { int tempBeginPlace = formatString.indexOf("DD"); int tempEndPlace = tempBeginPlace + 2; strDay = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("HH24") >= 0) { int tempBeginPlace = formatString.indexOf("HH24"); int tempEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); formatString = formatString.replaceAll("24", ""); //In order to keep the number of digits consistent, remove 24 hourType = 24; } else if (formatString.indexOf("HH12") >= 0) { int tempBeginPlace = formatString.indexOf("HH12"); int tempEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); formatString = formatString.replaceAll("12", ""); //In order to keep the number of digits consistent, remove 12 hourType = 12; } else if (formatString.indexOf("HH") >= 0) { int tempBeginPlace = formatString.indexOf("HH"); int tempEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); hourType = 12; //If the hour is not specified, the default is 12 hours; } if (formatString.indexOf("MI") >= 0) { int tempBeginPlace = formatString.indexOf("MI"); int tempEndPlace = tempBeginPlace + 2; strMinu = _dateTime.substring(tempBeginPlace, tempEndPlace); } if (formatString.indexOf("SS") >= 0) { int tempBeginPlace = formatString.indexOf("SS"); int tempEndPlace = tempBeginPlace + 2; strSec = _dateTime.substring(tempBeginPlace, temEndPlace); } // Check if it is a leap year if (!strYear.equals("")) { int intYear = Integer.parseInt(strYear); // Divisible by 4, but not divisible by 100 ② Divisible by 4, and divisible by 400 if (intYear % 4 == 0) { if (intYear % 100 != 0) { yearType = 2; } } if (intYear % 4 == 0) { if (intYear % 400 == 0) { yearType = 2; } } } // format the month if (!strMonth.equals("")) { int intMonth = Integer.parseInt(strMonth); if (intMonth == 0) { strMonth = "01"; intMonth = 1; } if (intMonth > 12) { strMonth = "12"; intMonth = 12; } } // format date if (!strDay.equals("")) { int intDay = Integer.parseInt(strDay); if (intDay == 0) { strDay = "01"; intDay = 1; } if (intDay > 31) { strDay = "31"; intDay = 31; } if ((strMonth.equals("01")) || (strMonth.equals("03")) || (strMonth.equals("05")) || (strMonth.equals("07")) || (strMonth.equals("08")) || (strMonth.equals("10")) || (strMonth.equals("12"))) { if (intDay > 31) { strDay = "31"; intDay = 31; } } if ((strMonth.equals("02")) || (strMonth.equals("04")) || (strMonth.equals("06")) || (strMonth.equals("09")) || (strMonth.equals("11"))) { if (intDay > 30) { strDay = "30"; intDay = 30; } if (strMonth.equals("02")) { // Special treatment for February if (yearType == 2) { if (intDay > 29) { strDay = "29"; intDay = 29; } } else { if (intDay > 28) { strDay = "28"; intDay = 28; } } } } // format the hour if (!strHour.equals("")) { int intHour = Integer.parseInt(strHour); if (intHour > 24) { strHour = "24"; intHour = 24; } if (hourType == 12) { if (intHour == 0) { intHour = 1; strHour = "01"; } if (intHour > 12) { intHour = intHour - 12; strHour = "0" + intHour; } } else { if (intHour > 23) { intHour = 23; strHour = "23"; } } } // format points if (!strMinu.equals("")) { int intMinute = Integer.parseInt(strMinute); if (intMinu > 59) { strMinu = "59"; intMin = 59; } } // format seconds if (!strSec.equals("")) { int intSec = Integer.parseInt(strSec); if (intSec > 59) { strSec = "59"; intSec = 59; } } } returnValue = strYear + strMonth + strDay + strHour + strMinu + strSec; return returnValue; } catch (Exception e) { throw e; } }
format abnormal date into normal date
Guess you like
Origin blog.csdn.net/qq_40390762/article/details/131300658
Recommended
Ranking