简体   繁体   English

在静态上下文中将弃用的 Java 日期替换为 Calendar.set 或 GregorianCalendar.set

[英]Replace deprecated Java Date to Calendar.set ou GregorianCalendar.set in a static context

I'm working on Android project using Java and Android Studio IDE is warning me Date(int, int, int) is deprecated as of API16, but when I replace it to Calendar.set the build fails as non-static method cannot be referenced in a static context.我正在使用 Java 开发 Android 项目,Android Studio IDE 警告我 Date(int, int, int) 自 API16 起已弃用,但是当我将其替换为 Calendar.set 时,构建失败,因为无法引用非静态方法在静态上下文中。

This method is being called on a date picker onItenSelectedListener that is static正在静态的日期选择器 onItenSelectedListener 上调用此方法

How it can be deprecated if there is no replacement that can be really used?如果没有可以真正使用的替代品,它如何被弃用?

java.time and ThreeTenABP java.time 和 ThreeTenABP

Use LocalDate from java.time, the modern Java date and time API.使用 java.time 中的LocalDate ,现代 Java 日期和时间 API。 It's much nicer to work with.和它一起工作要好得多。

    int year = 2019;
    int month = 10;
    int day = 15;

    LocalDate date = LocalDate.of(year, month, day);
    System.out.println(date);

The output from this snippet is:此代码段的输出是:

2019-10-15 2019-10-15

LocalDate numbers both years and months the same way humans do, so there's no subtracting funny values to adjust. LocalDate以与人类相同的方式计算年份和月份,因此无需减去有趣的值进行调整。 If you need a Date for an API not yet upgraded to java.time, the conversion goes like this:如果您需要一个尚未升级到 java.time 的 API 的Date ,则转换过程如下:

    Instant startOfDay = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
    Date oldfashionedDate = DateTimeUtils.toDate(startOfDay);
    System.out.println(oldfashionedDate);

Tue Oct 15 00:00:00 CEST 2019 2019 年 10 月 15 日星期二 00:00:00 CEST

The classes Date , Calendar and GregorianCalendar are all poorly designed and all long outdated.DateCalendarGregorianCalendar都设计得很差,而且都已经过时了。 So do consider not using them.所以请考虑不使用它们。

Question: Doesn't java.time require Android API level 26?问题:java.time 不需要 Android API 级别 26 吗?

java.time works nicely on both older and newer Android devices. java.time 在较旧和较新的 Android 设备上都能很好地工作。 It just requires at least Java 6 .它只需要至少Java 6

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.在 Java 8 及更高版本和更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。 Only in this case for converting from Instant to Date use Date.from(startOfDay) .仅在这种情况下,从Instant转换为Date使用Date.from(startOfDay)
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
  • On (older) Android use the Android edition of ThreeTen Backport.在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。 It's called ThreeTenABP.它被称为 ThreeTenABP。 And make sure you import the date and time classes from org.threeten.bp with subpackages.并确保使用子包从org.threeten.bp导入日期和时间类。

Links链接

I solved the problem replacing我解决了更换问题

return new Date(year-1900, month, day)

by经过

GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
calendar.set(year, month-1, day);
return calendar.getTime();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM