to top
Android APIs
public class

Time

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

Class Overview

An alternative to the Calendar and GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision. It is modelled after struct tm, and in fact, uses struct tm to implement most of the functionality.

Summary

Constants
int EPOCH_JULIAN_DAY The Julian day of the epoch, that is, January 1, 1970 on the Gregorian calendar.
int FRIDAY
int HOUR
int MINUTE
int MONDAY
int MONDAY_BEFORE_JULIAN_EPOCH The Julian day of the Monday in the week of the epoch, December 29, 1969 on the Gregorian calendar.
int MONTH
int MONTH_DAY
int SATURDAY
int SECOND
int SUNDAY
int THURSDAY
String TIMEZONE_UTC
int TUESDAY
int WEDNESDAY
int WEEK_DAY
int WEEK_NUM
int YEAR
int YEAR_DAY
Fields
public boolean allDay True if this is an allDay event.
public long gmtoff Offset from UTC (in seconds).
public int hour Hour of day [0-23]
public int isDst This time is in daylight savings time.
public int minute Minute [0-59]
public int month Month [0-11]
public int monthDay Day of month [1-31]
public int second Seconds [0-61] (2 leap seconds allowed)
public String timezone The timezone for this Time.
public int weekDay Day of week [0-6]
public int year Year.
public int yearDay Day of year [0-365]
Public Constructors
Time(String timezone)
Construct a Time object in the timezone named by the string argument "timezone".
Time()
Construct a Time object in the default timezone.
Time(Time other)
A copy constructor.
Public Methods
boolean after(Time that)
Returns true if the time represented by this Time object occurs after the given time.
boolean before(Time that)
Returns true if the time represented by this Time object occurs before the given time.
void clear(String timezone)
Clears all values, setting the timezone to the given timezone.
static int compare(Time a, Time b)
Compare two Time objects and return a negative number if a is less than b, a positive number if a is greater than b, or 0 if they are equal.
String format(String format)
Print the current value given the format string provided.
String format2445()
Format according to RFC 2445 DATETIME type.
String format3339(boolean allDay)
Return a string in the RFC 3339 format.
int getActualMaximum(int field)
Return the maximum possible value for the given field given the value of the other fields.
static String getCurrentTimezone()
Returns the timezone string that is currently set for the device.
static int getJulianDay(long millis, long gmtoff)
Computes the Julian day number, given the UTC milliseconds and the offset (in seconds) from UTC.
static int getJulianMondayFromWeeksSinceEpoch(int week)
Takes a number of weeks since the epoch and calculates the Julian day of the Monday for that week.
int getWeekNumber()
Computes the week number according to ISO 8601.
static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek)
Returns the week since EPOCH_JULIAN_DAY (Jan 1, 1970) adjusted for first day of week.
static boolean isEpoch(Time time)
Returns true if the day of the given time is the epoch on the Julian Calendar (January 1, 1970 on the Gregorian calendar).
long normalize(boolean ignoreDst)
Ensures the values in each field are in range.
boolean parse(String s)
Parses a date-time string in either the RFC 2445 format or an abbreviated format that does not include the "time" field.
boolean parse3339(String s)
Parse a time in RFC 3339 format.
void set(int second, int minute, int hour, int monthDay, int month, int year)
Sets the fields.
void set(int monthDay, int month, int year)
Sets the date from the given fields.
void set(Time that)
Copy the value of that to this Time object.
void set(long millis)
Sets the fields in this Time object given the UTC milliseconds.
long setJulianDay(int julianDay)

Sets the time from the given Julian day number, which must be based on the same timezone that is set in this Time object.

void setToNow()
Sets the time of the given Time object to the current time.
void switchTimezone(String timezone)
Convert this time object so the time represented remains the same, but is instead located in a different timezone.
long toMillis(boolean ignoreDst)
Converts this time to milliseconds.
String toString()
Return the current time in YYYYMMDDTHHMMSS format
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int EPOCH_JULIAN_DAY

Added in API level 3

The Julian day of the epoch, that is, January 1, 1970 on the Gregorian calendar.

Constant Value: 2440588 (0x00253d8c)

public static final int FRIDAY

Added in API level 3

Constant Value: 5 (0x00000005)

public static final int HOUR

Added in API level 3

Constant Value: 3 (0x00000003)

public static final int MINUTE

Added in API level 3

Constant Value: 2 (0x00000002)

public static final int MONDAY

Added in API level 3

Constant Value: 1 (0x00000001)

