---
source_url: https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/utility-methods
title: Utility methods
updated_at: 2026-06-04T11:09:28.313Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Utility methods

Helper methods for working with [PubNub timetokens](https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#time).

## Timetoken to date

Convert a PubNub timetoken (100-nanosecond intervals since January 1, 1970) to an `Instant` object for displaying message timestamps in human-readable format.

### Method signature

```kotlin
TimetokenUtil.timetokenToInstant(timetoken: Long): Instant
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| timetoken | Long | Yes |  | Represents the PubNub timetoken to convert into a human-readable date format. |

#### Output

| Type | Description |
| --- | --- |
| `Instant` | The human-readable date representation of the timetoken. |

### Sample code

Convert a timetoken value of `17276954606232118` to a human-readable date and time format.

```kotlin
val timetoken: Long = 17276954606232118
val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
val localDateTimeInUTC = instant.toLocalDateTime(TimeZone.UTC)

println("PubNub timetoken: ${timetoken}")
println("Current date: ${localDateTimeInUTC.date}")
println("Current time: ${localDateTimeInUTC.time}")
```

The output of the method is as follows:

```bash
PubNub timetoken: 17276954606232118
Current time: 11:24:20.623211800
Current date: 2024-09-30
```

## Date to timetoken

Convert an `Instant` object to a PubNub timetoken for retrieving messages at a specific date and time.

### Method signature

```kotlin
TimetokenUtil.instantToTimetoken(instant: Instant): Long
```

#### Input

| Parameter | Description |
| --- | --- |
| `instant` *Type: `Instant`Default: n/a | Represents the date and time to convert into a PubNub timetoken. |

#### Output

| Type | Description |
| --- | --- |
| `Long` | Converted timetoken value. |

### Sample code

Convert a human-readable date and time, `September 30, 2024 12:12:24 GMT`, to a timetoken.

```kotlin
val localDateTime = LocalDateTime(year = 2024, monthNumber = 9, dayOfMonth = 30, hour = 12, minute = 12, second =  44, nanosecond =  123456789)
val zone = TimeZone.currentSystemDefault()
val instant = localDateTime.toInstant(zone)
val timetoken = TimetokenUtil.instantToTimetoken(instant)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("PubNub timetoken: ${timetoken}")
```

```bash
Current date: 2024-09-30
Current time: 12:12:44.123456789
PubNub timetoken: 17276911641234567
```

## Unix timestamp to timetoken

Convert a Unix timestamp (seconds since January 1, 1970) to a PubNub timetoken for [retrieving historical messages](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/history) within a specific time range.

### Method signature

This method takes the following parameters:

```kotlin
TimetokenUtil.unixToTimetoken(unixTime: Long): Long 
```

#### Input

| Parameter | Description |
| --- | --- |
| `unixTime` *Type: `Long`Default: n/a | Represents the Unix timestamp to convert into a PubNub timetoken. |

#### Output

| Type | Description |
| --- | --- |
| `Long` | Converted timetoken value. |

### Sample code

Convert a Unix timestamp value of `1727866935316` representing `2024-10-02 11:02:15.316` to PubNub timetoken:

```kotlin
val unixTime = 1727866935316
val timetoken: Long = TimetokenUtil.unixToTimetoken(unixTime)
val instant: Instant = TimetokenUtil.timetokenToInstant(timetoken)
val localDateTime = instant.toLocalDateTime(TimeZone.UTC)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("PubNub timetoken: ${timetoken}")
```

The output of the method is as follows:

```bash
Current date: 2024-10-02
Current time: 11:02:15.316
PubNub timetoken: 17278669353160000
```

## Timetoken to Unix timestamp

Convert a PubNub timetoken to a Unix timestamp for use with systems requiring Unix time format.

### Method signature

```kotlin
TimetokenUtil.timetokenToUnix(timetoken: Long): Long 
```

#### Input

| Parameter | Description |
| --- | --- |
| `timetoken` *Type: `Long`Default: n/a | Represents the PubNub timetoken to convert into a Unix timestamp. |

#### Output

| Type | Description |
| --- | --- |
| `Long` | Converted Unix timestamp value. |

### Sample code

Convert a PubNub timetoken `17276954606232118` representing `2024-09-30 11:24:20.623211800` to Unix time:

```kotlin
val timetoken = 17276954606232118
val unixTime = TimetokenUtil.timetokenToUnix(timetoken)
val instant = Instant.fromEpochMilliseconds(unixTime)
val localDateTime = instant.toLocalDateTime(TimeZone.UTC)

println("Current date: ${localDateTime.date}")
println("Current time: ${localDateTime.time}")
println("PubNub timetoken: ${timetoken}")
```

The output of the method is as follows:

```bash
Current date: 2024-09-30
Current time: 11:24:20.623
PubNub timetoken: 17276954606232118
```