无法解决 android studio 中的符号“java.time.LocalDate”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28745205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:07:00  来源:igfitidea点击:

cannot resolve symbol 'java.time.LocalDate' error in android studio

javaandroidandroid-studiojava-8java-time

提问by siim sammul

I am trying to use java.time.LocalDate in my android app, however if I try to import it, then any class from java.time gives a cannot resolve symbol error in Android studio 1.1

我正在尝试在我的 android 应用程序中使用 java.time.LocalDate,但是如果我尝试导入它,那么 java.time 中的任何类都会在 Android studio 1.1 中给出无法解析的符号错误

The only reason I could figure out for this is that it doesn't support java 8. Is that really the case?

我能弄清楚的唯一原因是它不支持 java 8。真的是这样吗?

回答by Basil Bourque

Android API level 26

安卓 API 级别 26

Android API level 26 gained an implementation of java.timeincluding your LocalDateclass.

Android API 级别 26 获得了java.time的实现,包括您的LocalDate类。

Earlier Android

早期的安卓

For Android <26, alternatives include:

对于 Android <26,替代方案包括:

  • ThreeTen-Backportis a back-port of much of the java.time functionality to Java 6 & 7.
  • Joda-Timeis commonly used in Android projects. Joda-Time inspired the java.time package in Java 8, but they are not drop-in replacements. The Joda-Time team advises migration to java.time.
  • ThreeTen-Backport 是 Java 6 和 7 的大部分 java.time 功能的后向端口。
  • Joda-Time常用于 Android 项目。Joda-Time 启发了 Java 8 中的 java.time 包,但它们不是直接替代品。Joda-Time 团队建议迁移到 java.time。


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

回答by Meno Hochschild

Yes, Android does not support Java-8. And about the new date/time-API contained in Java-8, you can look at this issue:

是的,Android 不支持 Java-8。关于 Java-8 中包含的新日期/时间 API,你可以看看这个问题:

http://code.google.com/p/android/issues/detail?id=62192

http://code.google.com/p/android/issues/detail?id=62192

Currently there are no plans to introduce JSR-310 (java.time-package) in Google Android.

目前没有计划在 Google Android 中引入 JSR-310 (java.time-package)。

回答by Mara

Finally date/time-API contained in Java 8 is added to Android API level 26 which was recently released.

最后,Java 8 中包含的日期/时间 API 被添加到最近发布的 Android API 级别 26。

DateTimeFormatter API 26

日期时间格式器 API 26