public class

DateUtils

extends Object
java.lang.Object
   ↳ android.text.format.DateUtils

Class Overview

This class contains various date-related utilities for creating text for things like elapsed time and date ranges, strings for days of the week and months, and AM/PM text etc.

Summary

Constants
String ABBREV_MONTH_FORMAT
String ABBREV_WEEKDAY_FORMAT
long DAY_IN_MILLIS
int FORMAT_12HOUR
int FORMAT_24HOUR
int FORMAT_ABBREV_ALL
int FORMAT_ABBREV_MONTH
int FORMAT_ABBREV_RELATIVE
int FORMAT_ABBREV_TIME
int FORMAT_ABBREV_WEEKDAY
int FORMAT_CAP_AMPM
int FORMAT_CAP_MIDNIGHT
int FORMAT_CAP_NOON
int FORMAT_CAP_NOON_MIDNIGHT
int FORMAT_NO_MIDNIGHT
int FORMAT_NO_MONTH_DAY
int FORMAT_NO_NOON
int FORMAT_NO_NOON_MIDNIGHT
int FORMAT_NO_YEAR
int FORMAT_NUMERIC_DATE
int FORMAT_SHOW_DATE
int FORMAT_SHOW_TIME
int FORMAT_SHOW_WEEKDAY
int FORMAT_SHOW_YEAR
int FORMAT_UTC
long HOUR_IN_MILLIS
String HOUR_MINUTE_24
int LENGTH_LONG Request the full spelled-out name.
int LENGTH_MEDIUM Request an abbreviated version of the name.
int LENGTH_SHORT Request a shorter abbreviated version of the name.
int LENGTH_SHORTER Request an even shorter abbreviated version of the name.
int LENGTH_SHORTEST Request an even shorter abbreviated version of the name.
long MINUTE_IN_MILLIS
String MONTH_DAY_FORMAT
String MONTH_FORMAT
String NUMERIC_MONTH_FORMAT
long SECOND_IN_MILLIS
String WEEKDAY_FORMAT
long WEEK_IN_MILLIS
String YEAR_FORMAT
String YEAR_FORMAT_TWO_DIGITS
long YEAR_IN_MILLIS
int[] sameMonthTable
int[] sameYearTable
Public Constructors
DateUtils()
Public Methods
static String formatDateRange(Context context, long startMillis, long endMillis, int flags)
Formats a date or a time range according to the local conventions.
static String formatDateTime(Context context, long millis, int flags)
Formats a date or a time according to the local conventions.
static String formatElapsedTime(long elapsedSeconds)
Formats an elapsed time in the form "MM:SS" or "H:MM:SS" for display on the call-in-progress screen.
static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds)
Formats an elapsed time in the form "MM:SS" or "H:MM:SS" for display on the call-in-progress screen.
final static CharSequence formatSameDayTime(long then, long now, int dateStyle, int timeStyle)
Format a date / time such that if the then is on the same day as now, it shows just the time and if it's a different day, it shows just the date.
static String getAMPMString(int ampm)
Return a localized string for AM or PM.
static String getDayOfWeekString(int dayOfWeek, int abbrev)
Return a string for the day of the week.
static String getMonthString(int month, int abbrev)
Return a localized string for the day of the week.
static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution, long transitionResolution, int flags)
Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".
static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution, int flags)
Returns a string describing 'time' as a time relative to 'now'.
static CharSequence getRelativeTimeSpanString(Context c, long millis)
Convenience function to return relative time string without preposition.
static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution)
Returns a string describing 'time' as a time relative to 'now'.
static CharSequence getRelativeTimeSpanString(long startTime)
Returns a string describing the elapsed time since startTime.
static CharSequence getRelativeTimeSpanString(Context c, long millis, boolean withPreposition)
static boolean isToday(long when)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ABBREV_MONTH_FORMAT

Constant Value: "%b"

public static final String ABBREV_WEEKDAY_FORMAT

Constant Value: "%a"

public static final long DAY_IN_MILLIS

Constant Value: 86400000 (0x0000000005265c00)

public static final int FORMAT_12HOUR

Constant Value: 64 (0x00000040)

public static final int FORMAT_24HOUR

Constant Value: 128 (0x00000080)

public static final int FORMAT_ABBREV_ALL

Constant Value: 524288 (0x00080000)

public static final int FORMAT_ABBREV_MONTH

Constant Value: 65536 (0x00010000)

public static final int FORMAT_ABBREV_RELATIVE

Constant Value: 262144 (0x00040000)

public static final int FORMAT_ABBREV_TIME

Constant Value: 16384 (0x00004000)

public static final int FORMAT_ABBREV_WEEKDAY

Constant Value: 32768 (0x00008000)

public static final int FORMAT_CAP_AMPM

