Java SQL Date Get getDate(Object object)

Here you can find the source of getDate(Object object)

Description

get Date

License

Open Source License

Declaration

public static Date getDate(Object object) 

Method Source Code


//package com.java2s;
/*//ww w .j av  a2  s . com
 * Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software;Designed and Developed mainly by many Chinese 
 * opensource volunteers. you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License version 2 only, as published by the
 * Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Any questions about this component can be directed to it's project Web address 
 * https://code.google.com/p/opencloudb/.
 *
 */

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static Date getDate(Object object) {
        Date date = null;

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
            if (object instanceof String) {
                try {
                    date = format.parse((String) object);
                } catch (Exception e) {
                    try {
                        date = format2.parse((String) object);
                    } catch (Exception ee) {
                        date = format3.parse((String) object);
                    }
                }
            } else if (object instanceof java.util.Date) {
                date = (Date) object;
            } else if (object instanceof Timestamp) {
                date = (Date) object;
            } else {
                date = new java.util.Date();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
}

Related

  1. getDate(Date date)
  2. getDate(Date value)
  3. getDate(final int year, final int month, final int day)
  4. getDate(final String value)
  5. getDate(Map m, String key)
  6. getDate(Object value)
  7. getDate(Object value, int columnType)
  8. getDate(ResultSet aResultSet, String aColumnName)
  9. getDate(ResultSet res, String name)