I'm trying to use DateTimeFormatter in my project, but Android Studio can't find java.time.format.DateTimeFormatter
package.
I downloaded Java SE Development Kit 8u92 and in settings of my project I also set "use jdk8".
What am I missing?
Asked
Active
Viewed 5,173 times
2

Stephan Leila
- 141
- 1
- 8
-
Why you don't use SimpleDateFormat. It's what I use in Android and it workd fine. – adalpari Jul 11 '16 at 10:12
-
SimpleDateFormat is what I am using in Android and it is not working fine. In particular it is unable to parse the time zone AEST (Australian Eastern Standard Time) which DateTimeFormatter apparently does: https://stackoverflow.com/questions/36473928/parse-date-with-aedt-and-aest-time-zone-in-java – Slartibartfast Jul 06 '18 at 07:57
-
I believe that when the question was asked, the answer was to use ThreeTenABP, the Android adaptation of the backport of java.time. Today core API desugaring seems a more attractive option. See the linked original question (which is later than this one). – Ole V.V. Jan 14 '23 at 13:06
-
@adalpari and Slartibartfas:t Please not. That would be a sizeable step backward. `SimpleDateFormat` was a notorious troublemaker of a class. The OP is wise in asking how to use its clearly better replacement, `DateTimeFormatter` from java.time. – Ole V.V. Jan 14 '23 at 13:09
3 Answers
3
In Android, actually you are ussing Android SDK, not JDK directly. You could use SimpleDateFormat:
import java.text.SimpleDateFormat;
Date myDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ZZZZ");
String date = dateFormat.format(myDate);

adalpari
- 3,052
- 20
- 39
-
1Oh, thanks for clarification. I like that in java 8 I can use `DateTimeFormatter.ISO_INSTANT` instead of hardcoding pattern myself – Stephan Leila Jul 11 '16 at 10:30
1
From official documentation about this class:
Since: 1.8
Android SDK
is working over jvm 1.6
Related answer: Is it possible to use Java 8 for Android development?

Community
- 1
- 1

Rudziankoŭ
- 10,681
- 20
- 92
- 192
0
Date time formatter Java Date time formatter to convert simple date it's very easy in utils class just like that please check and used this program.
public static String getApiSurveyResponseDateConvertToLocal(String date) {
try {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date datee = inputFormat.parse(date);
return outputFormat.format(datee);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}

Rahul Rokade
- 98
- 8
-
I strongly disagree. The `SimpleDateFormat`class that you suggest is a notorious troublemaker of a class and fortunately long outdated. No one should use it. The question also wisely asked about its modern replacement, the `DateTimeFormatter` class of java.time, the modern Java date and time API. – Ole V.V. Jan 13 '23 at 15:13