如何降级旧 Android 中的 java.time 代码? [英] How to downgrade java.time code in older Android?

查看:51
本文介绍了如何降级旧 Android 中的 java.time 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简洁的代码,它生成两个日期之间的天数列表,然后是当天的日期,以及它在列表中的位置(最重要的是,所有日期都采用相同的格式,这使得比较它们变得容易).

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;

问题是,当我在 Java IDE 中使用此代码时,我不知道它的某些部分 (.parse() ; .now() ; .isAfter() ; .plusDays()) 是为 26+ 级 API 手机保留的.或者,我的应用程序应该使用的最大 API 是 API 23.

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.

推荐答案

Back-port

无需降级".大部分 java.time 功能已经向后移植.

ThreeTen-Backport 库添加到您的 probject,专门在 ThreeTenABP 项目中适用于 Android.请参阅下面的链接,位于底部.

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.

顺便说一下,您提到的那些文本格式是标准的,在 ISO 8601 中定义.java.time 类在解析/生成字符串时默认使用这些格式.

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.

java.time 框架内置于 Java 8 及更高版本中.这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date, Calendar, &SimpleDateFormat.

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.

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

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

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

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

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.

从哪里获取 java.time 类?

Where to obtain the java.time classes?

  • Java SE 8Java SE 9Java SE 10Java SE 11 及更高版本 - 具有捆绑实现的标准 Java API 的一部分.
    • Java 9 添加了一些小功能和修复.
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    这篇关于如何降级旧 Android 中的 java.time 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