1

I just found this library to use java.time from Java 8 on Android. In my TimePickerDialog I ask for a time, and after that I have to send a HTTP request to a server with a Date object in it (with the time from the dialog), in this format: "YYYY-MM-DDTHH:MM:SS+HH:MM" I started trying with the usual classes (Calendar, Date, SimpleDateFormat), but after a while I gave up and looked for a cleaner solution and found the ZonedDateTime class from Java 8. It looks like exactly the class I want to use, but when I try to create an instance of it with ZonedDateTime.now(), I get the following stacktrace:

org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered
  at org.threeten.bp.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:176)
  at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:133)
  at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
  at org.threeten.bp.ZoneId.of(ZoneId.java:357)
  at org.threeten.bp.ZoneId.of(ZoneId.java:285)
  at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:244)
  at org.threeten.bp.Clock.systemDefaultZone(Clock.java:137)
  at org.threeten.bp.ZonedDateTime.now(ZonedDateTime.java:168)
  at eu.arrowhead.arrowheaddemo.ReservationsActivity.onTimeSet(ReservationsActivity.java:162)
  at eu.arrowhead.arrowheaddemo.TimePickerFragment.onTimeSet(TimePickerFragment.java:56)
  at android.app.TimePickerDialog.onClick(TimePickerDialog.java:141)
  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:164)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6077)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I did AndroidThreeTen.init(this); as per the documantion in my application class (in onCreate), so I dont know what causes the problem.

Bonus question: once I got it working and I have the date I want to send in the ZonedDateTime object, how do I convert it to a simple java.util.Date ? Thank you very much in advance.

EDIT: proof for the init:

public class MyApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        AndroidThreeTen.init(this);
    }
}

Btw API level is 24.

Tom
  • 16,842
  • 17
  • 45
  • 54
logi0517
  • 813
  • 1
  • 13
  • 32
  • can you show your gradle file? – Opiatefuchs Oct 12 '16 at 11:54
  • compile 'com.jakewharton.threetenabp:threetenabp:1.0.4' is in the dependencies – logi0517 Oct 12 '16 at 11:55
  • can you show the part where you set AndroidThreeTen.init()? – Opiatefuchs Oct 12 '16 at 12:06
  • Tangents: Generally best to exchange data in UTC rather than a time zone. Use `Instant.toString()` to generate a String in standard ISO 8601 format. And do not confuse date-time objects with the strings that they generate to represent their internal value. With an HTTP request you are sending/receiving text, not objects. You need to *parse* received text into a date-time object, and for sending you ask the date-time object to *generate* a String. The strings are distinct and separate. Strings have a format, the objects do *not*. – Basil Bourque Oct 12 '16 at 16:47
  • FYI: [How to use ThreeTenABP in Android](http://stackoverflow.com/q/38922754/642706) – Basil Bourque Oct 14 '16 at 00:37
  • Do your AndroidManifest use MyApplication as Application class? if not your MyApplication onCreate will never be used – Vincent D. Oct 21 '16 at 17:51
  • good tip. This is probably what I forgot to do 9 days ago, although I cant try it out now, I used another solution/library. – logi0517 Oct 22 '16 at 11:20
  • 2
    for anyone getting here: use this in your application tag in your AndroidManifest: android:name=".MyApplication" – logi0517 Oct 22 '16 at 11:21

0 Answers0