異常な日付を通常の日付にフォーマットする

/** 
 * 時刻を標準化する新しく追加された書式設定された時間クラスは、フロント デスクから渡された日付を実際に実行可能な日付に書式設定するのに適しています * たとえば、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 str Year = ""; 
    String strMonth = ""; 
    String strDay = ""; 
    String strHour = ""; 
    String strMinu = ""; 
    String strSec = ""; 
    inthourType = 12; / /12時間形式、24時間
    int yearType = 1; //1 は平年、2 は闰年 
        {うるう年の場合は 2
    を試してください
    { 
        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", ""); 
            //保持位数一致のため、去除
            24hourType = 24; 
        else 
        if (formatString.indexOf("HH12") >= 0) 
        { 
            int tempBeginPlace = formatString. 
            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。サブストリング(tempBeginPlace, temEndPlace);
        if (formatString.indexOf("SS") >= 0) 
        { 
            int tempBeginPlace = formatString.indexOf("SS") 
        ; 
            int temEndPlace = tempBeginPlace + 2; 
            strSec = _dateTime.substring(tempBeginPlace, temEndPlace); 
        } 

        //判断の有無は闰年
        if (!str Year.equals("")) 
        { 
            int intyear = Integer.parseInt(str Year); 
            //4 で割り切れるが、100 では割り切れない② 4 で割り切れ、400 で割り切れる
            if (int Year % 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.
                    || (strMonth.equals("07")) 
                    || (strMonth.equals("08")) 
                    || (strMonth.equals("10"))  
                    || (strMonth.equals("10"))
                    strDay = "31"; 
                    intDay = 31; 
            if ((strMonth.equals("02")) 
                    || (strMonth.equals("04")) 
                || (strMonth.equals(" 
            06 
                    ")) 
                    || (strMonth.equals("09")) 
                    || (strMonth. 
            等しい("11"))) { 
                if (intDay > 30) 
                { 
                    strDay = "30"; 
                    intDay = 30; 
                }
                if (strMonth.
                        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;  int 
                intSec = Integer.parseInt( 
            strSec 
                ); 
                if (intSec > 59) 
            //秒のフォーマット
            if (!strSec.eq​​uals("")) 
            { 
                { 
                    strSec = "59"; 
                    intSec = 59; 
            returnValue = stryear + strMonth + 
                strDay + 
        strHour 
        + strMinu + strSec; 
        戻り値を返します。
    } 
    catch (例外 e) 
    {
        スロー e; 
    } 
}

おすすめ

転載: blog.csdn.net/qq_40390762/article/details/131300658