Java Date Parse getDateFromString(final String dateString, final String dateFormatString)

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

Description

Takes a date in string format and a date format string and returns a Date

License

Open Source License

Parameter

Parameter Description
dateString - the date in a string representation
dateFormatString - the format of how the date is represented in the dateString argument

Return

Date - returns a date object as specified by the dateString argument

Declaration

public static Date getDateFromString(final String dateString, final String dateFormatString) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    /**/* w w  w  .  j a v a  2 s.  c o  m*/
     * Takes a date in string format and a date format string and returns
     * a Date
     *
     * @param dateString       - the date in a string representation
     * @param dateFormatString - the format of how the date is represented in the
     *                         dateString argument
     * @return Date - returns a date object as specified by the dateString argument
     */
    public static Date getDateFromString(final String dateString, final String dateFormatString) {
        try {
            return parseExact(dateString, dateFormatString);
        } catch (final ParseException e) {
            throw new RuntimeException("Date could not be parsed in the specified format: " + dateFormatString
                    + " - " + e.getMessage());
        }
    }

    /**
     * Given the format value parse the date time from the string.
     *
     * @param value
     * @param format
     * @return Date
     * @throws ParseException
     */
    public static Date parseExact(final String value, final String format) throws ParseException {
        final DateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        return formatter.parse(value);
    }
}

Related

  1. getDateFromRFC822String(Object rfc822)
  2. getDateFromStr(DateFormat format, String s)
  3. getDateFromStr(String date)
  4. getDateFromStr(String inputDate, String inputFormatStr)
  5. getDateFromString(final String dateStr, final String pattern)
  6. getDateFromString(final String inputStringDate, final String format)
  7. getDateFromString(String _date, String _format)
  8. getDateFromString(String date)
  9. getDateFromString(String date, String fmt)