Partager via


Time.ToMillis(Boolean) Method

Definition

Converts this time to milliseconds.

[Android.Runtime.Register("toMillis", "(Z)J", "GetToMillis_ZHandler")]
public virtual long ToMillis (bool ignoreDst);
[<Android.Runtime.Register("toMillis", "(Z)J", "GetToMillis_ZHandler")>]
abstract member ToMillis : bool -> int64
override this.ToMillis : bool -> int64

Parameters

ignoreDst
Boolean

Returns

Attributes

Remarks

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 <em>not</em> 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.

This method can return -1 when the date / time fields have been set to a local time that conflicts with available timezone information. For example, when daylight savings transitions cause an hour to be skipped: times within that hour will return -1 if isDst = -1.

Java documentation for android.text.format.Time.toMillis(boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to