Here you can find the source of toSqlDate(String dateStr, String format)
public static java.sql.Date toSqlDate(String dateStr, String format) throws ParseException
//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; } }