1

i have written this code to check given date is within 10 days or outside 10 days but AlarmManager.INTERVAL_DAY always give 0 . how to check given date lies within 10 days in android or java?

long lastdate=1535826537767

if ((System.currentTimeMillis() - lastdate) < (AlarmManager.INTERVAL_DAY * 10)) {
    Log.d("Status", "Date within 10 days")       
} else {
    Log.d("Status", "Date within 10 days")
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Anil Thakur
  • 123
  • 1
  • 11

2 Answers2

3

Simply:

long ten_days_in_in_millis = 864000000;
if ((System.currentTimeMillis() - lastdate) < ten_days_in_in_millis) {..
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • This code ignores the issues of time zones affecting the calendar. **Not all days are 24-hours** long. See [the Answer by Desert](https://stackoverflow.com/a/52172358/642706) for an example of properly considering time zone effects on date. – Basil Bourque Sep 04 '18 at 23:20
  • no dear its not working when when i check with today date its not going inside within 10 days – Anil Thakur Sep 05 '18 at 02:42
  • can you check with one day before from current date please – Anil Thakur Sep 05 '18 at 03:06
  • @BasilBourque why can't you focus on the main point of the question? How do you know that the OP hasn't already converted (if at all needed) the date in local time zone? The question is not about time zone issues, and as it stands you can't assume that hasn't been taken care of it beforehand. – NiVeR Sep 05 '18 at 05:59
  • @AnilThakur This code is very simple, the only variable is the date in millis you are checking, if you have correct value for it then this code works. – NiVeR Sep 05 '18 at 06:02
2

java.time

You can make use of java.time classes built into Java 8 and later to check if a certain date lies within last 10 days.

First: convert lastdate (that you already have) to LocalDate.

Instant instant = Instant.ofEpochMilli(lastdate) ; // A moment in UTC.
LocalDate originalDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); 
LocalDate tenDaysOldDate= LocalDate.now().minusDays(10); //10 days old date

if (Period.between(tenDaysOldDate, originalDate ).getDays() > 10)
      //greater than 10 days

LocalDate is immutable and thread-safe. This class should be preferred over the problematic Date class.

Period is also immutable and thread-safe. It measures time in years, months and days.


Most of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in the ThreeTenABP project. See How to use ThreeTenABP….

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Faraz
  • 6,025
  • 5
  • 31
  • 88
  • I prefer this solution due to its legibility and the use of current APIs. But the use of Java 8 might be something that is too limitting for some – Marged Sep 04 '18 at 18:54
  • Where did you see that the OP is using java 8? – NiVeR Sep 04 '18 at 18:59
  • @FarazDurrani What I am saying is that your answer may not be applicable to the question if they are using java < 8. – NiVeR Sep 04 '18 at 19:02
  • @NiVeR Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Sep 04 '18 at 23:18