Constant Value: 256 (0x00000100)

public static final int FORMAT_CAP_MIDNIGHT

Constant Value: 4096 (0x00001000)

public static final int FORMAT_CAP_NOON

Constant Value: 1024 (0x00000400)

public static final int FORMAT_CAP_NOON_MIDNIGHT

Constant Value: 5120 (0x00001400)

public static final int FORMAT_NO_MIDNIGHT

Constant Value: 2048 (0x00000800)

public static final int FORMAT_NO_MONTH_DAY

Constant Value: 32 (0x00000020)

public static final int FORMAT_NO_NOON

Constant Value: 512 (0x00000200)

public static final int FORMAT_NO_NOON_MIDNIGHT

Constant Value: 2560 (0x00000a00)

public static final int FORMAT_NO_YEAR

Constant Value: 8 (0x00000008)

public static final int FORMAT_NUMERIC_DATE

Constant Value: 131072 (0x00020000)

public static final int FORMAT_SHOW_DATE

Constant Value: 16 (0x00000010)

public static final int FORMAT_SHOW_TIME

Constant Value: 1 (0x00000001)

public static final int FORMAT_SHOW_WEEKDAY

Constant Value: 2 (0x00000002)

public static final int FORMAT_SHOW_YEAR

Constant Value: 4 (0x00000004)

public static final int FORMAT_UTC

Constant Value: 8192 (0x00002000)

public static final long HOUR_IN_MILLIS

Constant Value: 3600000 (0x000000000036ee80)

public static final String HOUR_MINUTE_24

Constant Value: "%H:%M"

public static final int LENGTH_LONG

Request the full spelled-out name. For use with the 'abbrev' parameter of getDayOfWeekString(int, int) and getMonthString(int, int).

Constant Value: 10 (0x0000000a)

public static final int LENGTH_MEDIUM

Request an abbreviated version of the name. For use with the 'abbrev' parameter of getDayOfWeekString(int, int) and getMonthString(int, int).

Constant Value: 20 (0x00000014)

public static final int LENGTH_SHORT

Request a shorter abbreviated version of the name. For use with the 'abbrev' parameter of getDayOfWeekString(int, int) and getMonthString(int, int).

Constant Value: 30 (0x0000001e)

public static final int LENGTH_SHORTER

Request an even shorter abbreviated version of the name. For use with the 'abbrev' parameter of getDayOfWeekString(int, int) and getMonthString(int, int).

Constant Value: 40 (0x00000028)

public static final int LENGTH_SHORTEST

Request an even shorter abbreviated version of the name. For use with the 'abbrev' parameter of getDayOfWeekString(int, int) and getMonthString(int, int).

Constant Value: 50 (0x00000032)

public static final long MINUTE_IN_MILLIS

Constant Value: 60000 (0x000000000000ea60)

public static final String MONTH_DAY_FORMAT

Constant Value: "%-d"

public static final String MONTH_FORMAT

Constant Value: "%B"

public static final String NUMERIC_MONTH_FORMAT

Constant Value: "%m"

public static final long SECOND_IN_MILLIS

Constant Value: 1000 (0x00000000000003e8)

public static final String WEEKDAY_FORMAT

Constant Value: "%A"

public static final long WEEK_IN_MILLIS

Constant Value: 604800000 (0x00000000240c8400)

public static final String YEAR_FORMAT

Constant Value: "%Y"

public static final String YEAR_FORMAT_TWO_DIGITS

Constant Value: "%g"

public static final long YEAR_IN_MILLIS

Constant Value: 31449600000 (0x00000007528ad000)

public static final int[] sameMonthTable

public static final int[] sameYearTable

Public Constructors

public DateUtils ()

Public Methods

public static String formatDateRange (Context context, long startMillis, long endMillis, int flags)

Formats a date or a time range according to the local conventions.

Example output strings (date formats in these examples are shown using the US date format convention but that may change depending on the local settings):

  • 10:15am
  • 3:00pm - 4:00pm
  • 3pm - 4pm
  • 3PM - 4PM
  • 08:00 - 17:00
  • Oct 9
  • Tue, Oct 9
  • October 9, 2007
  • Oct 9 - 10
  • Oct 9 - 10, 2007
  • Oct 28 - Nov 3, 2007
  • Dec 31, 2007 - Jan 1, 2008
  • Oct 9, 8:00am - Oct 10, 5:00pm
  • 12/31/2007 - 01/01/2008

