Time for a modern answer.
java.time and ThreeTenABP
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d/M/u"); String validUntil = "1/1/1990"; LocalDate validDate = LocalDate.parse(validUntil, dateFormatter); LocalDate currentDate = LocalDate.now(ZoneId.of("Pacific/Efate")); if (currentDate.isAfter(validDate)) { System.out.println("Catalog is outdated"); }
When I just ran this code, the output was:
The catalog is out of date
Since this is never the same date in all time zones, LocalDate.now explicit time zone for LocalDate.now . If you want the catalog to expire simultaneously in all time zones, you can ZoneOffset.UTC as long as you inform users that you are using UTC.
I am using java.time, a modern Java date and time API. The Calendar , SimpleDateFormat and Date date and time classes you use are poorly designed and, fortunately, are deprecated. Also, despite the name, Date does not represent a date, but a point in time. One consequence of this is that, although today is February 15, 2019, the newly created Date object is already after (therefore not equal to) the Date object from parsing 15/02/2019 . This confuses some. In contrast, the current LocalDate is a date without a time of day (and without a time zone), so the two LocalDate representing today's date will always be equal.
Question: Can I use java.time on Android?
Yes, java.time works great on old and new Android devices. It just requires at least Java 6 .
- Java 8 and later, as well as newer Android devices (starting at API level 26) have a modern API built in.
- In Java 6 and 7, get ThreeTen Backport, the backport of modern classes (ThreeTen for JSR 310; see the links below).
- On (older) Android, use the Android version of ThreeTen Backport. It is called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp with org.threeten.bp .
communication
Ole VV Feb 15 '19 at 14:49 2019-02-15 14:49
source share