-1

i have a date in String "2020-06-02"

now i have to convert it in LocalDate but without using DateTimeFormatter.

is it possible.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate inputAdDate = LocalDate.parse(adDate, formatter);

as of now my code looks like this. but i dont want to use DateTimeFormatter as it is not supported in API level below 26.

Umesh Chakradhar
  • 151
  • 3
  • 13
  • 1
    Try `LocalDate.parse(adDate)`. – Sweeper Jun 02 '20 at 09:12
  • @Sweeper java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate; – Umesh Chakradhar Jun 02 '20 at 09:16
  • See [here](https://developer.android.com/studio/write/java8-support-table), both `LocalDate` and `DateTimeFormatter` are supported. You probably haven't set things up correctly. Did you read [this](https://developer.android.com/studio/write/java8-support#library-desugaring)? – Sweeper Jun 02 '20 at 09:22
  • @Sweeper https://developer.android.com/reference/java/time/format/DateTimeFormatter this link says its added in API level 26 – Umesh Chakradhar Jun 02 '20 at 09:25
  • And so does the [page for `LocalDate`](https://developer.android.com/reference/java/time/LocalDate)... – Sweeper Jun 02 '20 at 09:28
  • Why can't you use `DateTimeFormatter`? Both classes are part of `java.time`. – akuzminykh Jun 02 '20 at 09:29
  • 2
    The parse method with single parameter expects the input string should be in ISO-8601 format. And I see your input date string in the same format. So, you don’t need to pass the formatter paramter. – Alok Singh Jun 02 '20 at 10:01
  • 1
    Well, are you sure that you *can* use `LocalDate`, but cannot use `DateTimeFormatter`? That seems weird to me, because both classes are added to the standard Java libraries at the same time. – MC Emperor Jun 02 '20 at 13:19
  • 1
    Both `LocalDate` and `DateTimeFormatter` were introduced as part of java.time (the modern Java date and time API) in API level 26. Up to level 25 you may use both through the backport, the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (that’s ABP for Android backport). – Ole V.V. Jun 02 '20 at 18:47
  • Does this answer your question (if you read it well)? [cannot resolve symbol 'java.time.LocalDate' error in android studio](https://stackoverflow.com/questions/28745205/cannot-resolve-symbol-java-time-localdate-error-in-android-studio) – Ole V.V. Jun 02 '20 at 18:48

2 Answers2

3

Try this.

String date = "2020-06-02";
LocalDate localDate = LocalDate.parse(date);

you need to import this package.

import java.time.LocalDate;
Natsu
  • 443
  • 3
  • 10
1

Also, you can try the java.text.SimpleDateFormat

String strDate = "20-03-2019";
SimpleDateFormat sdfDateDMY = new SimpleDateFormat("dd-MM-yyyy");
sdfDateDMY.parse(strDate);

  • 1
    I recommend against it (even though it will work under API level 26). The OP is seeking to use `LocalDate` from java.time the modern Java date and time API. (1) `SimpleDateFormat` will not work with `LocalDate`. (2) `SimpleDateFormat` is notoriously troublesome and long outdated. – Ole V.V. Jun 02 '20 at 19:05
  • I didn't know that `LocalDate` doesn't work with `SimpleDateFormat`. Good to know that. Thanks – davosandoval Jun 03 '20 at 08:12