public static final int MONDAY_BEFORE_JULIAN_EPOCH

Added in API level 11

The Julian day of the Monday in the week of the epoch, December 29, 1969 on the Gregorian calendar.

Constant Value: 2440585 (0x00253d89)

public static final int MONTH

Added in API level 3

Constant Value: 5 (0x00000005)

public static final int MONTH_DAY

Added in API level 3

Constant Value: 4 (0x00000004)

public static final int SATURDAY

Added in API level 3

Constant Value: 6 (0x00000006)

public static final int SECOND

Added in API level 3

Constant Value: 1 (0x00000001)

public static final int SUNDAY

Added in API level 3

Constant Value: 0 (0x00000000)

public static final int THURSDAY

Added in API level 3

Constant Value: 4 (0x00000004)

public static final String TIMEZONE_UTC

Added in API level 3

Constant Value: "UTC"

public static final int TUESDAY

Added in API level 3

Constant Value: 2 (0x00000002)

public static final int WEDNESDAY

Added in API level 3

Constant Value: 3 (0x00000003)

public static final int WEEK_DAY

Added in API level 3

Constant Value: 7 (0x00000007)

public static final int WEEK_NUM

Added in API level 3

Constant Value: 9 (0x00000009)

public static final int YEAR

Added in API level 3

Constant Value: 6 (0x00000006)

public static final int YEAR_DAY

Added in API level 3

Constant Value: 8 (0x00000008)

Fields

public boolean allDay

Added in API level 3

True if this is an allDay event. The hour, minute, second fields are all zero, and the date is displayed the same in all time zones.

public long gmtoff

Added in API level 3

Offset from UTC (in seconds).

public int hour

Added in API level 3

Hour of day [0-23]

public int isDst

Added in API level 3

This time is in daylight savings time. One of:

  • positive - in dst
  • 0 - not in dst
  • negative - unknown

public int minute

Added in API level 3

Minute [0-59]

public int month

Added in API level 3

Month [0-11]

public int monthDay

Added in API level 3

Day of month [1-31]

public int second

Added in API level 3

Seconds [0-61] (2 leap seconds allowed)

public String timezone

Added in API level 3

The timezone for this Time. Should not be null.

public int weekDay

Added in API level 3

Day of week [0-6]

public int year

Added in API level 3

Year. For example, 1970.

public int yearDay

Added in API level 3

Day of year [0-365]

Public Constructors

public Time (String timezone)

Added in API level 3

Construct a Time object in the timezone named by the string argument "timezone". The time is initialized to Jan 1, 1970.

Parameters
timezone string containing the timezone to use.
See Also

public Time ()

Added in API level 3

Construct a Time object in the default timezone. The time is initialized to Jan 1, 1970.

public Time (Time other)

Added in API level 3

A copy constructor. Construct a Time object by copying the given Time object. No normalization occurs.

Public Methods

public boolean after (Time that)

Added in API level 3

Returns true if the time represented by this Time object occurs after the given time.

Parameters
that a given Time object to compare against
Returns
  • true if this time is greater than the given time

public boolean before (Time that)

Added in API level 3

Returns true if the time represented by this Time object occurs before the given time.

Parameters
that a given Time object to compare against
Returns
  • true if this time is less than the given time

public void clear (String timezone)

Added in API level 3

Clears all values, setting the timezone to the given timezone. Sets isDst to a negative value to mean "unknown".

Parameters
timezone the timezone to use.

public static int compare (Time a, Time b)

Added in API level 3

Compare two Time objects and return a negative number if a is less than b, a positive number if a is greater than b, or 0 if they are equal.

Parameters
a first Time instance to compare
b second Time instance to compare
Returns
  • a negative result if a is earlier, a positive result if a is earlier, or 0 if they are equal.
Throws
NullPointerException if either argument is null
IllegalArgumentException if allDay is true but hour, minute, and second are not 0.

public String format (String format)

Added in API level 3

Print the current value given the format string provided. See man strftime for what means what. The final string must be less than 256 characters.

Parameters
format a string containing the desired format.
Returns
  • a String containing the current time expressed in the current locale.

public String format2445 ()

Added in API level 3

Format according to RFC 2445 DATETIME type.

The same as format("%Y%m%dT%H%M%S").

public String format3339 (boolean allDay)

Added in API level 3

Return a string in the RFC 3339 format.

If allDay is true, expresses the time as Y-M-D

Otherwise, if the timezone is UTC, expresses the time as Y-M-D-T-H-M-S UTC

