Here you can find the source of getDateFromString(final String dateString, final String dateFormatString)
Parameter | Description |
---|---|
dateString | - the date in a string representation |
dateFormatString | - the format of how the date is represented in the dateString argument |
public static Date getDateFromString(final String dateString, final String dateFormatString)
//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); } }