Java SQL Date From toSqlDate(String dateStr, String format)

Here you can find the source of toSqlDate(String dateStr, String format)

Description

to Sql Date

License

Open Source License

Declaration

public static java.sql.Date toSqlDate(String dateStr, String format) throws ParseException 

Method Source Code

//package com.java2s;
/**//w ww  . j av  a 2  s .  c  om
 * Copyright (c) 2014 Xiufeng Liu ( xiufeng.liu@uwaterloo.ca )
 * <p/>
 * This file is free software: you may copy, redistribute and/or modify it
 * under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * <p/>
 * This file 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 for more details.
 * <p/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static java.sql.Date toSqlDate(String dateStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        dateFormat.setLenient(false);
        Date date = dateFormat.parse(dateStr);
        return new java.sql.Date(date.getTime());
    }

    public static java.sql.Date toSqlDate(String dateStr, String format) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(false);
        Date date = dateFormat.parse(dateStr);
        return new java.sql.Date(date.getTime());
    }

    public static java.sql.Date toSqlDate(long val) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = df.format(new Date(val));
        return toSqlDate(dateStr, "yyyy-MM-dd");
    }

    public static long getTime(String dateStr, int hour, String timeZone) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
            Date date = simpleDateFormat.parse(dateStr + " " + hour + ":00:00");
            return date.getTime();
        } catch (ParseException ex) {
            System.out.println("Exception " + ex);
        }
        return 0L;
    }

    public static long getTime(String timestamp, String timeZone) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
            Date date = simpleDateFormat.parse(timestamp);
            return date.getTime();
        } catch (ParseException ex) {
            System.out.println("Exception " + ex);
        }
        return 0L;
    }
}

Related

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