Java SQL Time From toTime(Object object, Object oDefault)

Here you can find the source of toTime(Object object, Object oDefault)

Description

to Time

License

Apache License

Declaration

public static java.util.Date toTime(Object object, Object oDefault) 

Method Source Code


//package com.java2s;
/*//from   www . ja v a 2s . co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Array;

import java.util.Calendar;
import java.util.Collection;

import java.util.Iterator;

import java.util.Map;

public class Main {
    public static java.util.Date toTime(Object object, Object oDefault) {
        if (object == null) {
            if (oDefault == null)
                return null;
            return toTime(oDefault, null);
        }
        if (object instanceof java.sql.Date) {
            return new java.util.Date(((java.sql.Date) object).getTime());
        }
        if (object instanceof java.sql.Timestamp) {
            return new java.util.Date(((java.sql.Timestamp) object).getTime());
        }
        if (object instanceof java.util.Date) {
            return (java.util.Date) object;
        }
        if (object instanceof java.util.Calendar) {
            return new java.util.Date(((java.util.Calendar) object).getTimeInMillis());
        }
        if (object instanceof Long) {
            return new java.util.Date(((Long) object).longValue());
        }
        if (object instanceof Number) {
            return intToTime(((Number) object).intValue());
        }
        if (object instanceof Collection) {
            return toTime(getFirst(object), oDefault);
        }
        if (object.getClass().isArray()) {
            return toTime(getFirst(object), oDefault);
        }
        if (object instanceof Map) {
            return toTime(oDefault, null);
        }
        String sValue = object.toString();
        if (sValue == null || sValue.length() == 0)
            return toTime(oDefault, null);
        return stringToTime(sValue);
    }

    public static java.util.Date intToTime(int iTime) {
        if (iTime < 0)
            return null;
        int iHH = 0;
        int iMM = 0;
        int iSS = 0;
        if (iTime < 10000) {
            iHH = iTime / 100;
            iMM = (iTime % 100);
        } else {
            iHH = iTime / 10000;
            iMM = (iTime % 10000) / 100;
            iSS = (iTime % 10000) % 100;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, iHH);
        calendar.set(Calendar.MINUTE, iMM);
        calendar.set(Calendar.SECOND, iSS);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Object getFirst(Object object) {
        if (object == null)
            return null;
        if (object instanceof Collection) {
            Collection collection = (Collection) object;
            Iterator iterator = collection.iterator();
            if (iterator.hasNext())
                return iterator.next();
        } else if (object.getClass().isArray()) {
            int length = Array.getLength(object);
            if (length == 0)
                return null;
            return Array.get(object, 0);
        }
        return null;
    }

    public static String toString(Object object, String sDefault) {
        if (object == null)
            return sDefault;
        if (object instanceof String) {
            return (String) object;
        }
        if (object instanceof java.util.Date) {
            return longDateToString(((java.util.Date) object).getTime(), false);
        }
        if (object instanceof java.util.Calendar) {
            return longDateToString(((java.util.Calendar) object).getTimeInMillis(), false);
        }
        if (object instanceof Collection) {
            return toString(getFirst(object), sDefault);
        }
        if (object instanceof Map) {
            return sDefault;
        }
        if (object instanceof byte[]) {
            return new String((byte[]) object);
        }
        if (object instanceof char[]) {
            return new String((char[]) object);
        }
        return object.toString();
    }

    public static String toString(Object object, String sDefault, int iMaxLength) {
        String sResult = toString(object, sDefault);
        if (sResult != null && iMaxLength > 0 && sResult.length() > iMaxLength) {
            return sResult.substring(0, iMaxLength);
        }
        return sResult;
    }

    public static java.util.Date stringToTime(String sTime) {
        if (sTime == null || sTime.length() == 0)
            return null;
        String sNorm = normalizeStringTime(sTime);
        if (sNorm == null || sNorm.length() < 4)
            return null;
        int iHH = 0;
        int iMM = 0;
        int iSS = 0;
        try {
            iHH = Integer.parseInt(sNorm.substring(0, 2));
        } catch (Throwable th) {
        }
        try {
            iMM = Integer.parseInt(sNorm.substring(2, 4));
        } catch (Throwable th) {
        }
        if (sNorm.length() > 5) {
            try {
                iSS = Integer.parseInt(sNorm.substring(4, 6));
            } catch (Throwable th) {
            }
        }
        Calendar calendar = Calendar.getInstance();
        if (sTime.length() > 10 || sNorm.equals("000000")) {
            String sDate = normalizeStringDate(sTime);
            if (sDate != null && sDate.length() > 0) {
                int iDate = 0;
                try {
                    iDate = Integer.parseInt(sDate);
                    int iYear = iDate / 10000;
                    int iMonth = (iDate % 10000) / 100;
                    int iDay = (iDate % 10000) % 100;
                    calendar.set(Calendar.YEAR, iYear);
                    calendar.set(Calendar.MONTH, iMonth - 1);
                    calendar.set(Calendar.DATE, iDay);
                } catch (Throwable th) {
                }
            }
        }
        calendar.set(Calendar.HOUR_OF_DAY, iHH);
        calendar.set(Calendar.MINUTE, iMM);
        calendar.set(Calendar.SECOND, iSS);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static String longDateToString(long lDateTime, boolean boAddTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(lDateTime);
        int iYear = cal.get(java.util.Calendar.YEAR);
        int iMonth = cal.get(java.util.Calendar.MONTH) + 1;
        int iDay = cal.get(java.util.Calendar.DATE);
        String sTime = "";
        if (boAddTime) {
            int iHH = cal.get(Calendar.HOUR_OF_DAY);
            int iMM = cal.get(Calendar.MINUTE);
            int iSS = cal.get(Calendar.SECOND);
            String sHH = iHH < 10 ? "0" + iHH : String.valueOf(iHH);
            String sMM = iMM < 10 ? "0" + iMM : String.valueOf(iMM);
            String sSS = iSS < 10 ? "0" + iSS : String.valueOf(iSS);
            sTime = " " + sHH + ":" + sMM + ":" + sSS;
        }
        String sMonth = iMonth < 10 ? "0" + iMonth : String.valueOf(iMonth);
        String sDay = iDay < 10 ? "0" + iDay : String.valueOf(iDay);
        return iYear + "-" + sMonth + "-" + sDay + sTime;
    }

    public static String normalizeStringTime(String sValue) {
        if (sValue == null)
            return "000000";
        sValue = sValue.trim();
        if (sValue.length() == 0)
            return "000000";
        int iFirstSep = sValue.indexOf(':');
        if (iFirstSep < 0) {
            iFirstSep = sValue.indexOf(',');
            if (iFirstSep <= 0) {
                iFirstSep = sValue.indexOf('.');
                if (iFirstSep <= 0) {
                    try {
                        Integer.parseInt(sValue);
                    } catch (Throwable th) {
                        return "000000";
                    }
                    if (sValue.length() > 6)
                        return sValue.substring(0, 6);
                    if (sValue.length() == 1)
                        sValue = "000" + sValue;
                    if (sValue.length() == 2)
                        sValue = "00" + sValue;
                    if (sValue.length() == 3)
                        sValue = "0" + sValue;
                    if (sValue.length() == 4)
                        sValue = sValue + "00";
                    if (sValue.length() == 5)
                        sValue = sValue + "0";
                    return sValue;
                }
            }
        }
        int iSecondSep = sValue.indexOf(':', iFirstSep + 1);
        if (iSecondSep < 0) {
            iSecondSep = sValue.indexOf(',', iFirstSep + 1);
            if (iSecondSep < 0) {
                iSecondSep = sValue.indexOf('.', iFirstSep + 1);
            }
        }
        String sHH = "";
        String sMM = "";
        String sSS = "";
        if (iFirstSep > 0) {
            int iBegin = 0;
            if (iFirstSep > 5)
                iBegin = iFirstSep - 2;
            sHH = sValue.substring(iBegin, iFirstSep).trim();
            if (iSecondSep > 0) {
                sMM = sValue.substring(iFirstSep + 1, iSecondSep).trim();
                sSS = sValue.substring(iSecondSep + 1).trim();
                if (sSS.length() > 2)
                    sSS = sSS.substring(0, 2);
            } else {
                sMM = sValue.substring(iFirstSep + 1).trim();
                sSS = "00";
                if (sMM.length() > 2)
                    sMM = sMM.substring(0, 2);
            }
        }
        // Check Hour
        if (sHH.length() == 0)
            sHH = "00";
        else if (sHH.length() == 1)
            sHH = "0" + sHH;
        int iHH = 0;
        try {
            iHH = Integer.parseInt(sHH);
        } catch (Throwable th) {
            return "000000";
        }
        if (iHH < 0 || iHH > 23)
            return "000000";
        // Check Minutes
        if (sMM.length() == 0)
            sMM = "00";
        else if (sMM.length() == 1)
            sMM = "0" + sMM;
        int iMM = 0;
        try {
            iMM = Integer.parseInt(sMM);
        } catch (Throwable th) {
            return "000000";
        }
        if (iMM < 0 || iMM > 59)
            return "000000";
        // Check Seconds
        if (sSS.length() == 0)
            sSS = "00";
        else if (sSS.length() == 1)
            sSS = "0" + sSS;
        int iSS = 0;
        try {
            iSS = Integer.parseInt(sSS);
        } catch (Throwable th) {
            return "000000";
        }
        if (iSS < 0 || iSS > 59)
            return "000000";
        return sHH + sMM + sSS;
    }

    public static String normalizeStringDate(String sValue) {
        return normalizeStringDate(sValue, false);
    }

    public static String normalizeStringDate(String sValue, boolean boExcludeNumber) {
        if (sValue == null)
            return null;
        sValue = sValue.trim();
        if (sValue.length() == 0)
            return null;
        if (sValue.length() >= 28 && Character.isLetter(sValue.charAt(0))) {
            // Ad es. Tue Jan 01 00:00:00 CET 2013
            String sYear = sValue.substring(sValue.length() - 4);
            try {
                Integer.parseInt(sYear);
            } catch (Throwable th) {
                return null;
            }
            String sMonth = getMonth(sValue.substring(4, 7));
            if (sMonth == null || sMonth.length() < 2)
                return null;
            String sDay = sValue.substring(8, 10);
            int iDay = 0;
            try {
                iDay = Integer.parseInt(sDay);
            } catch (Throwable th) {
                return null;
            }
            if (iDay < 1 || iDay > 31)
                return null;
            return sYear + sMonth + sDay;
        }
        int iFirstSep = sValue.indexOf('/');
        if (iFirstSep < 0) {
            iFirstSep = sValue.indexOf('-');
            if (iFirstSep <= 0) {
                iFirstSep = sValue.indexOf('.');
                if (iFirstSep <= 0) {
                    if (boExcludeNumber)
                        return null;
                    if (sValue.length() > 8) {
                        long lValue = 0;
                        try {
                            lValue = Long.parseLong(sValue);
                        } catch (Throwable th) {
                            return null;
                        }
                        Calendar cal = Calendar.getInstance();
                        cal.setTimeInMillis(lValue);
                        int iDate = cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100
                                + cal.get(Calendar.DAY_OF_MONTH);
                        return String.valueOf(iDate);
                    }
                    if (sValue.length() != 8)
                        return null;
                    try {
                        Integer.parseInt(sValue);
                    } catch (Throwable th) {
                        return null;
                    }
                    return sValue;
                }
            }
        }
        int iSecondSep = sValue.indexOf('/', iFirstSep + 1);
        if (iSecondSep < 0) {
            iSecondSep = sValue.indexOf('-', iFirstSep + 1);
            if (iSecondSep < 0) {
                iSecondSep = sValue.indexOf('.', iFirstSep + 1);
                if (iSecondSep < 0)
                    return null;
            }
        }
        String sDay = null;
        String sMonth = null;
        String sYear = null;
        if (iFirstSep >= 4) {
            // year - month - day
            sYear = sValue.substring(0, iFirstSep).trim();
            sMonth = sValue.substring(iFirstSep + 1, iSecondSep).trim();
            sDay = sValue.substring(iSecondSep + 1).trim();
            if (sDay.length() > 2)
                sDay = sDay.substring(0, 2);
        } else {
            // day - month - year
            sDay = sValue.substring(0, iFirstSep).trim();
            sMonth = sValue.substring(iFirstSep + 1, iSecondSep).trim();
            sYear = sValue.substring(iSecondSep + 1).trim();
            if (sYear.length() > 4)
                sYear = sYear.substring(0, 4);
        }
        // Check Day
        if (sDay.length() == 0)
            sDay = "01";
        else if (sDay.length() == 1)
            sDay = "0" + sDay;
        int iDay = 0;
        try {
            iDay = Integer.parseInt(sDay);
        } catch (Throwable th) {
            return null;
        }
        if (iDay < 1 || iDay > 31)
            return null;
        // Check Month
        if (sMonth.length() > 2 && Character.isLetter(sMonth.charAt(0))) {
            sMonth = getMonth(sMonth);
            if (sMonth == null)
                return null;
        }
        if (sMonth.length() == 0)
            sMonth = "01";
        else if (sMonth.length() == 1)
            sMonth = "0" + sMonth;
        int iMonth = 0;
        try {
            iMonth = Integer.parseInt(sMonth);
        } catch (Throwable th) {
            return null;
        }
        if (iMonth < 1 || iMonth > 12)
            return null;
        // Check Year
        int iYear = 0;
        try {
            iYear = Integer.parseInt(sYear);
        } catch (Throwable th) {
            return null;
        }
        if (iYear < 1000 || iYear > 9999)
            return null;
        return sYear + sMonth + sDay;
    }

    public static String getMonth(String sMonth) {
        String sMonthLC = sMonth.toLowerCase();
        if (sMonthLC.startsWith("jan") || sMonthLC.startsWith("ge"))
            return "01";
        if (sMonthLC.startsWith("f"))
            return "02";
        if (sMonthLC.startsWith("mar"))
            return "03";
        if (sMonthLC.startsWith("ap"))
            return "04";
        if (sMonthLC.startsWith("may") || sMonthLC.startsWith("mag"))
            return "05";
        if (sMonthLC.startsWith("jun") || sMonthLC.startsWith("gi"))
            return "06";
        if (sMonthLC.startsWith("jul") || sMonthLC.startsWith("lu"))
            return "07";
        if (sMonthLC.startsWith("au") || sMonthLC.startsWith("ag"))
            return "08";
        if (sMonthLC.startsWith("s"))
            return "09";
        if (sMonthLC.startsWith("o"))
            return "10";
        if (sMonthLC.startsWith("n"))
            return "11";
        if (sMonthLC.startsWith("d"))
            return "12";
        return null;
    }
}

Related

  1. toTime(final Object value)
  2. toTime(int value)
  3. toTime(java.util.GregorianCalendar cal)
  4. toTime(LocalTime localTime)
  5. toTime(LocalTime localTime)
  6. toTime(Object value)
  7. toTime(String format, String str)
  8. toTime(String str)
  9. toTime(String value)