Android Utililty Methods Long to Date Convert

List of utility methods to do Long to Date Convert

Description

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

Method

StringBuilderhrColMin(final long time, final boolean alwaysIncludeHours)
hr Col Min
final long seconds = time / 1000;
final long minutes = (seconds / 60) % 60;
final long hours = (seconds / (60 * 60)) % 60;
Log.i("DateTimeUtils", "hrColMin  hours  minutes  seconds: "
        + hours + "  : " + minutes + "  : " + seconds);
final StringBuilder asText = new StringBuilder();
if (alwaysIncludeHours || hours > 0) {
    if (hours < 10) {
...
StringBuilderhrColMinColSec(final long time, final boolean alwaysIncludeHours)
hr Col Min Col Sec
final long seconds = (time / 1000) % 60;
final StringBuilder asText = hrColMin(time, alwaysIncludeHours);
asText.append(":");
if (seconds < 10) {
    asText.append(0);
asText.append(seconds);
return asText;
...
booleanisAtMostNMonthsAgo(long date, int howMany)
is At Most N Months Ago
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -howMany);
Date d = new Date(date);
return d.after(cal.getTime());
booleanisAtMostNWeeksAgo(long date, int howMany)
is At Most N Weeks Ago
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, -howMany);
Date d = new Date(date);
return d.after(cal.getTime());
booleanisYesterday(long date)
Checks if the given date is yesterday.
final Calendar currentDate = Calendar.getInstance();
currentDate.setTimeInMillis(date);
final Calendar yesterdayDate = Calendar.getInstance();
yesterdayDate.add(Calendar.DATE, -1);
return yesterdayDate.get(Calendar.YEAR) == currentDate
        .get(Calendar.YEAR)
        && yesterdayDate.get(Calendar.DAY_OF_YEAR) == currentDate
                .get(Calendar.DAY_OF_YEAR);
...
booleanisYesterday(long date)
Checks if the given date is yesterday.
final Calendar currentDate = Calendar.getInstance();
currentDate.setTimeInMillis(date);
final Calendar yesterdayDate = Calendar.getInstance();
yesterdayDate.add(Calendar.DATE, -1);
return yesterdayDate.get(Calendar.YEAR) == currentDate
        .get(Calendar.YEAR)
        && yesterdayDate.get(Calendar.DAY_OF_YEAR) == currentDate
                .get(Calendar.DAY_OF_YEAR);
...
StringlongTimeToDate(long miliseconds)
long Time To Date in format "yyyy-MM-dd HH:mm:ss"
Date dt = new Date(miliseconds);
return AGO_FULL_DATE_FORMATTER.format(dt);
StringlongTimeToString(int i)
long Time To String
StringBuilder sb = new StringBuilder();
i = i / 1000;
int m = i / 60;
int s = i % 60;
if (i >= 60) {
    if (m < 10) {
        sb.append("0");
        sb.append(String.valueOf(m));
...
CalendarlongToCalendar(Long time)
long To Calendar
Calendar c = null;
if (time != null) {
    c = Calendar.getInstance();
    c.setTimeInMillis(time);
return c;
StringlongToSqlDateFormat(long date)
long To Sql Date Format
Date d = new Date(date);
SimpleDateFormat dateFormat = new SimpleDateFormat(SQL_DATE_FORMAT);
return dateFormat.format(d);