0

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.

This method is being called on a date picker onItenSelectedListener that is static

How it can be deprecated if there is no replacement that can be really used?

  • 1
    Please add your code for further investigation – Md. Asaduzzaman Jan 02 '20 at 11:29
  • You cannot call a non-static function from a static context.Kindly add the warning message from IDE too. – Sonu Sanjeev Jan 02 '20 at 11:31
  • 1
    The last one date manipulation api that includes LocalDate and LocalDateTime comes with several resources. https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant – Delnei Friedrich Jan 02 '20 at 12:25
  • Using `LocalDate` from java.time, the modern Java date and time API, is an excellent idea. On the ohter hand an answer not containing much more than a link isn’t really considered helpful on Stack Overflow. Could you please edit your answer and summarize the important point(s) from your link and/or explain how it answers the question? That would be wonderful. – Ole V.V. Jan 02 '20 at 17:19
  • java.time is an alternative for replace deprecated date api. To explain how all this works a link with tutorial makes sense. – Delnei Friedrich Jan 04 '20 at 01:36

2 Answers2

1

java.time and ThreeTenABP

Use LocalDate from java.time, the modern Java date and time 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

LocalDate numbers both years and months the same way humans do, so there’s no subtracting funny values to adjust. If you need a Date for an API not yet upgraded to java.time, the conversion goes like this:

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

Tue Oct 15 00:00:00 CEST 2019

The classes Date, Calendar and GregorianCalendar are all poorly designed and all long outdated. So do consider not using them.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. Only in this case for converting from Instant to Date use 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).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

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();
  • 1
    It doesn’t give the same result, though. Using the example values from my answer I got `Fri Nov 15 00:00:00 CET 2019` from your first snippet and `Tue Oct 15 20:11:33 CEST 2019` from the other one. I’d say the similarity is non-trivial to spot. This is how confusing the old date and time classes are. – Ole V.V. Jan 02 '20 at 18:18