Java String to Date stringToDate(final String string, final String format)

Here you can find the source of stringToDate(final String string, final String format)

Description

parse the string to date

License

Apache License

Parameter

Parameter Description
string a parameter
format a parameter

Return

Date

Declaration

public static Date stringToDate(final String string, final String format) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**//from   w  ww. j ava2  s.  c o m
     * parse the string to date format:yyyy-MM-dd HH:mm:ss
     * 
     * @param string
     * @return Date
     */
    public static Date stringToDate(final String string) {
        return stringToDate(string, YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
    }

    /**
     * parse the string to date
     * 
     * @param string
     * @param format
     * @return Date
     */
    public static Date stringToDate(final String string, final String format) {
        return stringToDate(string, format, null);
    }

    /**
     * parse the string to date
     * 
     * @param string
     * @param format
     * @param locale
     * @return Date
     */
    public static Date stringToDate(final String string, final String format, final Locale locale) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format, locale == null ? Locale.getDefault() : locale);
        try {
            date = sdf.parse(string);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return date;
    }
}

Related

  1. stringDateToDate(String StrDate)
  2. stringFechaToDate(String fechaS)
  3. stringToDate(final String dateString)
  4. stringToDate(final String dateTimeStr)
  5. stringToDate(final String str, final String dateFormat)
  6. stringToDate(String data)
  7. StringToDate(String data)
  8. stringToDate(String date)
  9. stringToDate(String date)