Friday 7 June 2019

java - ZonedDateTime.now() causes org.threeten.bp.zone.ZoneRulesException

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.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...