The flags argument is a bitmask of options from the following list:

  • FORMAT_SHOW_TIME
  • FORMAT_SHOW_WEEKDAY
  • FORMAT_SHOW_YEAR
  • FORMAT_NO_YEAR
  • FORMAT_SHOW_DATE
  • FORMAT_NO_MONTH_DAY
  • FORMAT_12HOUR
  • FORMAT_24HOUR
  • FORMAT_CAP_AMPM
  • FORMAT_NO_NOON
  • FORMAT_CAP_NOON
  • FORMAT_NO_MIDNIGHT
  • FORMAT_CAP_MIDNIGHT
  • FORMAT_UTC
  • FORMAT_ABBREV_TIME
  • FORMAT_ABBREV_WEEKDAY
  • FORMAT_ABBREV_MONTH
  • FORMAT_ABBREV_ALL
  • FORMAT_NUMERIC_DATE

If FORMAT_SHOW_TIME is set, the time is shown as part of the date range. If the start and end time are the same, then just the start time is shown.

If FORMAT_SHOW_WEEKDAY is set, then the weekday is shown.

If FORMAT_SHOW_YEAR is set, then the year is always shown. If FORMAT_NO_YEAR is set, then the year is not shown. If neither FORMAT_SHOW_YEAR nor FORMAT_NO_YEAR are set, then the year is shown only if it is different from the current year, or if the start and end dates fall on different years. If both are set, FORMAT_SHOW_YEAR takes precedence.

Normally the date is shown unless the start and end day are the same. If FORMAT_SHOW_DATE is set, then the date is always shown, even for same day ranges.

If FORMAT_NO_MONTH_DAY is set, then if the date is shown, just the month name will be shown, not the day of the month. For example, "January, 2008" instead of "January 6 - 12, 2008".

If FORMAT_CAP_AMPM is set and 12-hour time is used, then the "AM" and "PM" are capitalized.

If FORMAT_NO_NOON is set and 12-hour time is used, then "12pm" is shown instead of "noon".

If FORMAT_CAP_NOON is set and 12-hour time is used, then "Noon" is shown instead of "noon".

If FORMAT_NO_MIDNIGHT is set and 12-hour time is used, then "12am" is shown instead of "midnight".

If FORMAT_CAP_NOON is set and 12-hour time is used, then "Midnight" is shown instead of "midnight".

If FORMAT_12HOUR is set and the time is shown, then the time is shown in the 12-hour time format. You should not normally set this. Instead, let the time format be chosen automatically according to the system settings. If both FORMAT_12HOUR and FORMAT_24HOUR are set, then FORMAT_24HOUR takes precedence.

If FORMAT_24HOUR is set and the time is shown, then the time is shown in the 24-hour time format. You should not normally set this. Instead, let the time format be chosen automatically according to the system settings. If both FORMAT_12HOUR and FORMAT_24HOUR are set, then FORMAT_24HOUR takes precedence.

If FORMAT_UTC is set, then the UTC timezone is used for the start and end milliseconds.

If FORMAT_ABBREV_TIME is set and 12-hour time format is used, then the start and end times (if shown) are abbreviated by not showing the minutes if they are zero. For example, instead of "3:00pm" the time would be abbreviated to "3pm".

If FORMAT_ABBREV_WEEKDAY is set, then the weekday (if shown) is abbreviated to a 3-letter string.

If FORMAT_ABBREV_MONTH is set, then the month (if shown) is abbreviated to a 3-letter string.

If FORMAT_ABBREV_ALL is set, then the weekday and the month (if shown) are abbreviated to 3-letter strings.

If FORMAT_NUMERIC_DATE is set, then the date is shown in numeric format instead of using the name of the month. For example, "12/31/2008" instead of "December 31, 2008".

Parameters
context the context is required only if the time is shown
startMillis the start time in UTC milliseconds
endMillis the end time in UTC milliseconds
flags a bit mask of options
Returns
  • a string containing the formatted date/time range.

public static String formatDateTime (Context context, long millis, int flags)

Formats a date or a time according to the local conventions. There are lots of options that allow the caller to control, for example, if the time is shown, if the day of the week is shown, if the month name is abbreviated, if noon is shown instead of 12pm, and so on. For the complete list of options, see the documentation for formatDateRange(Context, long, long, int).

Example output strings (date formats in these examples are shown using the US date format convention but that may change depending on the local settings):

  • 10:15am
  • 3:00pm
  • 3pm
  • 3PM
  • 08:00
  • 17:00
  • noon
  • Noon
  • midnight
  • Midnight
  • Oct 31
  • Oct 31, 2007
  • October 31, 2007
  • 10am, Oct 31
  • 17:00, Oct 31
  • Wed
  • Wednesday
  • 10am, Wed, Oct 31
  • Wed, Oct 31
  • Wednesday, Oct 31
  • Wed, Oct 31, 2007
  • Wed, October 31
  • 10/31/2007

Parameters
context the context is required only if the time is shown
millis a point in time in UTC milliseconds
flags a bit mask of formatting options
Returns
  • a string containing the formatted date/time.

