Get a string representation of a date as it relates to the current time. : Date « Date Type « Android






Get a string representation of a date as it relates to the current time.

     
//package divestoclimb.util;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * A class of utility functions for outputting date/time information relative
 * to another date/time.
 * TODO: localize. This would be best done by keeping this class's logic Android-
 * independent so it returned some kind of structured format that could build
 * an output string for format strings stored in resources.
 * @author Ben Roberts (divestoclimb@gmail.com)
 */
class FriendlyDate {

  /**
   * Get a string representation of a date as it relates to the current time.
   * @param time The Date object to use to generate the string representation
   * @return A string explaining how long ago or in the future the date is from 
   * now.
   */
  public static String format(Date time) {
    return format(time, new Date());
  }

  // This implementation can't handle leap seconds, but it really doesn't
  // need to be that precise for my purposes
  public static final int MILLIS_PER_SEC=1000;
  public static final int MILLIS_PER_MIN=MILLIS_PER_SEC*60;
  public static final int MILLIS_PER_HR=MILLIS_PER_MIN*60;
  public static final int MILLIS_PER_DAY=MILLIS_PER_HR*24;
  public static final int MILLIS_PER_WEEK=MILLIS_PER_DAY*7;

  /**
   * Get a string representation of a date as it relates to another date.
   * @param time The Date object to use to generate the string representation
   * @param relative The Date object to compare against
   * @return A string explaining how far behind or ahead the date is from relative.
   */
  public static String format(Date time, Date relative) {
    Calendar timeCal = Calendar.getInstance();
    timeCal.setTime(time);
    Calendar relCal = Calendar.getInstance();
    relCal.setTime(relative);

    // Compute the difference between these two dates in milliseconds, but
    // first account for any timezone differences between the two dates.
    long timeMillis = timeCal.getTimeInMillis();
    timeMillis += timeCal.getTimeZone().getOffset(timeMillis);
    long relMillis = relCal.getTimeInMillis();
    relMillis += relCal.getTimeZone().getOffset(relMillis);
    long diff = timeMillis - relMillis;
    String dir = (diff < 0)? "ago":"from now";
    diff = Math.abs(diff);

    if(diff / MILLIS_PER_MIN < 0.2) {
      return "within seconds";
    }
    if(diff / MILLIS_PER_MIN < 0.8) {
      return "less than a minute "+dir;
    }
    if(diff / MILLIS_PER_MIN < 1.5) {
      return "about a minute "+dir;
    }
    if(diff / MILLIS_PER_HR < 0.9) {
      return new Integer(Math.round(diff / MILLIS_PER_MIN)).toString()+" minutes "+dir;
    }
    if(diff / MILLIS_PER_HR < 1.2) {
      return "about an hour "+dir;
    }
    if(diff / MILLIS_PER_HR < 1.9) {
      return "more than an hour "+dir;
    }
    if(diff / MILLIS_PER_DAY < 0.8) {
      return new Integer(Math.round(diff / MILLIS_PER_HR)).toString()+" hours "+dir;
    }
    if(diff / MILLIS_PER_DAY < 1.2) {
      return "about a day "+dir;
    }
    if(diff / MILLIS_PER_DAY < 1.9) {
      return "more than a day "+dir;
    }
    if(diff / MILLIS_PER_WEEK < 1) {
      return new Integer(Math.round(diff / MILLIS_PER_DAY)).toString()+" days "+dir;
    }
    if(diff / MILLIS_PER_WEEK < 1.5) {
      return "about a week "+dir;
    }
    if(diff / MILLIS_PER_WEEK < 7) {
      return new Integer(Math.round(diff / MILLIS_PER_WEEK)).toString()+" weeks "+dir;
    }

    // Just give the raw date
    return DateFormat.getDateInstance().format(time);

  }
}

   
    
    
    
    
  








Related examples in the same category

1.Write Date to AudioTrack
2.Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel.
3.A formatter that formats dates to show the elapsed time relative to some base date.
4.Iso Date
5.Get the string of the date using format "yyyy-MM-dd HH:mm:ss"
6.Parse a string that contains date, return a date object using format "yyyy-MM-dd HH:mm:ss"
7.Create Date from timestamp
8.Convert date in RFC2822 and UTC strings, and to build Date from string of dates in RFC2822 and UTC format
9.Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date
10.Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date
11.Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date
12.Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date
13.Formatting and parsing the various date formats we expect to encounter.
14.ISO8601, ISO8601, RFC822 Date format
15.Easter Date
16.Parse Date
17.compare Two dates with Day value
18.compare Two dates with Second value
19.Get start of a date
20.Get start date of a Month
21.Get start date of a year
22.Create date from year, month and day value
23.Create Date from year, month, day, hour, minute, second
24.Get year value from Date
25.Get Month value from Date
26.Get Day value from Date
27.Get now in Date and Millis-seconds
28.Add day, Month and year to a Date
29.Parse date in format of yyyyMMddHHmmss or yyyyMMdd
30.String to Date
31.Convert date value in long to YYYYMMDDHHMMSS format
32.Convert Date to Number
33.Get short and long date String
34.Convert Java Date To Xml Time
35.Convert dates to Julian dates.
36.Parse an RFC 822 date string.
37.Format a date into a format suitable for SQLite
38.Dot String to Date
39.Parses an RFC822 formatted date string.
40.Formats a Date according to RFC822.
41.DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date);
42.yyyy-MM-dd HH:mm:ss date format
43.Utility class for formatting and parsing the various date formats we expect to encounter.
44.get Date String from milliseconds
45.Generate a ISO 8601 date
46.Generate a Calendar from ISO 8601 date
47.parse Date for list of possible formats
48.date To String
49.Get date string for Locale tr
50.RFC3339 Date
51.Date formats
52.build Date Format day-of-week Short
53.Get end of each day
54.Get the end of each Month
55.Get end of a year
56.calculate Month Distance
57.calculate Day Distance
58.The month, and just the month, is zero-based. Add 1 for display.
59.Get hour different
60.format Millis Into Human Readable