Java SQL Date From toSQLDate(String sDate, String format)

Here you can find the source of toSQLDate(String sDate, String format)

Description

to SQL Date

License

Open Source License

Declaration

public static java.sql.Date toSQLDate(String sDate, String format) 

Method Source Code


//package com.java2s;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static java.sql.Date toSQLDate(String sDate) {
        return toSQLDate(sDate, "dd/MM/yyyy");
    }//from w  w  w.ja v  a2 s.  c o m

    public static java.sql.Date toSQLDate(String sDate, String format) {
        java.sql.Date sqlDate = null;
        Date utilDate = null;
        if (sDate != null) {
            utilDate = toUtilDate(sDate, format);
            sqlDate = toSQLDate(utilDate);
        }
        return sqlDate;
    }

    public static java.sql.Date toSQLDate(Date utilDate) {
        java.sql.Date sqlDate = null;
        sqlDate = new java.sql.Date(utilDate.getTime());
        return sqlDate;
    }

    public static java.sql.Date toSQLDate(Timestamp timeStampDate) {
        java.sql.Date sqlDate = null;
        sqlDate = new java.sql.Date(
                new GregorianCalendar(timeStampDate.getYear(), timeStampDate.getMonth(), timeStampDate.getDate())
                        .getTime().getTime());

        return sqlDate;
    }

    public static Date toUtilDate(String sDate) {
        return toUtilDate(sDate, "dd/MM/yyyy");
    }

    public static Date toUtilDate(String sDate, String format) {
        Date utilDate = null;
        try {
            SimpleDateFormat tempDate = new SimpleDateFormat(format);
            utilDate = tempDate.parse(sDate);
        } catch (ParseException e) {
            System.out.println("Found error exception while toUtilDate.\n" + e);
        }
        return utilDate;
    }

    public static Date toUtilDate(java.sql.Date sqlDate) {
        Date utilDate = null;
        utilDate = (Date) sqlDate;
        return utilDate;
    }

    public static Date toUtilDate(Timestamp timeStampDate) {
        Date utilDate = null;
        GregorianCalendar gc = new GregorianCalendar(timeStampDate.getYear(), timeStampDate.getMonth(),
                timeStampDate.getDate());
        utilDate = gc.getTime();
        return utilDate;
    }
}

Related

  1. toSQLDate(Object object, Object oDefault)
  2. toSqlDate(String date)
  3. toSqlDate(String date)
  4. toSqlDate(String dateStr, String format)
  5. toSQLDate(String dt)
  6. toSqlDateFromStrDate(String p_strDate)
  7. toSqlDateJdbcEscape(final String str)
  8. toSqlDateString(java.sql.Date d, String pattern)