5

It may sound very funny and basic, but I am struggling with creating a DateTime kind of object in Kotlin by passing the number of seconds from epoch to it.

Either the examples that I am getting are of the libraries that require API level 26, or just talk about format conversion from string of DD-MM-YYYY to some other.

I have tried "LocalDateTime", "Timestamp", "Date" so far, but Kotlin either refuses to recognise them or the doesn't agree to the function parameters, or is unable to resolve namespace conflict. Or the examples are of Java, which don't work in Kotlin or don't have equivalent in Kotlin.

With Date() I am getting error "Cannot access '': It is private in Date()"

With LocalDate, LocalDateTime, I am getting the API level error

With Timestamp I am getting error "None of the following functions can be called with the arguments supplied". I am passing Long as the parameter as mentioned in documentation. But it is expecting "Parcel" or "Date".

Could someone please help?

Mukesh Ghatiya
  • 398
  • 1
  • 5
  • 15

3 Answers3

10

Update: In recent versions of Android tooling, API desugaring brings to older versions of Android much of the java.time functionality that is built into Android 26 and later.

enter image description here


tl;dr

Avoid legacy date-time classes

The legacy classes such as Date & Calendar are terrible, created by people who did not understand date-time handling. These classes were supplanted years ago by the modern java.time classes defined in JSR 310.

java.time

The Instant class replaces the Date class, both representing a moment in UTC (an offset of zero hours-minutes-seconds).

Instant instant = Instant.now() ;  // Capture the current moment in UTC.

The Calendar class, or more precisely, its most commonly-used concrete subclass GregorianCalendar is replaced by ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;  // Capture the current moment as seen in the wall-clock time used by the people of a particular region (a time zone). 

Table of all date-time types in Java, both modern and legacy

Convert

Best to avoid the legacy date-time classes entirely. But sometimes you may need to interoperate with old code not yet updated to java.time. In that case, convert. Look to new to…/from… conversion methods added to the old classes.

Calendar calendar = GregorianCalendar.from( zdt ) ;

…and…

ZonedDateTime zdt = ( ( GregorianCalendar ) calendar ).toZonedDateTime() ;

Back-port

The java.time classes are built into Java 8 and later, and Android 26 and later.

For Java 6 & 7, use the back-port found in the ThreeTen-Backport project. Most of the java.time functionality is found there. This project is led by the same man leading the java.time project and JSR 310, Stephen Colebourne.

For Android before 26, use the adaptation of that back-port to Android, the ThreeTenABP project.

Table of which java.time library to use with which version of Java or Android

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

I use Calendar for Kotlin

val date = Calendar.getInstance()
date.timeInMillis = 5000000
txtDate.text = date.time.toString()
Chooven
  • 105
  • 5
  • Thanks. This worked! Now my struggle is going to be to get Firestore's Timestamp working, which has been giving me headache as well. But that's a different topic I guess. – Mukesh Ghatiya Oct 18 '19 at 19:32
  • FYI, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, & `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes. Most *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Oct 18 '19 at 20:58
0

Does this work for you?

Instant.fromEpochSeconds(<number>)
bjartek
  • 929
  • 1
  • 5
  • 11
  • ```Instant.fromEpochSeconds(t)``` gives error of unresolved reference. ```Instant.ofEpochSecond(t)``` gives error of API level – Mukesh Ghatiya Oct 18 '19 at 19:10