Java Utililty Methods Date Parse

List of utility methods to do Date Parse

Description

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

Method

DatedateParser(String param)
date Parser
Date date = null;
try {
    date = dateFormatter.parse(param);
} catch (ParseException ex) {
return date;
StringdateParseShortString(Date date)
date Parse Short String
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
DatedateStringToDate(String dateString, String DateStringType)
date String To Date
SimpleDateFormat formatter = new SimpleDateFormat(DateStringType);
Date t = null;
try {
    return formatter.parse(dateString);
} catch (Exception e) {
    return t;
DatedateStringToDateObject(String dateString, String dateFormat)
Converts a given dateString to a date object using the given dateFormat
Date date;
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
date = format.parse(dateString);
return date;
Datedatetime(String s)
datetime
return parsedate(s, FMT_DATETIME);
DatedateTime(String s)
date Time
List<DateFormat> formats = asList(dateTimeFormat("kk:mm dd/MM/yyyy", UTC),
        dateTimeFormat("kk:mm:ss dd/MM/yyyy", UTC), dateTimeFormat("kk:mm:ss.SSS dd/MM/yyyy", UTC),
        dateTimeFormat("E MMM dd kk:mm:ss Z yyyy", UTC));
for (DateFormat format : formats) {
    try {
        return format.parse(s);
    } catch (ParseException ignored) {
throw new RuntimeException("Failed to parse string as dateTime: " + s);
DatedatetimeParse(String datetime)
datetime Parse
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2 = null;
try {
    date2 = sdf.parse(datetime);
} catch (ParseException e) {
    e.printStackTrace();
return date2;
...
StringDateTimeParse(String DateTimeString, String LMS)
Date Time Parse
String parsedDate = "";
Date dte = null;
DateFormat formatter = null;
try {
    if (LMS.equals("WebCT")) {
        formatter = new SimpleDateFormat("MMM ddd,yyyy hh:mm a");
        dte = (Date) formatter.parse(DateTimeString);
    } else if (LMS.equals("Moodle")) {
...
DategetDateFromCurrentAnd24HRTime(Date current, String hr24Time)
get Date From Current And HR Time
Calendar calendar = Calendar.getInstance();
calendar.setTime(current);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
Calendar newCalendar = Calendar.getInstance();
newCalendar.set(Calendar.DAY_OF_MONTH, day);
newCalendar.set(Calendar.MONTH, month);
...
DategetDateFromDateAge(Date currentDate, String age)
Retrieves the original date by current date and an age.
Calendar cal = Calendar.getInstance();
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String nowString = sdf.format(currentDate);
    cal.setTime(sdf.parse(nowString));
} catch (ParseException e) {
    cal.setTime(currentDate);
if (age.contains("m") || age.contains("M")) {
    age = age.replace("m", "");
    age = age.replace("M", "");
    int months = Integer.parseInt(age) * -1;
    cal.add(Calendar.MONTH, months);
} else {
    double years = Double.parseDouble(age) * -1;
    cal.add(Calendar.YEAR, (int) years);
return cal.getTime();