public static String formatElapsedTime (long elapsedSeconds)

Formats an elapsed time in the form "MM:SS" or "H:MM:SS" for display on the call-in-progress screen.

Parameters
elapsedSeconds the elapsed time in seconds.

public static String formatElapsedTime (StringBuilder recycle, long elapsedSeconds)

Formats an elapsed time in the form "MM:SS" or "H:MM:SS" for display on the call-in-progress screen.

Parameters
recycle StringBuilder to recycle, if possible
elapsedSeconds the elapsed time in seconds.

public static final CharSequence formatSameDayTime (long then, long now, int dateStyle, int timeStyle)

Format a date / time such that if the then is on the same day as now, it shows just the time and if it's a different day, it shows just the date.

The parameters dateFormat and timeFormat should each be one of DEFAULT, FULL, LONG, MEDIUM or SHORT

Parameters
then the date to format
now the base time
dateStyle how to format the date portion.
timeStyle how to format the time portion.

public static String getAMPMString (int ampm)

Return a localized string for AM or PM.

Parameters
ampm Either Calendar.AM or Calendar.PM.
Returns
  • Localized version of "AM" or "PM".
Throws
IndexOutOfBoundsException if the ampm is out of bounds.

public static String getDayOfWeekString (int dayOfWeek, int abbrev)

Return a string for the day of the week.

Parameters
dayOfWeek One of Calendar.SUNDAY, Calendar.MONDAY, etc.
abbrev One of LENGTH_LONG, LENGTH_SHORT, LENGTH_SHORTER or LENGTH_SHORTEST. For forward compatibility, anything else will return the same as {#LENGTH_MEDIUM}.
Throws
IndexOutOfBoundsException if the dayOfWeek is out of bounds.

public static String getMonthString (int month, int abbrev)

Return a localized string for the day of the week.

Parameters
month One of Calendar.JANUARY, Calendar.FEBRUARY, etc.
abbrev One of LENGTH_LONG, LENGTH_SHORT, LENGTH_SHORTER or LENGTH_SHORTEST. For forward compatibility, anything else will return the same as {#LENGTH_MEDIUM}.
Returns
  • Localized day of the week.

public static CharSequence getRelativeDateTimeString (Context c, long time, long minResolution, long transitionResolution, int flags)

Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".

Example output strings for the US date format.

  • 3 mins ago, 10:15 AM
  • yesterday, 12:20 PM
  • Dec 12, 4:12 AM
  • 11/14/2007, 8:20 AM

Parameters
time some time in the past.
minResolution the minimum elapsed time (in milliseconds) to report when showing relative times. For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS.
transitionResolution the elapsed time (in milliseconds) at which to stop reporting relative measurements. Elapsed times greater than this resolution will default to normal date formatting. For example, will transition from "6 days ago" to "Dec 12" when using WEEK_IN_MILLIS.

public static CharSequence getRelativeTimeSpanString (long time, long now, long minResolution, int flags)

Returns a string describing 'time' as a time relative to 'now'.

Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "in 42 minutes".

Can use FORMAT_ABBREV_RELATIVE flag to use abbreviated relative times, like "42 mins ago".

Parameters
time the time to describe, in milliseconds
now the current time in milliseconds
minResolution the minimum timespan to report. For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS. Pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS
flags a bit mask of formatting options, such as FORMAT_NUMERIC_DATE or FORMAT_ABBREV_RELATIVE

public static CharSequence getRelativeTimeSpanString (Context c, long millis)

Convenience function to return relative time string without preposition.

Parameters
c context for resources
millis time in milliseconds
Returns

public static CharSequence getRelativeTimeSpanString (long time, long now, long minResolution)

Returns a string describing 'time' as a time relative to 'now'.

Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "in 42 minutes".

Parameters
time the time to describe, in milliseconds
now the current time in milliseconds
minResolution the minimum timespan to report. For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS. Pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS

public static CharSequence getRelativeTimeSpanString (long startTime)

Returns a string describing the elapsed time since startTime.

Parameters
startTime some time in the past.
Returns
  • a String object containing the elapsed time.

public static CharSequence getRelativeTimeSpanString (Context c, long millis, boolean withPreposition)

Parameters
withPreposition If true, the string returned will include the correct preposition ("at 9:20am", "on 10/12/2008" or "on May 29").
Returns
  • a relative time string to display the time expressed by millis. Times are counted starting at midnight, which means that assuming that the current time is March 31st, 0:30:
    • "millis=0:10 today" will be displayed as "0:10"
    • "millis=11:30pm the day before" will be displayed as "Mar 30"
    If the given millis is in a different year, then the full date is returned in numeric format (e.g., "10/12/2008").

public static boolean isToday (long when)

Returns
  • true if the supplied when is today else false