Android Utililty Methods Date to String Convert

List of utility methods to do Date to String Convert

Description

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

Method

StringgetDateByFormatedString(String date)
get Date By Formated String
Date mDate = null;
String dateFormated = "";
Calendar cal = Calendar.getInstance();
if (date != null && !date.isEmpty()) {
    try {
        mDate = DateTimeUtil.stringToDate(date);
    } catch (ParseException e) {
        Log.e(TAG, "Unparsable date");
...
StringgetDateTimeString(Date date)
get Date Time String
SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
String result = df.format(date);
return result;
StringgetDateTimeString(Date date, int format, Locale currentLocale)
get Date Time String
String key = format + "_" + currentLocale.toString();
DateFormat formatter = dateFormats.get(key);
if (formatter == null) {
    formatter = DateFormat.getDateTimeInstance(format, format,
            currentLocale);
    dateFormats.put(key, formatter);
return formatter.format(date);
...
StringgetFileName()
get File Name in format of "yyyy-MM-dd"
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(new Date(System.currentTimeMillis()));
return date;
StringgetFormatString(Date date, String dateFormat)
get Format String
if (date == null || StringUtil.isEmpty(dateFormat)) {
    return null;
return new SimpleDateFormat(dateFormat, Locale.ENGLISH)
        .format(date);
floatgetHourInFloat(Date date)
Converts "2013-09-08 15:30" to 15.5
final long hours = DateUtils.getFragmentInHours(date,
        Calendar.DAY_OF_MONTH);
final long minutes = DateUtils.getFragmentInMinutes(date,
        Calendar.HOUR_OF_DAY);
return hours + (minutes / 60.f);
StringgetLongDateStr(Date date)
Converts given date time to long date string.
return LONG_DATE_FORMAT.format(date);
StringgetLongFormat(Date date)
get Long Format
if (date == null) {
    return null;
return getFormatString(date, LOCAL_LONG_DATE_FORMAT);
StringgetShortDateTimeString(Date date)
get Short Date Time String
SimpleDateFormat df = new SimpleDateFormat(SHORT_DATE_TIME_FORMAT);
return df.format(date);
StringgetShortFormat(Date date)
get Short Format
if (date == null) {
    return null;
return getFormatString(date, LOCAL_SHORT_DATE_FORMAT);