Java String to Date stringToDatetime(String str, String format)

Here you can find the source of stringToDatetime(String str, String format)

Description

string To Datetime

License

Open Source License

Declaration

public static Date stringToDatetime(String str, String format) 

Method Source Code

//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;
        }
    }
}

Related

  1. stringToDateForIng(String date_str)
  2. StringToDateFormat(String str, String format)
  3. stringToDateInTimeZone(String time, String timeZone)
  4. stringToDateMetaData(String dateString)
  5. stringToDateTemporalCoverage(String dateString)
  6. stringToDatetime(String string)
  7. stringToDateTime(String stringDate, String format)
  8. stringToDateWithFormat(String stringToConvert, String dateFormat)
  9. strMSShortToDate(String str_date)