4

I've got this neat code which generates a list of days between two dates, and then the date of the current day, as well as its position in the list (Most importantly, all the dates are in the same format, which makes it easy to compare them).

//Create list of days
String s = "2018-08-28";
String e = "2018-09-05";
LocalDate start = LocalDate.parse(s);
LocalDate end = LocalDate.parse(e);
List<LocalDate> totalDates = new ArrayList<>();
while (!start.isAfter(end)) {
    totalDates.add(start);
    start = start.plusDays(1);
}

//Date and place of current day
LocalDate a = LocalDate.now();
int current_day = totalDates.indexOf(a) + 1;

The problem is that while I was playing with this code in a Java IDE, I did not know that some of its parts (.parse() ; .now() ; .isAfter() ; .plusDays()) were reserved for 26+ level API phones. Or, the maximum API in which my application should work is API 23.

I want to know how I can "downgrade" it in the most efficient way, and I don't know what to do or where to start.

2
  • Look at the Calendar API. docs.oracle.com/javase/7/docs/api/java/util/Calendar.html Commented Aug 30, 2018 at 20:26
  • 3
    @ChristopherSchneider No, the terrible Calendar class was supplanted years ago by the modern java.time classes. Back-port available in ThreeTen-Backport and ThreeTenABP projects. Commented Aug 30, 2018 at 20:41

2 Answers 2

9

Back-port

No need to "downgrade". Most of the java.time functionality has been back-ported.

Add the ThreeTen-Backport library to your probject, adapted for Android specifically in the ThreeTenABP project. See links below, at bottom.

The legacy date-time classes are an awful ugly mess — never use them.

By the way, those textual formats to which you referred are standard, defined in ISO 8601. The java.time classes use these formats by default when parsing/generating strings.


About java.time

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

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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

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

Where to obtain the java.time classes?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, I didn't know about it! However, I'm not sure I totally understood the documents. Are the only thing I have to do to make the java.time available for API 26-: First - put implementation 'com.jakewharton.threetenabp:threetenabp:1.1.0' in the dependencies And second - put AndroidThreeTen.init(this); just below my OnCreate?
@Artemyx Sorry, I don’t know. I am not an Android developer. Search Stack Overflow. If not covered, post another Question specific to that issue.
Oh ok. Thanks anyway!
9

ThreeTeenABP is not needed anymore to enable support for these language APIs on older versions of the Android platform. You can use core library desugaring.

Just update the Android plugin to 4.0.0 (or higher) and include the following in your module’s build.gradle file:

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.