public class DateUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ABBREV_MONTH_FORMAT
Deprecated.
use
SimpleDateFormat instead. |
static String |
ABBREV_WEEKDAY_FORMAT |
static long |
DAY_IN_MILLIS |
static int |
FORMAT_12HOUR
Deprecated.
|
static int |
FORMAT_24HOUR
Deprecated.
|
static int |
FORMAT_ABBREV_ALL |
static int |
FORMAT_ABBREV_MONTH |
static int |
FORMAT_ABBREV_RELATIVE |
static int |
FORMAT_ABBREV_TIME |
static int |
FORMAT_ABBREV_WEEKDAY |
static int |
FORMAT_CAP_AMPM
Deprecated.
|
static int |
FORMAT_CAP_MIDNIGHT
Deprecated.
|
static int |
FORMAT_CAP_NOON
Deprecated.
|
static int |
FORMAT_CAP_NOON_MIDNIGHT
Deprecated.
|
static int |
FORMAT_NO_MIDNIGHT |
static int |
FORMAT_NO_MONTH_DAY |
static int |
FORMAT_NO_NOON |
static int |
FORMAT_NO_NOON_MIDNIGHT
Deprecated.
|
static int |
FORMAT_NO_YEAR |
static int |
FORMAT_NUMERIC_DATE |
static int |
FORMAT_SHOW_DATE |
static int |
FORMAT_SHOW_TIME |
static int |
FORMAT_SHOW_WEEKDAY |
static int |
FORMAT_SHOW_YEAR |
static int |
FORMAT_UTC
Deprecated.
Use
formatDateRange
and pass in Time.TIMEZONE_UTC for the timeZone instead. |
static long |
HOUR_IN_MILLIS |
static String |
HOUR_MINUTE_24
Deprecated.
use
SimpleDateFormat instead. |
static int |
LENGTH_LONG
Deprecated.
use
SimpleDateFormat instead. |
static int |
LENGTH_MEDIUM
Deprecated.
use
SimpleDateFormat instead. |
static int |
LENGTH_SHORT
Deprecated.
use
SimpleDateFormat instead. |
static int |
LENGTH_SHORTER
Deprecated.
use
SimpleDateFormat instead. |
static int |
LENGTH_SHORTEST
Deprecated.
use
SimpleDateFormat instead. |
static long |
MINUTE_IN_MILLIS |
static String |
MONTH_DAY_FORMAT |
static String |
MONTH_FORMAT |
static String |
NUMERIC_MONTH_FORMAT |
static int[] |
sameMonthTable
Deprecated.
do not use.
|
static int[] |
sameYearTable
Deprecated.
do not use.
|
static long |
SECOND_IN_MILLIS |
static long |
WEEK_IN_MILLIS |
static String |
WEEKDAY_FORMAT |
static String |
YEAR_FORMAT |
static String |
YEAR_FORMAT_TWO_DIGITS |
static long |
YEAR_IN_MILLIS
This constant is actually the length of 364 days, not of a year!
|
Constructor and Description |
---|
DateUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
assign(Calendar lval,
Calendar rval)
Deprecated.
use
Time |
static Formatter |
formatDateRange(Context context,
Formatter formatter,
long startMillis,
long endMillis,
int flags)
Formats a date or a time range according to the local conventions.
|
static Formatter |
formatDateRange(Context context,
Formatter formatter,
long startMillis,
long endMillis,
int flags,
String timeZone)
Formats a date or a time range according to the local conventions.
|
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.
|
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)
Deprecated.
use
SimpleDateFormat instead. |
static String |
getDayOfWeekString(int dayOfWeek,
int abbrev)
Deprecated.
use
SimpleDateFormat instead. |
static String |
getMonthString(int month,
int abbrev)
Deprecated.
use
SimpleDateFormat instead. |
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(Context c,
long millis)
Convenience function to return relative time string without preposition.
|
static CharSequence |
getRelativeTimeSpanString(Context c,
long millis,
boolean withPreposition) |
static CharSequence |
getRelativeTimeSpanString(long startTime)
Returns a string describing the elapsed time since startTime.
|
static CharSequence |
getRelativeTimeSpanString(long time,
long now,
long minResolution)
Returns a string describing 'time' as a time relative to 'now'.
|
static CharSequence |
getRelativeTimeSpanString(long time,
long now,
long minResolution,
int flags)
Returns a string describing 'time' as a time relative to 'now'.
|
static String |
getStandaloneMonthString(int month,
int abbrev)
Deprecated.
use
SimpleDateFormat instead. |
static boolean |
isToday(long when) |
static boolean |
isUTC(String s)
Deprecated.
use
Time
Return true if this date string is local time |
static Calendar |
newCalendar(boolean zulu)
Deprecated.
use
Time |
static CharSequence |
timeString(long millis)
Deprecated.
use
#DateFormat.getTimeFormat(Context) instead. |
static String |
writeDateTime(Calendar cal)
Deprecated.
use
Time |
static String |
writeDateTime(Calendar cal,
boolean zulu)
Deprecated.
use
Time |
static String |
writeDateTime(Calendar cal,
StringBuilder sb)
Deprecated.
use
Time |
public static final long SECOND_IN_MILLIS
public static final long MINUTE_IN_MILLIS
public static final long HOUR_IN_MILLIS
public static final long DAY_IN_MILLIS
public static final long WEEK_IN_MILLIS
public static final long YEAR_IN_MILLIS
public static final int FORMAT_SHOW_TIME
public static final int FORMAT_SHOW_WEEKDAY
public static final int FORMAT_SHOW_YEAR
public static final int FORMAT_NO_YEAR
public static final int FORMAT_SHOW_DATE
public static final int FORMAT_NO_MONTH_DAY
@Deprecated public static final int FORMAT_12HOUR
@Deprecated public static final int FORMAT_24HOUR
@Deprecated public static final int FORMAT_CAP_AMPM
public static final int FORMAT_NO_NOON
@Deprecated public static final int FORMAT_CAP_NOON
public static final int FORMAT_NO_MIDNIGHT
@Deprecated public static final int FORMAT_CAP_MIDNIGHT
@Deprecated public static final int FORMAT_UTC
public static final int FORMAT_ABBREV_TIME
public static final int FORMAT_ABBREV_WEEKDAY
public static final int FORMAT_ABBREV_MONTH
public static final int FORMAT_NUMERIC_DATE
public static final int FORMAT_ABBREV_RELATIVE
public static final int FORMAT_ABBREV_ALL
@Deprecated public static final int FORMAT_CAP_NOON_MIDNIGHT
@Deprecated public static final int FORMAT_NO_NOON_MIDNIGHT
@Deprecated public static final String HOUR_MINUTE_24
SimpleDateFormat
instead.public static final String MONTH_FORMAT
@Deprecated public static final String ABBREV_MONTH_FORMAT
SimpleDateFormat
instead.public static final String NUMERIC_MONTH_FORMAT
public static final String MONTH_DAY_FORMAT
public static final String YEAR_FORMAT
public static final String YEAR_FORMAT_TWO_DIGITS
public static final String WEEKDAY_FORMAT
public static final String ABBREV_WEEKDAY_FORMAT
public static final int[] sameYearTable
public static final int[] sameMonthTable
@Deprecated public static final int LENGTH_LONG
SimpleDateFormat
instead.getDayOfWeekString(int, int)
and getMonthString(int, int)
.@Deprecated public static final int LENGTH_MEDIUM
SimpleDateFormat
instead.getDayOfWeekString(int, int)
and getMonthString(int, int)
.@Deprecated public static final int LENGTH_SHORT
SimpleDateFormat
instead.getDayOfWeekString(int, int)
and getMonthString(int, int)
.@Deprecated public static final int LENGTH_SHORTER
SimpleDateFormat
instead.LENGTH_SHORT
.@Deprecated public static final int LENGTH_SHORTEST
SimpleDateFormat
instead.getDayOfWeekString(int, int)
and getMonthString(int, int)
.@Deprecated public static String getDayOfWeekString(int dayOfWeek, int abbrev)
SimpleDateFormat
instead.dayOfWeek
- One of Calendar.SUNDAY
,
Calendar.MONDAY
, etc.abbrev
- One of LENGTH_LONG
, LENGTH_SHORT
,
LENGTH_MEDIUM
, or LENGTH_SHORTEST
.
Note that in most languages, LENGTH_SHORT
will return the same as LENGTH_MEDIUM
.
Undefined lengths will return LENGTH_MEDIUM
but may return something different in the future.IndexOutOfBoundsException
- if the dayOfWeek is out of bounds.@Deprecated public static String getAMPMString(int ampm)
SimpleDateFormat
instead.ampm
- Either Calendar.AM
or Calendar.PM
.IndexOutOfBoundsException
- if the ampm is out of bounds.@Deprecated public static String getMonthString(int month, int abbrev)
SimpleDateFormat
instead.month
- One of Calendar.JANUARY
,
Calendar.FEBRUARY
, etc.abbrev
- One of LENGTH_LONG
, LENGTH_MEDIUM
,
or LENGTH_SHORTEST
.
Undefined lengths will return LENGTH_MEDIUM
but may return something different in the future.@Deprecated public static String getStandaloneMonthString(int month, int abbrev)
SimpleDateFormat
instead.month
- One of Calendar.JANUARY
,
Calendar.FEBRUARY
, etc.abbrev
- One of LENGTH_LONG
, LENGTH_MEDIUM
,
or LENGTH_SHORTEST
.
Undefined lengths will return LENGTH_MEDIUM
but may return something different in the future.public static CharSequence getRelativeTimeSpanString(long startTime)
startTime
- some time in the past.getRelativeTimeSpanString(long, long, long)
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution)
Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "in 42 minutes".
time
- the time to describe, in millisecondsnow
- the current time in millisecondsminResolution
- 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_MILLISpublic static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution, int flags)
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".
time
- the time to describe, in millisecondsnow
- the current time in millisecondsminResolution
- 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_MILLISflags
- a bit mask of formatting options, such as
FORMAT_NUMERIC_DATE
or
FORMAT_ABBREV_RELATIVE
public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution, long transitionResolution, int flags)
Example output strings for the US date format.
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 final CharSequence timeString(long millis)
#DateFormat.getTimeFormat(Context)
instead.public static String formatElapsedTime(long elapsedSeconds)
elapsedSeconds
- the elapsed time in seconds.public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds)
recycle
- StringBuilder
to recycle, if possibleelapsedSeconds
- the elapsed time in seconds.public static final CharSequence formatSameDayTime(long then, long now, int dateStyle, int timeStyle)
The parameters dateFormat and timeFormat should each be one of
DateFormat.DEFAULT
,
DateFormat.FULL
,
DateFormat.LONG
,
DateFormat.MEDIUM
or
DateFormat.SHORT
then
- the date to formatnow
- the base timedateStyle
- how to format the date portion.timeStyle
- how to format the time portion.public static boolean isToday(long when)
public static boolean isUTC(String s)
Time
Return true if this date string is local timepublic static String writeDateTime(Calendar cal)
Time
cal
- the date and time to writepublic static String writeDateTime(Calendar cal, boolean zulu)
Time
cal
- the date and time to writezulu
- If the calendar is in UTC, pass true, and a Z will
be written at the end as per RFC2445. Otherwise, the time is
considered in localtime.public static String writeDateTime(Calendar cal, StringBuilder sb)
Time
cal
- the date and time to writesb
- a StringBuilder to use. It is assumed that setLength
has already been called on sb to the appropriate length
which is sb.setLength(zulu ? 16 : 15)public static String formatDateRange(Context context, long startMillis, long endMillis, int flags)
Note that this is a convenience method. Using it involves creating an
internal Formatter
instance on-the-fly, which is
somewhat costly in terms of memory and time. This is probably acceptable
if you use the method only rarely, but if you rely on it for formatting a
large number of dates, consider creating and reusing your own
Formatter
instance and use the version of
formatDateRange
that takes a Formatter
.
context
- the context is required only if the time is shownstartMillis
- the start time in UTC millisecondsendMillis
- the end time in UTC millisecondsflags
- a bit mask of options See
formatDateRange
public static Formatter formatDateRange(Context context, Formatter formatter, long startMillis, long endMillis, int flags)
Note that this is a convenience method for formatting the date or
time range in the local time zone. If you want to specify the time
zone please use
formatDateRange
.
context
- the context is required only if the time is shownformatter
- the Formatter used for formatting the date range.
Note: be sure to call setLength(0) on StringBuilder passed to
the Formatter constructor unless you want the results to accumulate.startMillis
- the start time in UTC millisecondsendMillis
- the end time in UTC millisecondsflags
- a bit mask of options See
formatDateRange
public static Formatter formatDateRange(Context context, Formatter formatter, long startMillis, long endMillis, int flags, String timeZone)
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):
The flags argument is a bitmask of options from the following list:
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. You should not use this flag because in some locales these terms cannot be capitalized, and in many others it doesn't make sense to do so even though it is possible.
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". You should probably not use this flag because in many locales it will not make sense to capitalize the term.
If FORMAT_NO_MIDNIGHT is set and 12-hour time is used, then "12am" is shown instead of "midnight".
If FORMAT_CAP_MIDNIGHT is set and 12-hour time is used, then "Midnight" is shown instead of "midnight". You should probably not use this flag because in many locales it will not make sense to capitalize the term.
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 time zone is used for the start and end milliseconds unless a time zone is specified. If a time zone is specified it will be used regardless of the FORMAT_UTC flag.
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".
If the end date ends at 12:00am at the beginning of a day, it is formatted as the end of the previous day in two scenarios:
context
- the context is required only if the time is shownformatter
- the Formatter used for formatting the date range.
Note: be sure to call setLength(0) on StringBuilder passed to
the Formatter constructor unless you want the results to accumulate.startMillis
- the start time in UTC millisecondsendMillis
- the end time in UTC millisecondsflags
- a bit mask of optionstimeZone
- the time zone to compute the string in. Use null for local
or if the FORMAT_UTC flag is being used.public static String formatDateTime(Context context, long millis, int flags)
formatDateRange(android.content.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):
context
- the context is required only if the time is shownmillis
- a point in time in UTC millisecondsflags
- a bit mask of formatting optionspublic static CharSequence getRelativeTimeSpanString(Context c, long millis, boolean withPreposition)
withPreposition
- If true, the string returned will include the correct
preposition ("at 9:20am", "on 10/12/2008" or "on May 29").public static CharSequence getRelativeTimeSpanString(Context c, long millis)
c
- context for resourcesmillis
- time in millisecondsCharSequence
containing relative time.getRelativeTimeSpanString(Context, long, boolean)