Java SQL Date Create castToSqlDate(Object value)

Here you can find the source of castToSqlDate(Object value)

Description

cast To Sql Date

License

Apache License

Declaration

public static final java.sql.Date castToSqlDate(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

public class Main {
    public static final java.sql.Date castToSqlDate(Object value) {
        if (value == null) {
            return null;
        }//from   w  ww.  ja v  a2s.com

        if (value instanceof Calendar) {
            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        }

        if (value instanceof java.sql.Date) {
            return (java.sql.Date) value;
        }

        if (value instanceof java.util.Date) {
            return new java.sql.Date(((java.util.Date) value).getTime());
        }

        long longValue = 0;

        if (value instanceof Number) {
            longValue = ((Number) value).longValue();
        }

        if (value instanceof String) {
            String strVal = (String) value;
            if (strVal.length() == 0) {
                return null;
            }

            longValue = Long.parseLong(strVal);
        }

        if (longValue <= 0) {
            throw new IllegalArgumentException("can not cast to Date, value : " + value);
        }

        return new java.sql.Date(longValue);
    }
}

Related

  1. asSqlDate(Date date)
  2. asSqlDate(Date date)
  3. asSqlDate(String date)
  4. beforeNow(final Date d)
  5. beginOfDay(Date date)
  6. convert(Date date)
  7. convertDate(java.sql.Date date)
  8. convertDateFromSQLDate(java.sql.Date sqlDate)
  9. convertDateFromSqlToUtil(java.sql.Date sqlDate)