Here you can find the source of toIntDate(Object object, int iDefault)
public static int toIntDate(Object object, int iDefault)
//package com.java2s; /*/*from w w w . ja v a2s . c o 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.Date; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.Map; public class Main { public static int toIntDate(Object object, int iDefault) { Date date = toDate(object, null); if (date == null) return iDefault; return longDateToInt(date.getTime()); } public static java.util.Date toDate(int iValue, int iDefault) { if (iValue < 10000) { if (iDefault < 10000) return null; return toDate(iDefault, 0); } return new java.util.Date(intToLongDate(iValue)); } public static java.util.Date toDate(Object object, Object oDefault) { if (object == null) { if (oDefault == null) return null; return toDate(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) { int iDate = ((Number) object).intValue(); if (iDate < 10000) return toDate(oDefault, null); return new java.util.Date(intToLongDate(iDate)); } if (object instanceof Collection) { return toDate(getFirst(object), oDefault); } if (object.getClass().isArray()) { return toDate(getFirst(object), oDefault); } if (object instanceof Map) { return toDate(oDefault, null); } String sValue = object.toString(); if (sValue == null || sValue.length() == 0) return toDate(oDefault, null); String sDate = normalizeStringDate(sValue); if (sDate == null) return toDate(oDefault, null); return new java.util.Date(stringToLongDate(sDate)); } public static int longDateToInt(long lDate) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(lDate); return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); } public static long intToLongDate(int iDate) { if (iDate < 10000) return System.currentTimeMillis(); int iYear = iDate / 10000; int iMonth = (iDate % 10000) / 100; int iDay = (iDate % 10000) % 100; Calendar calendar = new GregorianCalendar(iYear, iMonth - 1, iDay); return calendar.getTimeInMillis(); } 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 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 long stringToLongDate(String sDate) { if (sDate == null) return System.currentTimeMillis(); int iDate = 0; try { iDate = Integer.parseInt(sDate); } catch (Exception ex) { return System.currentTimeMillis(); } int iYear = iDate / 10000; int iMonth = (iDate % 10000) / 100; int iDay = (iDate % 10000) % 100; Calendar calendar = new GregorianCalendar(iYear, iMonth - 1, iDay); return calendar.getTimeInMillis(); } 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 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; } }