Gluon on iPhone telling NoSuchMethodError

2019-07-27 04:37发布

问题:

Next thing:

java.lang.NoSuchMethodError: 
java.util.Date.from(Ljava/time/Instant;)Ljava/util/Date;

its running on desktop but not deployed on mobile..

Thanks for any suggestion..

回答1:

Most of the java.util.Date class works on mobile (Android and iOS). However there a few cases that are not available.

On Android or iOS if you try

Date date = Date.from(Instant.now());

that refers to the Java 8 static method Date.from(Instant), you'll get the exception you mentioned:

W System.err: Caused by: java.lang.NoSuchMethodError: No static method from(Ljava/time/Instant;)Ljava/util/Date; in class Ljava/util/Date; or its super classes (declaration of 'java.util.Date' appears in /system/framework/core-oj.jar)

To solve this issue, you can use the usual constructor instead, which is in turn what the static method uses:

// Android, iOS
Date date = new Date(Instant.now().toEpochMilli()));

Alternatively you can use the new java.time package.