Android Date String to Date Convert getDateFromString(String dateString, String dateFormat)

Here you can find the source of getDateFromString(String dateString, String dateFormat)

Description

Get the Date from String with custom format.

License

Open Source License

Parameter

Parameter Description
dateString a parameter
dateFormat a parameter

Exception

Parameter Description
ParseException an exception

Declaration

public static Date getDateFromString(String dateString,
        String dateFormat) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

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

public class Main {
    public static SimpleDateFormat yyyyMMddFormat = new SimpleDateFormat(
            "yyyy-MM-dd", Locale.ENGLISH);

    /**//ww  w . ja va2  s  . co  m
     * Get the Date from String with custom format. Default format is yyyy-MM-dd
     * 
     * @param dateString
     * @param dateFormat
     * @return
     * @throws ParseException
     */
    public static Date getDateFromString(String dateString,
            String dateFormat) throws ParseException {
        SimpleDateFormat formatter;
        if (dateFormat == null) {
            formatter = yyyyMMddFormat;
        } else {
            formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        }

        return formatter.parse(dateString);
    }
}

Related

  1. ParseDate(String str)
  2. ParseUTCDate(String str)
  3. formatToDate(String date)
  4. fromStringFR(String dateString)
  5. fromStringUS(String dateString)
  6. getDefaultDateTimeString(String date, Locale locale)
  7. getShortDateTimeString(String date, Locale locale)
  8. iso8601ToTimestamp(final String strDate)
  9. iso8601ToTimestamp(final String strDate)