# Class DateTime (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.util.DateTime)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.DateTime)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.DateTime)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.DateTime)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.DateTime)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.DateTime)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.DateTime)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.DateTime)

    public final class DateTime implements Serializable

Immutable representation of a date with an optional time and an optional time zone based on [RFC 3339](http://tools.ietf.org/html/rfc3339).

Implementation is immutable and therefore thread-safe.

## Inheritance

[Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> DateTime

## Implements

[Serializable](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html)

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Methods

### parseRfc3339(String str)

    public static DateTime parseRfc3339(String str)

Parses an RFC3339 date/time value.

Upgrade warning: in prior version 1.17, this method required milliseconds to be exactly 3
digits (if included), and did not throw an exception for all types of invalid input values, but
starting in version 1.18, the parsing done by this method has become more strict to enforce
that only valid RFC3339 strings are entered, and if not, it throws a NumberFormatException. Also, in accordance with the RFC3339 standard, any number of
milliseconds digits is now allowed.

Any time information beyond millisecond precision is truncated.

For the date-only case, the time zone is ignored and the hourOfDay, minute, second, and
millisecond parameters are set to zero.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `str` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` Date/time string in RFC3339 format |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime` |   |

### parseRfc3339ToSecondsAndNanos(String str)

    public static DateTime.SecondsAndNanos parseRfc3339ToSecondsAndNanos(String str)

Parses an RFC3339 timestamp to a pair of seconds and nanoseconds since Unix Epoch.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `str` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` Date/time string in RFC3339 format |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime.SecondsAndNanos` |   |

## Constructors

### DateTime(boolean dateOnly, long value, Integer tzShift)

    public DateTime(boolean dateOnly, long value, Integer tzShift)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime), which may represent a date-only value, from the number of
milliseconds since the Unix epoch, and a shift from UTC in minutes.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `dateOnly` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` specifies if this should represent a date-only value |
| `value` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT) |
| `tzShift` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html` time zone, represented by the number of minutes off of UTC, or `null` for `TimeZone.getDefault()`. |

### DateTime(String value)

    public DateTime(String value)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime) from an [RFC 3339](http://tools.ietf.org/html/rfc3339)
date/time value.

Upgrade warning: in prior version 1.17, this method required milliseconds to be exactly 3
digits (if included), and did not throw an exception for all types of invalid input values, but
starting in version 1.18, the parsing done by this method has become more strict to enforce
that only valid RFC3339 strings are entered, and if not, it throws a NumberFormatException. Also, in accordance with the RFC3339 standard, any number of
milliseconds digits is now allowed.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` an [RFC 3339](http://tools.ietf.org/html/rfc3339) date/time value. |

### DateTime(Date value)

    public DateTime(Date value)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime) from a [Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html).

The time zone is interpreted as `TimeZone.getDefault()`, which may vary with
implementation.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/8/docs/api/java/util/Date.html` date and time |

### DateTime(Date date, TimeZone zone)

    public DateTime(Date date, TimeZone zone)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime) from a [Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) and [TimeZone](https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `date` | `https://docs.oracle.com/javase/8/docs/api/java/util/Date.html` date and time |
| `zone` | `https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html` time zone; if `null`, it is interpreted as `TimeZone.getDefault()`. |

### DateTime(long value)

    public DateTime(long value)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime) from the number of milliseconds since the Unix epoch.

The time zone is interpreted as `TimeZone.getDefault()`, which may vary with
implementation.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT) |

### DateTime(long value, int tzShift)

    public DateTime(long value, int tzShift)

Instantiates [DateTime](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime) from the number of milliseconds since the Unix epoch, and a shift
from UTC in minutes.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT) |
| `tzShift` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` time zone, represented by the number of minutes off of UTC. |

## Methods

### equals(Object o)

    public boolean equals(Object o)

A check is added that the time zone is the same. If you ONLY want to check equality of time
value, check equality on the [#getValue()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.DateTime#com_google_api_client_util_DateTime_getValue__).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `o` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

**Overrides** [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-)

### getTimeZoneShift()

    public int getTimeZoneShift()

Returns the time zone shift from UTC in minutes or `0` for date-only value.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### getValue()

    public long getValue()

Returns the date/time value expressed as the number of milliseconds since the Unix epoch.

If the time zone is specified, this value is normalized to UTC, so to format this date/time
value, the time zone shift has to be applied.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### hashCode()

    public int hashCode()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

**Overrides** [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--)

### isDateOnly()

    public boolean isDateOnly()

Returns whether this is a date-only value.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### toString()

    public String toString()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

**Overrides** [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--)

### toStringRfc3339()

    public String toStringRfc3339()

Formats the value as an RFC 3339 date/time string.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |