Android Open Source - MaterialManager Time Utils






From Project

Back to project page MaterialManager.

License

The source code is released under:

There is no license, someone decided to pretty much republish Cabinet with no credit so I?m taking away the license altogether.

If you think the Android project MaterialManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.afollestad.cabinet.utils;
/* ww  w.ja  v  a2  s.  c  o m*/
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Utilities for getting human readable time strings.
 *
 * @author Aidan Follestad (afollestad)
 */
public class TimeUtils {

    private static final int SECONDS_IN_MINUTE = 60;
    private static final int MINUTES_IN_HOUR = 60;
    private static final int HOURS_IN_DAY = 24;
    private static final int DAYS_IN_YEAR = 365;
    private static final int MILLIS_IN_SECOND = 1000;
    private static final long MILLISECONDS_IN_MINUTE = (long) MILLIS_IN_SECOND * SECONDS_IN_MINUTE;
    private static final long MILLISECONDS_IN_HOUR = (long) MILLIS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR;
    private static final long MILLISECONDS_IN_DAY = (long) MILLIS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY;
    private static final long MILLISECONDS_IN_YEAR = (long) MILLIS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR;

    public static String toStringLong(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return toStringLong(cal);
    }

    /**
     * Gets a human-readable long time string (includes both the time and date, always).
     */
    public static String toStringLong(Calendar date) {
        Calendar now = Calendar.getInstance();
        int hourInt = date.get(Calendar.HOUR);
        int minuteInt = date.get(Calendar.MINUTE);
        String dayStr = getNumberWithSuffix(date.get(Calendar.DAY_OF_MONTH));

        String timeStr = "";
        if (hourInt == 0) timeStr += "12";
        else timeStr += "" + hourInt;
        if (minuteInt < 10) timeStr += ":0" + minuteInt;
        else timeStr += ":" + minuteInt;
        if (date.get(Calendar.AM_PM) == Calendar.AM) timeStr += "AM";
        else timeStr += "PM";

        if (now.get(Calendar.YEAR) == date.get(Calendar.YEAR)) {
            // Same year
            return timeStr + " " + convertMonth(date.get(Calendar.MONTH), false) + " " + dayStr;
        } else {
            // Different year
            return timeStr + " " + convertMonth(date.get(Calendar.MONTH), false) + " " + dayStr + ", " + date.get(Calendar.YEAR);
        }
    }

    public static String toString(Date date, boolean includeTime, boolean shortMonth) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return toString(cal, includeTime, shortMonth);
    }

    /**
     * Gets a human-readable time string (includes both the time and date, excluded certain parts if possible).
     *
     * @param shortMonth Whether or display a long or short month string (e.g. 'January' or 'Jan').
     */
    public static String toString(Calendar date, boolean includeTime, boolean shortMonth) {
        Calendar now = Calendar.getInstance();
        int hourInt = date.get(Calendar.HOUR);
        int minuteInt = date.get(Calendar.MINUTE);
        String dayStr = getNumberWithSuffix(date.get(Calendar.DAY_OF_MONTH));

        String timeStr = "";
        if (hourInt == 0) timeStr += "12";
        else timeStr += "" + hourInt;
        if (minuteInt < 10) timeStr += ":0" + minuteInt;
        else timeStr += ":" + minuteInt;
        if (date.get(Calendar.AM_PM) == Calendar.AM) timeStr += "AM";
        else timeStr += "PM";

        if (now.get(Calendar.YEAR) == date.get(Calendar.YEAR)) {
            // Same year
            if (now.get(Calendar.MONTH) == date.get(Calendar.MONTH)) {
                // Same year, same month
                if (now.get(Calendar.DAY_OF_YEAR) == date.get(Calendar.DAY_OF_YEAR)) {
                    // Same year, same month, same day
                    return timeStr;
                } else {
                    // Same year, same month, different day
                    String toReturn = "";
                    if (includeTime) toReturn = timeStr + " ";
                    toReturn += convertMonth(date.get(Calendar.MONTH), shortMonth) + " " + dayStr;
                    return toReturn;
                }
            } else {
                // Different month, same year
                String toReturn = "";
                if (includeTime) toReturn = timeStr + " ";
                toReturn += convertMonth(date.get(Calendar.MONTH), shortMonth) + " " + dayStr;
                return toReturn;
            }
        } else {
            // Different year
            String year = Integer.toString(date.get(Calendar.YEAR));
            String toReturn = "";
            if (includeTime) toReturn = timeStr + " ";
            toReturn += convertMonth(date.get(Calendar.MONTH), shortMonth) + " " + dayStr + ", " + year;
            return toReturn;
        }
    }

    public static String toStringDate(Date date, boolean shortMonth, boolean alwaysIncludeYear) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return toStringDate(cal, shortMonth, alwaysIncludeYear);
    }

    /**
     * Gets a human-readable date string (month, day, and year).
     *
     * @param shortMonth        Whether or display a long or short month string (e.g. 'January' or 'Jan').
     * @param alwaysIncludeYear Include the year even if it's the current year.
     */
    public static String toStringDate(Calendar time, boolean shortMonth, boolean alwaysIncludeYear) {
        Calendar now = Calendar.getInstance();
        String day = getNumberWithSuffix(time.get(Calendar.DAY_OF_MONTH));
        if (now.get(Calendar.YEAR) == time.get(Calendar.YEAR) && !alwaysIncludeYear) {
            // Same year
            if (now.get(Calendar.MONTH) == time.get(Calendar.MONTH)) {
                // Same year, same month
                return convertMonth(time.get(Calendar.MONTH), shortMonth) + " " + day;
            } else {
                // Different month, same year
                return convertMonth(time.get(Calendar.MONTH), shortMonth) + " " + day;
            }
        } else {
            // Different year
            String year = Integer.toString(time.get(Calendar.YEAR));
            return convertMonth(time.get(Calendar.MONTH), shortMonth) + " " + day + ", " + year;
        }
    }

    public static String toStringTime(Calendar time) {
        int hourInt = time.get(Calendar.HOUR);
        int minuteInt = time.get(Calendar.MINUTE);
        String timeStr = "";
        if (hourInt == 0) timeStr += "12";
        else timeStr += "" + hourInt;
        if (minuteInt < 10) timeStr += ":0" + minuteInt;
        else timeStr += ":" + minuteInt;
        if (time.get(Calendar.AM_PM) == Calendar.AM) timeStr += "AM";
        else timeStr += "PM";
        return timeStr;
    }

    public static String toStringTime(Date time) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(time);
        return toStringTime(cal);
    }

    public static String toStringShort(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return toStringShort(cal);
    }

    public static String toStringShort(Calendar time) {
        Calendar now = Calendar.getInstance();
        long diff = now.getTimeInMillis() - time.getTimeInMillis();
        long years = diff / MILLISECONDS_IN_YEAR;
        if (years == 0) {
            long days = diff / MILLISECONDS_IN_DAY;
            if (days == 0) {
                long hours = diff / MILLISECONDS_IN_HOUR;
                if (hours == 0) {
                    long minutes = diff / MILLISECONDS_IN_MINUTE;
                    if (minutes == 0) {
                        long seconds = diff / MILLIS_IN_SECOND;
                        return seconds + "s";
                    } else {
                        return minutes + "m";
                    }
                } else {
                    return hours + "h";
                }
            } else {
                if (days == 7) return "1w";
                else if (days > 7) {
                    long weeks = days / 7;
                    days = days % 7;
                    String str = weeks + "w";
                    if (days > 0) str += days + "d";
                    return str;
                } else return days + "d";
            }
        } else {
            return years + "y";
        }
    }

    private static String convertMonth(int month, boolean useShort) {
        String monthStr;
        switch (month) {
            default:
                monthStr = "January";
                break;
            case Calendar.FEBRUARY:
                monthStr = "February";
                break;
            case Calendar.MARCH:
                monthStr = "March";
                break;
            case Calendar.APRIL:
                monthStr = "April";
                break;
            case Calendar.MAY:
                monthStr = "May";
                break;
            case Calendar.JUNE:
                monthStr = "June";
                break;
            case Calendar.JULY:
                monthStr = "July";
                break;
            case Calendar.AUGUST:
                monthStr = "August";
                break;
            case Calendar.SEPTEMBER:
                monthStr = "September";
                break;
            case Calendar.OCTOBER:
                monthStr = "October";
                break;
            case Calendar.NOVEMBER:
                monthStr = "November";
                break;
            case Calendar.DECEMBER:
                monthStr = "December";
                break;
        }
        if (useShort) monthStr = monthStr.substring(0, 3);
        return monthStr;
    }

    private static String getNumberWithSuffix(int number) {
        int j = number % 10;
        if (j == 1 && number != 11) {
            return number + "st";
        }
        if (j == 2 && number != 12) {
            return number + "nd";
        }
        if (j == 3 && number != 13) {
            return number + "rd";
        }
        return number + "th";
    }
}




