Java Utililty Methods Date from String

List of utility methods to do Date from String

Description

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

Method

DateconvertToDate(final String str, final DateFormat dateFormat)
convert To Date
try {
    return dateFormat.parse(str);
} catch (ParseException e) {
    return null;
DateconvertToDate(String date)
convert To Date
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
if (date != null && !date.isEmpty()) {
    return sdf.parse(date);
return null;
DateconvertToDate(String dateAsString)
Convert a String into a date
Date utilDate = null;
if (isDatePopulated(dateAsString)) {
    DateFormat df = DateFormat.getInstance();
    df.setLenient(false);
    SimpleDateFormat sf = (SimpleDateFormat) df;
    sf.applyPattern("dd/MM/yyyy");
    utilDate = sf.parse(dateAsString);
return utilDate;
DateconvertToDate(String dateString)
convert To Date
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
Date date = sdf.parse(dateString);
return date;
DateconvertToDate(String dateString, String format)
convert To Date
if (dateString == null || dateString.length() == 0)
    return null;
SimpleDateFormat sdf;
boolean defaultFlag = false;
if (format != null && format.length() > 0) {
    sdf = new SimpleDateFormat(format);
} else {
    sdf = new SimpleDateFormat("MM/dd/yyyy");
...
DateconvertToDate(String dateString, String pattern)
concerts the string format into a Date
if (dateString == null || dateString.trim().isEmpty()) {
    return null;
if (pattern == null || pattern.isEmpty()) {
    throw new Exception("convertToDate failed: pattern cannot be null or empty");
try {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
...
DateconvertToDate(String ddMMyy)
convert to date
SimpleDateFormat format = new SimpleDateFormat("ddMMyy");
format.setLenient(false);
Date date = null;
date = format.parse(ddMMyy);
return date;
DateconvertToDate(String inDate)
convert To Date
Date date = null;
for (SimpleDateFormat format : dateFormats) {
    format.setLenient(false);
    try {
        if (inDate.length() < 12 && format.toPattern().length() < 12) {
            date = format.parse(inDate.trim());
        } else if (inDate.length() > 12 && format.toPattern().length() > 12) {
            date = format.parse(inDate.trim());
...
DateconvertToDate(String input)
Convert String with various formats into java.util.Date
Date date = null;
if (null == input) {
    return null;
for (SimpleDateFormat format : dateFormats) {
    try {
        format.setLenient(false);
        date = format.parse(input);
...
StringconvertToDate(String str)
convert To Date
if (str == null)
    return null;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(sdf1.parse(str));