Otherwise the time is expressed the time as Y-M-D-T-H-M-S +- GMT

Returns
  • string in the RFC 3339 format.

public int getActualMaximum (int field)

Added in API level 3

Return the maximum possible value for the given field given the value of the other fields. Requires that it be normalized for MONTH_DAY and YEAR_DAY.

Parameters
field one of the constants for HOUR, MINUTE, SECOND, etc.
Returns
  • the maximum value for the field.

public static String getCurrentTimezone ()

Added in API level 3

Returns the timezone string that is currently set for the device.

public static int getJulianDay (long millis, long gmtoff)

Added in API level 3

Computes the Julian day number, given the UTC milliseconds and the offset (in seconds) from UTC. The Julian day for a given date will be the same for every timezone. For example, the Julian day for July 1, 2008 is 2454649. This is the same value no matter what timezone is being used. The Julian day is useful for testing if two events occur on the same day and for determining the relative time of an event from the present ("yesterday", "3 days ago", etc.).

Use toMillis(boolean) to get the milliseconds.

Parameters
millis the time in UTC milliseconds
gmtoff the offset from UTC in seconds
Returns
  • the Julian day

public static int getJulianMondayFromWeeksSinceEpoch (int week)

Added in API level 11

Takes a number of weeks since the epoch and calculates the Julian day of the Monday for that week. This assumes that the week containing the EPOCH_JULIAN_DAY is considered week 0. It returns the Julian day for the Monday week weeks after the Monday of the week containing the epoch.

Parameters
week Number of weeks since the epoch
Returns
  • The julian day for the Monday of the given week since the epoch

public int getWeekNumber ()

Added in API level 3

Computes the week number according to ISO 8601. The current Time object must already be normalized because this method uses the yearDay and weekDay fields.

In IS0 8601, weeks start on Monday. The first week of the year (week 1) is defined by ISO 8601 as the first week with four or more of its days in the starting year. Or equivalently, the week containing January 4. Or equivalently, the week with the year's first Thursday in it.

The week number can be calculated by counting Thursdays. Week N contains the Nth Thursday of the year.

Returns
  • the ISO week number.

public static int getWeeksSinceEpochFromJulianDay (int julianDay, int firstDayOfWeek)

Added in API level 11

Returns the week since EPOCH_JULIAN_DAY (Jan 1, 1970) adjusted for first day of week. This takes a julian day and the week start day and calculates which week since EPOCH_JULIAN_DAY that day occurs in, starting at 0. *Do not* use this to compute the ISO week number for the year.

Parameters
julianDay The julian day to calculate the week number for
firstDayOfWeek Which week day is the first day of the week, see SUNDAY
Returns
  • Weeks since the epoch

public static boolean isEpoch (Time time)

Added in API level 3

Returns true if the day of the given time is the epoch on the Julian Calendar (January 1, 1970 on the Gregorian calendar).

Parameters
time the time to test
Returns
  • true if epoch.

public long normalize (boolean ignoreDst)

Added in API level 3

Ensures the values in each field are in range. For example if the current value of this calendar is March 32, normalize() will convert it to April 1. It also fills in weekDay, yearDay, isDst and gmtoff.

If "ignoreDst" is true, then this method sets the "isDst" field to -1 (the "unknown" value) before normalizing. It then computes the correct value for "isDst".

See toMillis(boolean) for more information about when to use true or false for "ignoreDst".

Returns
  • the UTC milliseconds since the epoch

public boolean parse (String s)

Added in API level 3

Parses a date-time string in either the RFC 2445 format or an abbreviated format that does not include the "time" field. For example, all of the following strings are valid:

  • "20081013T160000Z"
  • "20081013T160000"
  • "20081013"
Returns whether or not the time is in UTC (ends with Z). If the string ends with "Z" then the timezone is set to UTC. If the date-time string included only a date and no time field, then the allDay field of this Time class is set to true and the hour, minute, and second fields are set to zero; otherwise (a time field was included in the date-time string) allDay is set to false. The fields weekDay, yearDay, and gmtoff are always set to zero, and the field isDst is set to -1 (unknown). To set those fields, call normalize(boolean) after parsing. To parse a date-time string and convert it to UTC milliseconds, do something like this:
   Time time = new Time();
   String date = "20081013T160000Z";
   time.parse(date);
   long millis = time.normalize(false);
 

Parameters
s the string to parse
Returns
  • true if the resulting time value is in UTC time
Throws
TimeFormatException if s cannot be parsed.

public boolean parse3339 (String s)

