Java Utililty Methods String to Date

List of utility methods to do String to Date

Description

The list of methods to do String to Date are organized into topic(s).

Method

DatetoDate(String date, String format)
Parses the given date according to format and returns it as java.util.Date instance.
if (date == null || format == null)
    throw new IllegalArgumentException("Neither date, not format can be null!");
DateFormat timeStampFormat = new SimpleDateFormat(format);
Date result = timeStampFormat.parse(date);
return result;
DatetoDate(String date, String format)
to Date
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 0);
c.set(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR, 12);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
...
DatetoDate(String dateStr)
This function is use to convert string date format to data format
return toDate(dateStr, null);
DatetoDate(String dateStr)
to Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
    return sdf.parse(dateStr);
} catch (Exception e) {
    return null;
DatetoDate(String dateStr, String format)
to Date
return getSimpleDateFormat(format).parse(dateStr);
DatetoDate(String dateStr, String pattern)
to Date
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
    return sdf.parse(dateStr);
} catch (ParseException e) {
    return null;
DatetoDate(String dateString)
Attempts to convert a String representation of a date to a java.util.Date object.
Date date = null;
DateFormat df = DateFormat.getDateInstance();
try {
    df = DateFormat.getDateInstance(DateFormat.SHORT);
    date = df.parse(dateString);
} catch (Exception e) {
    try {
        df = DateFormat.getDateInstance(DateFormat.MEDIUM);
...
DatetoDate(String dateString)
to Date
if (Strings.isNullOrEmpty(dateString)) {
    return null;
try {
    return DATE_FORMAT.parse(dateString);
} catch (ParseException e) {
    throw new RuntimeException(e);
DatetoDate(String dateString)
to Date
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format.parse(dateString);
DatetoDate(String dateString, Locale locale)
This method parses a @param String dateString to a Date
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
Date d = df.parse(dateString, new ParsePosition(0));
return d;