2

I want to do date and time safely, so I followed several pieces of SO advice saying to use DateTimeFormatter, so I pasted in 3 lines:

LocalDate date = LocalDate.now();
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);

Not surprisingly I got Cannot resolve symbol... for just about everything. Been there, done that. Put cursor on error routine and hit Alt+Enter and the proper import will be generated.

But that didn't happen.

So I inserted these two lines as in another sample code snippet:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

Error: `Cannot resolve symbol LocalTime` and `DateTimeFormatter`

My question is: What should I do to resolve the symbols causing the error.

I'll go on. I'm submitting more because usually when I intend to submit a Question, I figure it out my own Answer during the process of explaining what I intended to submit.

Not this time.

(Why do I have

C:\Users\Dov\AppData\Local\Android\sdk and

C:\Users\Dov\AppData\Local\Android\sdk1?

It doesn't matter which I select for SDK manager. Both are up-to-date, it says.

{I know this is gonna be embarrassing. I've been doing this too long (maybe I should stop here) for this to be holding me back. }


I have checked Use embedded jdk.

I looked at every Setting I can think of.

I've Googled the error message.

I've done Clean (no errors) and Build.

import static... changes nothing.

I'm using Java 8, version 51, Android Studio 2.3.3, gradle-3.3

Gradle console says:

C:\Users\Dov\Google Drive\AndroidStudioProjects\WordyHelperton - Copy - Copy
  \app\src\main\java\com\dslomer64\servyhelperton\Utilities.

java:9: error: package java.time.format does not exist

And:

C:\Users\Dov\Google Drive\AndroidStudioProjects\WordyHelperton - Copy - Copy
  \app\src\main\java\com\dslomer64\servyhelperton\Utilities.

java:321: error: cannot find symbol

I have this:

enter image description here

And this:

package java.time.format;

public final class DateTimeFormatter {
  private final java.time.format.DateTimeFormatterBuilder.CompositePrinterParser printerParser;
  private final java.util.Locale locale;
  private final java.time.format.DecimalStyle decimalStyle;
  private final java.time.format.ResolverStyle resolverStyle;
  private final java.util.Set<java.time.temporal.TemporalField> resolverFields;
  private final java.time.chrono.Chronology chrono;
  private final java.time.ZoneId zone;
  public static final java.time.format.DateTimeFormatter ISO_LOCAL_DATE;
  ...
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • what is your minsdkversion? – fluffyBatman Sep 07 '17 at 18:14
  • 1
    Much of the java.time functionality is back-ported to Java 6 & 7 in the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [*ThreeTenABP*](https://stackoverflow.com/q/36000997/642706) project; see [*How to use…*](https://stackoverflow.com/q/38922754/642706). – Basil Bourque Sep 07 '17 at 21:59

2 Answers2

1

While working with Android, you are using Android SDK, not JDK directly. And support for both Android SDK's LocalTime and DateTimeFormatter added in API Level 26.

Even if you try to access these directly through JDK8, as of September 8th 2017, Android Studio has yet to support all Java 8 features. Even with the forthcoming 3.0 Preview 1, you'll only get support for following Java 8 APIs and that with minsdkversion dependency restriction as shown below.

enter image description here

Source: Supported Java 8 Language Features and APIs

So, as there is no mention of java.time.* API yet, trying to import it through JDK8 won't work as of Android Studio 3.0 Preview 1.

fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • So were all the suggestions to use `DateTimeFormatter` from people using Eclipse? – DSlomer64 Sep 07 '17 at 21:30
  • Much of the java.time functionality is back-ported to Java 6 & 7 in the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [*ThreeTenABP*](https://stackoverflow.com/q/36000997/642706) project; see [*How to use…*](https://stackoverflow.com/q/38922754/642706). – Basil Bourque Sep 07 '17 at 21:58
  • Here's a good link to follow: https://stackoverflow.com/a/38922755/2737933 – DSlomer64 Sep 08 '17 at 00:12
0

LocalTime and LocalDate are new to java 1.8. It looks like you are using 1.7 or earlier. You need to update your JDK to java 1.8

jwils
  • 495
  • 3
  • 11