Android Utililty Methods Date Format

List of utility methods to do Date Format

Description

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

Method

StringformatTM(Date d)
format TM
if (d == null)
    return null;
Calendar c = new GregorianCalendar();
c.setTime(d);
return formatTM(c, new StringBuffer(10)).toString();
DatefromString(String dateStr)
from String
return fromString(dateStr, DATE_FORMAT);
DatefromString(String dateStr, String dateFormat)
from String
return fromString(dateStr, dateFormat, Locale.ENGLISH);
DatefromString(String dateStr, String dateFormat, Locale locale)
from String
SimpleDateFormat format = new SimpleDateFormat(dateFormat, locale);
return format.parse(dateStr);
StringgetBeforeYear()
get Before Year
String currentYear = getFormatCurrentTime("yyyy");
int beforeYear = Integer.parseInt(currentYear) - 1;
return "" + beforeYear;
StringgetCurrentDate()
get Current Date
return getFormatDateTime(new Date(), "yyyy-MM-dd");
StringgetCurrentDateString(String dateFormat)
get Current Date String
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(cal.getTime());
StringgetCurrentDateTime()
get Current Date Time
return getFormatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss");
StringgetCurrentDay()
get Current Day
return getFormatCurrentTime("dd");
intgetCurrentDayOfWeek()
get Current Day Of Week
int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
return day == 0 ? (day + 7) : day;