Java String to Date strToDate(String date)

Here you can find the source of strToDate(String date)

Description

str To Date

License

LGPL

Declaration

public static Date strToDate(String date) 

Method Source Code

//package com.java2s;
/**/*w w  w  . jav a  2s .c  o m*/
 * Core-level framework class: String and Date basic utility methods.
 * <br><br>
 * Encapsulates utility methods for everyday programming tasks
 * with Strings, Dates and other common stuff.
 * <br>
 * Creation date: 18/09/2003<br>
 * Last Update: 18/09/2003<br>
 * (c) 2003 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * @author Martin Cordova (some code written by Carlos Pineda)
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

public class Main {
    public static Date strToDate(String date) {
        if (date == null) {
            return null;
        }
        Date realDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date newDate = sdf.parse(date);
            String str = sdf.format(newDate);
            realDate = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return realDate;
    }

    public static String format(String pattern, Map<String, Object> arguments) {
        String formatedStr = pattern;
        for (String key : arguments.keySet()) {
            formatedStr = formatedStr.replaceAll("\\{:" + key + "\\}", arguments.get(key).toString());
        }
        return formatedStr;
    }
}

Related

  1. stringToDateWithFormat(String stringToConvert, String dateFormat)
  2. strMSShortToDate(String str_date)
  3. strToDate(final String str)
  4. strToDate(String _date, String format)
  5. strToDate(String date)
  6. strToDate(String date, String pattern)
  7. StrToDate(String dateFormat)
  8. strToDate(String dateInString)
  9. strToDate(String dateStr, String dateFormat)