4

I am coding an Android application, which should receive a java.time.LocalDateTime object through an HTTP REST API that I developed in the server side.

The problem is that Android is still in Java 7, and java.time is only available in Java 8.

Having that said, what is the best way to represent a variable that contains date and time in Android? I prefer not to use sql.timestamp.

Thank you!

Fabio Lanza
  • 167
  • 2
  • 5
  • 14

5 Answers5

5

Back-port

You could use the ThreeTen Android Backport which is an Android adaption to the original ThreeTen-Backport that backports much of the new java.time api back to Java SE 6 and SE 7.

See this answer to get started.

Community
  • 1
  • 1
Marvin
  • 13,325
  • 3
  • 51
  • 57
3

Why bother you using Date or Calendar previous to Java 8 while the Android version of Joda-time (that is very close from the Java 8 dates) is available ?

Here is the GIT repo/website for more information :

https://github.com/dlew/joda-time-android

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 3
    The Joda-Time project is in maintenance mode. The team advises migration to the java.time classes. Much of the java.time functionality is back-ported to Java 6 & 7 in the *ThreeTen-Backport* project, and further adapted to Android in the *ThreeTenABP* project. – Basil Bourque Apr 16 '17 at 19:24
  • @Basil Bourque Thank you for these precisions. – davidxxx Apr 16 '17 at 19:35
  • Joda time library adds about 5k methods – resource8218 Apr 02 '18 at 22:35
2

You can use LocalDateTime if your minSdkVersion is 26 (Android 8.0) or higher 1. Otherwise, you can use core library desugaring.

android {
    defaultConfig {
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled true
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
Cristan
  • 12,083
  • 7
  • 65
  • 69
  • hello, yes I see im implementing this option and runs well but when I run it on a lower version of API 26 crashes because does not find LocalDateTime, what would you do in this case? implement threaten? – MACROSystems Jun 14 '22 at 10:54
  • 1
    I have used this extensively and it works well on older Android versions. Did you set `coreLibraryDesugaringEnabled true` in all of your modules? – Cristan Jun 14 '22 at 11:33
  • ahhhh, yes, Im developing a Library (not experienced on them) but yes I see this flag should be added to the app module that implements the library as well, thank you colleague. – MACROSystems Jun 14 '22 at 12:26
1

Use the java.util.Calendar which is available in Java 7. Refer to the link below for more information: https://developer.android.com/reference/java/util/Calendar.html

Calendar rightNow = Calendar.getInstance(); //initialized with the current date and time
Jie Heng
  • 196
  • 4
  • 2
    The troublesome old date-time classes such as `Calendar` are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & 7 in the *ThreeTen-Backport* project, and further adapted to Android in the *ThreeTenABP* project. – Basil Bourque Apr 16 '17 at 19:26
0

a LocalDateTime object has been serialized to a string|long JSON property through an HTTP REST API.

so you just to do is adaptee the JSON property to a Date object, and format a Date object to the JSON property if you want to send a Date object to the server which only accept a LocalDateTime object.

almost all the JSON library providing a dateFormat for serializing/deserializing a string to a Date, and convert long timestamp to Date is so simple:

Date date = new Date(timestamp);
holi-java
  • 29,655
  • 7
  • 72
  • 83