Java SQL Date Parse getSqlDateFromStr(DateFormat format, String s, boolean need_default)

Here you can find the source of getSqlDateFromStr(DateFormat format, String s, boolean need_default)

Description

get Sql Date From Str

License

Open Source License

Declaration

public static java.sql.Date getSqlDateFromStr(DateFormat format, String s, boolean need_default) 

Method Source Code

//package com.java2s;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public final static DateFormat format_yyyyMMdd_HHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static DateFormat format_yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
    public final static DateFormat format13 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static java.sql.Date getSqlDateFromStr(DateFormat format, String s, boolean need_default) {
        Date utilDate = getDateFromStr(format, s);
        return utilDate_to_sqlDate(utilDate, need_default);
    }/* w  ww  .  j  a va  2  s. c o  m*/

    public static Date getDateFromStr(DateFormat format, String s) {
        if (s == null || s.trim().equals("")) {
            return null;
        }
        if (format == null) {
            if (s.length() == 10) {
                format = format_yyyyMMdd;
            } else if (s.length() == 16) {
                format = format13;
            } else {
                format = format_yyyyMMdd_HHmmss;
            }
        }
        return parseDateStringToDate(format, s);
    }

    public static java.sql.Date utilDate_to_sqlDate(Date utilDate, boolean need_default) {
        if (utilDate == null) {
            if (need_default) {
                return new java.sql.Date(0);// 1970-01-01
            }
            return null;
        }
        return new java.sql.Date(utilDate.getTime());
    }

    public static Date parseDateStringToDate(DateFormat format, String dateString) {
        if (format != null) {
            synchronized (format) {
                try {
                    return format.parse(dateString);
                } catch (ParseException e) {
                    return null;
                }
            }
        } else {
            // String message = "format is null,the dataString is " +
            // dateString;
            return null;
        }

    }

    public static Date parseDateStringToDate(DateFormat format, String time, TimeZone timeZone) {
        synchronized (format) {
            try {
                format.setTimeZone(timeZone);
                return format.parse(time);
            } catch (ParseException e) {
                return null;
            }
        }
    }
}

Related

  1. getSQLDate(String dateString)
  2. getSqlDate(String dateString, String format)
  3. getSqlDate(String str)
  4. getSqlDateByShortStr(String dateStr)
  5. getSQLDateFromStr(String s)
  6. getSqlDateFromString(String dateAsString, String format)
  7. parse(String date)
  8. parseDate(final String dateString)