Android Date String to Date Convert parseStringToDate(String dateString, String sourceDateFormat, Locale locale)

Here you can find the source of parseStringToDate(String dateString, String sourceDateFormat, Locale locale)

Description

Parse String to Date based on given date format using the provided Locale.

Parameter

Parameter Description
dateString - String which has to be parsed.
sourceDateFormat - String format of the date.
locale - Locale to be used.

Exception

Parameter Description
ParseException exception when parsing the date.
NullPointerException if any of the parameters is null.
IllegalArgumentException if string parameter are blank or 0 - length.

Return

- parsed date.

Declaration

public static Date parseStringToDate(String dateString,
        String sourceDateFormat, Locale locale) throws ParseException 

Method Source Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{
    /***/*from   w  w  w  . j av  a  2  s  .  c o  m*/
     * Parse {@link String} to {@link Date} based on given date format using the provided Locale.
     * 
     * @param dateString -{@link String} which has to be parsed.
     * @param sourceDateFormat -{@link String} format of the date.
     * @param locale -{@link Locale} to be used.
     * @return -{@link Date} parsed date.
     * @throws ParseException exception when parsing the date.
     * @throws NullPointerException if any of the parameters is null.
     * @throws IllegalArgumentException if string parameter are blank or 0 - length.
     */
    public static Date parseStringToDate(String dateString,
            String sourceDateFormat, Locale locale) throws ParseException {
        if (StringUtils.isNull(dateString)) {
            throw new NullPointerException("dateString cannot be null");
        }
        if (StringUtils.isEmpty(dateString)
                || StringUtils.isBlank(dateString)) {
            throw new IllegalArgumentException("dateString cannot be empty");
        }
        if (StringUtils.isNull(sourceDateFormat)) {
            throw new NullPointerException(
                    "sourceDateFormat cannot be null");
        }
        if (StringUtils.isEmpty(sourceDateFormat)
                || StringUtils.isBlank(sourceDateFormat)) {
            throw new IllegalArgumentException(
                    "sourceDateFormat cannot be empty");
        }
        if (StringUtils.isNull(locale)) {
            throw new NullPointerException("locale cannot be null");
        }

        Date parsedDate = null;
        SimpleDateFormat originalFormat = new SimpleDateFormat(
                sourceDateFormat, locale);
        parsedDate = originalFormat.parse(dateString);
        return parsedDate;
    }
}

Related

  1. parseDateTime(String date)
  2. parseTaskWarrior(String date)
  3. getDate(String sDate, String dateFormat)
  4. convertStringToDate(String dateString, String currentFormat)
  5. dateToString(Date date, String format)
  6. parse2Date(SimpleDateFormat s, Object d)
  7. stringToDate(String src, String format)
  8. toDateFromLongFormat(String date)
  9. parse(String aFormat, String date)