/** * 新增的格式化时间类,将时间进行标准化格式,适用于将前台传入的日期格式化为实际可行的日期 * 如将20050600格式化为20050601,或将20050631格式化为20050630 * @param _dateTime 传入的原时间串 * @param _format 格式符,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小时制,24小时制 int yearType = 1; //1为平年,2为闰年 try { if (formatString.indexOf("YYYY") >= 0) { int tempBeginPlace = formatString.indexOf("YYYY"); int temEndPlace = tempBeginPlace + 4; strYear = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("MM") >= 0) { int tempBeginPlace = formatString.indexOf("MM"); int temEndPlace = tempBeginPlace + 2; strMonth = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("DD") >= 0) { int tempBeginPlace = formatString.indexOf("DD"); int temEndPlace = tempBeginPlace + 2; strDay = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("HH24") >= 0) { int tempBeginPlace = formatString.indexOf("HH24"); int temEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); formatString = formatString.replaceAll("24", ""); //为了保持位数一致,去除24 hourType = 24; } else if (formatString.indexOf("HH12") >= 0) { int tempBeginPlace = formatString.indexOf("HH12"); int temEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); formatString = formatString.replaceAll("12", ""); //为了保持位数一致,去除12 hourType = 12; } else if (formatString.indexOf("HH") >= 0) { int tempBeginPlace = formatString.indexOf("HH"); int temEndPlace = tempBeginPlace + 2; strHour = _dateTime.substring(tempBeginPlace, temEndPlace); hourType = 12; //如果未指定小时制,则默认为12小时制; } if (formatString.indexOf("MI") >= 0) { int tempBeginPlace = formatString.indexOf("MI"); int temEndPlace = tempBeginPlace + 2; strMinu = _dateTime.substring(tempBeginPlace, temEndPlace); } if (formatString.indexOf("SS") >= 0) { int tempBeginPlace = formatString.indexOf("SS"); int temEndPlace = tempBeginPlace + 2; strSec = _dateTime.substring(tempBeginPlace, temEndPlace); } //判断是否是闰年 if (!strYear.equals("")) { int intYear = Integer.parseInt(strYear); //能被4整除,但不能被100整除② 能被4整除,且能被400 if (intYear % 4 == 0) { if (intYear % 100 != 0) { yearType = 2; } } if (intYear % 4 == 0) { if (intYear % 400 == 0) { yearType = 2; } } } //格式化月 if (!strMonth.equals("")) { int intMonth = Integer.parseInt(strMonth); if (intMonth == 0) { strMonth = "01"; intMonth = 1; } if (intMonth > 12) { strMonth = "12"; intMonth = 12; } } //格式化日 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")) { //对2月的特别处理 if (yearType == 2) { if (intDay > 29) { strDay = "29"; intDay = 29; } } else { if (intDay > 28) { strDay = "28"; intDay = 28; } } } } //格式化小时 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"; } } } //格式化分 if (!strMinu.equals("")) { int intMinu = Integer.parseInt(strMinu); if (intMinu > 59) { strMinu = "59"; intMinu = 59; } } //格式化秒 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; } }
非正常日期格式化成正常的日期
猜你喜欢
转载自blog.csdn.net/qq_40390762/article/details/131300658
今日推荐
周排行