Added in API level 3

Parse a time in RFC 3339 format. This method also parses simple dates (that is, strings that contain no time or time offset). For example, all of the following strings are valid:

  • "2008-10-13T16:00:00.000Z"
  • "2008-10-13T16:00:00.000+07:00"
  • "2008-10-13T16:00:00.000-07:00"
  • "2008-10-13"

If the string contains a time and time offset, then the time offset will be used to convert the time value to UTC.

If the given string contains just a date (with no time field), then the allDay field is set to true and the hour, minute, and second fields are set to zero.

Returns true if the resulting time value is in UTC time.

Parameters
s the string to parse
Returns
  • true if the resulting time value is in UTC time
Throws
TimeFormatException if s cannot be parsed.

public void set (int second, int minute, int hour, int monthDay, int month, int year)

Added in API level 3

Sets the fields. Sets weekDay, yearDay and gmtoff to 0, and isDst to -1. Call normalize(boolean) if you need those.

public void set (int monthDay, int month, int year)

Added in API level 3

Sets the date from the given fields. Also sets allDay to true. Sets weekDay, yearDay and gmtoff to 0, and isDst to -1. Call normalize(boolean) if you need those.

Parameters
monthDay the day of the month (in the range [1,31])
month the zero-based month number (in the range [0,11])
year the year

public void set (Time that)

Added in API level 3

Copy the value of that to this Time object. No normalization happens.

public void set (long millis)

Added in API level 3

Sets the fields in this Time object given the UTC milliseconds. After this method returns, all the fields are normalized. This also sets the "isDst" field to the correct value.

Parameters
millis the time in UTC milliseconds since the epoch.

public long setJulianDay (int julianDay)

Added in API level 3

Sets the time from the given Julian day number, which must be based on the same timezone that is set in this Time object. The "gmtoff" field need not be initialized because the given Julian day may have a different GMT offset than whatever is currently stored in this Time object anyway. After this method returns all the fields will be normalized and the time will be set to 12am at the beginning of the given Julian day.

The only exception to this is if 12am does not exist for that day because of daylight saving time. For example, Cairo, Eqypt moves time ahead one hour at 12am on April 25, 2008 and there are a few other places that also change daylight saving time at 12am. In those cases, the time will be set to 1am.

Parameters
julianDay the Julian day in the timezone for this Time object
Returns
  • the UTC milliseconds for the beginning of the Julian day

public void setToNow ()

Added in API level 3

Sets the time of the given Time object to the current time.

public void switchTimezone (String timezone)

Added in API level 3

Convert this time object so the time represented remains the same, but is instead located in a different timezone. This method automatically calls normalize() in some cases

public long toMillis (boolean ignoreDst)

Added in API level 3

Converts this time to milliseconds. Suitable for interacting with the standard java libraries. The time is in UTC milliseconds since the epoch. This does an implicit normalization to compute the milliseconds but does not change any of the fields in this Time object. If you want to normalize the fields in this Time object and also get the milliseconds then use normalize(boolean).

If "ignoreDst" is false, then this method uses the current setting of the "isDst" field and will adjust the returned time if the "isDst" field is wrong for the given time. See the sample code below for an example of this.

If "ignoreDst" is true, then this method ignores the current setting of the "isDst" field in this Time object and will instead figure out the correct value of "isDst" (as best it can) from the fields in this Time object. The only case where this method cannot figure out the correct value of the "isDst" field is when the time is inherently ambiguous because it falls in the hour that is repeated when switching from Daylight-Saving Time to Standard Time.

Here is an example where toMillis(true) adjusts the time, assuming that DST changes at 2am on Sunday, Nov 4, 2007.

 Time time = new Time();
 time.set(4, 10, 2007);  // set the date to Nov 4, 2007, 12am
 time.normalize(false);       // this sets isDst = 1
 time.monthDay += 1;     // changes the date to Nov 5, 2007, 12am
 millis = time.toMillis(false);   // millis is Nov 4, 2007, 11pm
 millis = time.toMillis(true);    // millis is Nov 5, 2007, 12am
 

To avoid this problem, use toMillis(true) after adding or subtracting days or explicitly setting the "monthDay" field. On the other hand, if you are adding or subtracting hours or minutes, then you should use toMillis(false).

You should also use toMillis(false) if you want to read back the same milliseconds that you set with set(long) or set(Time) or after parsing a date string.

public String toString ()

Added in API level 3

Return the current time in YYYYMMDDTHHMMSS format

Returns
  • a printable representation of this object.