Here you can find the source of parseStringToDate(String dateString, String sourceDateFormat, Locale locale)
Parameter | Description |
---|---|
dateString | - String which has to be parsed. |
sourceDateFormat | - String format of the date. |
locale | - Locale to be used. |
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. |
public static Date parseStringToDate(String dateString, String sourceDateFormat, Locale locale) throws ParseException
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; } }