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

StringtoDateString(long ms)
to Date String
return format(ms, "yyyy-MM-dd");
StringtoDateString(String fmt, Date date)
to Date String
return new SimpleDateFormat(fmt).format(date);
DatetoDateStyleBoth(String dateStr)
to Date Style Both
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    return sdf.parse(dateStr);
} catch (ParseException e) {
    e.printStackTrace();
return null;
DatetoDatetime(String sDate)
to Datetime
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    return sdf.parse(sDate);
} catch (ParseException e) {
    throw new RuntimeException(e);
StringtoDateTimeFormat(String input, String inputfmt, String outputfmt)
to Date Time Format
Date date = null;
SimpleDateFormat infmt = new SimpleDateFormat(inputfmt);
try {
    date = infmt.parse(input);
} catch (ParseException e) {
    e.printStackTrace();
SimpleDateFormat outFormat = new SimpleDateFormat(outputfmt);
...
StringtoDateTimeSSSString(long datetime)
to Date Time SSS String
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").format(new java.util.Date(datetime));
StringtoDateTimeString(java.util.Date date)
Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date.
if (date == null)
    return "";
String dateString = toDateString(date);
String timeString = toTimeString(date);
if (dateString != null && timeString != null) {
    return dateString + " " + timeString;
} else {
    return "";
...
StringtoDateTimeString(java.util.Date date)
Makes a combined data and time string in the format "MM/DD/YYYY HH:MM:SS" from a Date.
if (date == null)
    return "";
String dateString = toDateString(date);
String timeString = toTimeString(date);
if (dateString != null && timeString != null)
    return dateString + " " + timeString;
else
    return "";
...
DatetoDateWithFormatString(String date, String format)
Returns a Java representation of a date string.
if (date == null)
    return null;
try {
    return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
    throw new IllegalArgumentException(
            "Unable to parse date '" + date + "' using format string '" + format + "'", e);
DatetoDateWithTimezone(String dateTime, String pattern)
To date with timezone.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
date = sdf.parse(dateTime);
return date;