Java Source Code List

com.afollestad.cabinet.App.java
com.afollestad.cabinet.ApplicationTest.java
com.afollestad.cabinet.adapters.FileAdapter.java
com.afollestad.cabinet.adapters.NavigationDrawerAdapter.java
com.afollestad.cabinet.cab.CopyCab.java
com.afollestad.cabinet.cab.CutCab.java
com.afollestad.cabinet.cab.MainCab.java
com.afollestad.cabinet.cab.PickerCab.java
com.afollestad.cabinet.cab.base.BaseCab.java
com.afollestad.cabinet.cab.base.BaseFileCab.java
com.afollestad.cabinet.comparators.AlphabeticalComparator.java
com.afollestad.cabinet.comparators.ExtensionComparator.java
com.afollestad.cabinet.comparators.FoldersFirstComparator.java
com.afollestad.cabinet.comparators.HighLowSizeComparator.java
com.afollestad.cabinet.comparators.LastModifiedComparator.java
com.afollestad.cabinet.comparators.LowHighSizeComparator.java
com.afollestad.cabinet.file.CloudFile.java
com.afollestad.cabinet.file.LocalFile.java
com.afollestad.cabinet.file.Remote.java
com.afollestad.cabinet.file.base.FileFilter.java
com.afollestad.cabinet.file.base.File.java
com.afollestad.cabinet.file.root.LsParser.java
com.afollestad.cabinet.file.root.LsTokenizer.java
com.afollestad.cabinet.file.root.RootFile.java
com.afollestad.cabinet.fragments.AboutDialog.java
com.afollestad.cabinet.fragments.DetailsDialog.java
com.afollestad.cabinet.fragments.DirectoryFragment.java
com.afollestad.cabinet.fragments.NavigationDrawerFragment.java
com.afollestad.cabinet.fragments.RemoteConnectionDialog.java
com.afollestad.cabinet.fragments.WelcomeFragment.java
com.afollestad.cabinet.services.NetworkService.java
com.afollestad.cabinet.sftp.FileNotExistsException.java
com.afollestad.cabinet.sftp.SftpClient.java
com.afollestad.cabinet.ui.DrawerActivity.java
com.afollestad.cabinet.ui.SettingsActivity.java
com.afollestad.cabinet.ui.TextEditor.java
com.afollestad.cabinet.ui.base.NetworkedActivity.java
com.afollestad.cabinet.ui.base.ThemableActivity.java
com.afollestad.cabinet.utils.APKIconDownloader.java
com.afollestad.cabinet.utils.PauseOnScrollListener.java
com.afollestad.cabinet.utils.Perm.java
com.afollestad.cabinet.utils.Pins.java
com.afollestad.cabinet.utils.StorageHelper.java
com.afollestad.cabinet.utils.ThemeUtils.java
com.afollestad.cabinet.utils.TimeUtils.java
com.afollestad.cabinet.utils.Utils.java
com.afollestad.cabinet.zip.Unzipper.java
com.afollestad.cabinet.zip.Zipper.java
eu.chainfire.libsuperuser.ApplicationTest.java
eu.chainfire.libsuperuser.Application.java
eu.chainfire.libsuperuser.Debug.java
eu.chainfire.libsuperuser.HideOverlaysReceiver.java
eu.chainfire.libsuperuser.ShellNotClosedException.java
eu.chainfire.libsuperuser.ShellOnMainThreadException.java
eu.chainfire.libsuperuser.Shell.java
eu.chainfire.libsuperuser.StreamGobbler.java