Java SQL Date From toSqlDate(String date)

Here you can find the source of toSqlDate(String date)

Description

to Sql Date

License

Open Source License

Declaration

public static java.sql.Date toSqlDate(String date) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

public class Main {

    public static java.sql.Date toSqlDate(String date) {
        java.util.Date newDate = toDate(date, "00:00:00");

        if (newDate == null)
            return null;

        return new java.sql.Date(newDate.getTime());
    }/*from   w ww  . j a  v a  2s .  c  o m*/

    public static java.sql.Date toSqlDate(String yearStr, String monthStr, String dayStr) {
        java.util.Date newDate = toDate(yearStr, monthStr, dayStr, "0", "0", "0");

        if (newDate == null)
            return null;

        return new java.sql.Date(newDate.getTime());
    }

    public static java.sql.Date toSqlDate(int year, int month, int day) {
        java.util.Date newDate = toDate(year, month, day, 0, 0, 0);

        if (newDate == null)
            return null;

        return new java.sql.Date(newDate.getTime());
    }

    public static java.util.Date toDate(String dateTime) {
        int index = dateTime.indexOf(" ");
        String date = dateTime.substring(0, index);
        String time = dateTime.substring(index + 1);

        return toDate(date, time);
    }

    public static java.util.Date toDate(String date, String time) {
        if (date == null || time == null)
            return null;

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String year = date.substring(0, dateSlash1);
        String month = date.substring(dateSlash1 + 1, dateSlash2);
        String day = date.substring(dateSlash2 + 1);

        String hour = time.substring(0, timeColon1);
        String minute = time.substring(timeColon1 + 1, timeColon2);
        String second = time.substring(timeColon2 + 1);
        ;

        return toDate(year, month, day, hour, minute, second);
    }

    public static java.util.Date toDate(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }
        return toDate(year, month, day, hour, minute, second);
    }

    public static java.util.Date toDate(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();

        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return calendar.getTime();
    }

    public static java.util.Date toDate(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates).getTime();
    }

    public static Calendar toCalendar(String yearStr, String monthStr, String dayStr, String hourStr,
            String minuteStr, String secondStr) {
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }

        return toCalendar(year, month, day, hour, minute, second);
    }

    public static Calendar toCalendar(String datetime) {
        int index = datetime.indexOf(" ");
        String date = datetime.substring(0, index);
        String time = datetime.substring(index + 1);

        int dateSlash1 = date.indexOf("-");
        int dateSlash2 = date.lastIndexOf("-");

        if (dateSlash1 <= 0 || dateSlash1 == dateSlash2)
            return null;

        int timeColon1 = time.indexOf(":");
        int timeColon2 = time.lastIndexOf(":");

        if (timeColon1 <= 0 || timeColon1 == timeColon2)
            return null;

        String yearStr = date.substring(0, dateSlash1);
        String monthStr = date.substring(dateSlash1 + 1, dateSlash2);
        String dayStr = date.substring(dateSlash2 + 1);

        String hourStr = time.substring(0, timeColon1);
        String minuteStr = time.substring(timeColon1 + 1, timeColon2);
        String secondStr = time.substring(timeColon2 + 1);
        ;

        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(yearStr);
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
            Calendar calendar = Calendar.getInstance();

            calendar.set(year, month - 1, day, hour, minute, second);
            return calendar;
        } catch (Exception e) {
            return null;
        }
    }

    public static Calendar toCalendar(int[] dates) {
        if (dates == null || dates.length < 6)
            return null;

        return toCalendar(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]);
    }

    public static Calendar toCalendar(int year, int month, int day, int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);

        return c;
    }
}

Related

  1. toSQLDate(java.util.Date date)
  2. toSqlDate(java.util.Date date)
  3. toSQLDate(java.util.Date date)
  4. toSQLDate(Object object, Object oDefault)
  5. toSqlDate(String date)
  6. toSqlDate(String dateStr, String format)
  7. toSQLDate(String dt)
  8. toSQLDate(String sDate, String format)
  9. toSqlDateFromStrDate(String p_strDate)