Here you can find the source of stringToDatetime(String str, String format)
public static Date stringToDatetime(String str, String format)
//package com.java2s; /******************************************************************************* * Copyright (c) 2005, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from ww w .j av a 2 s .com * IBM Corporation - initial API and implementation *******************************************************************************/ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static Date stringToDateTime(String str) { if (str == null || str.equals("")) return null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); return null; } } public static Date stringToDatetime(String str, String format) { if (str == null || str.equals("")) return null; SimpleDateFormat sdf = new SimpleDateFormat(format); try { return sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); return null; } } }