[:ja]/**
* 指定されたパターン文字列の文字列を Date オブジェクトにして返します。
* Date オブジェクトとして有効でない場合は null を返します。
*
* @param value 日付を表す文字列
* @param format 日付を表す文字列のパターン書式 (yyyy/MM/dd など)
* @return 日付を表す文字列の Date オブジェクト
*/
public static Date toDate(String value, String format) {
if ( value == null || value.equals(“”) ){
return null;
}
if ( format == null || format.equals(“”) ){
format = “yyyy’-‘MM’-‘dd”;
}
// 日付フォーマットを作成
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
// 日付の厳密チェックを指定
dateFormat.setLenient(false);
try {
//パース java.util.Date
java.util.Date newDate = dateFormat.parse(value);
//java.sql.Dateに変換
Calendar cal = Calendar.getInstance();
cal.setTime(newDate);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
java.sql.Date newSqlDate = new java.sql.Date(cal.getTimeInMillis());
return newSqlDate;
} catch ( ParseException e ) {
e.printStackTrace();
// 日付値なしを返す
return null;
} finally {
dateFormat = null;
}
}[:]