1

I have two dates: initial and final. I need to be able to quickly get all the interim days, given the month. The problem is that every digit in the date is due to a variable. The reason of it is that I use Date Picker with range, but all values are given separately (like startDay, startMonth, startYear and endDay, endMonth, endYear). So, I need to get all dates between those dates. It must look like this:

"24/02/2018|25/02/2018|26/02/2018|27/02/2018|28/02/2018|01/03/2018"

Look at this: 01/03/2018.

SOLUTION:

fun getDaysBetweenDates(startdate: Date, enddate: Date): List<String> {
    val dates = ArrayList<String>()
    val calendar = GregorianCalendar()
    calendar.time = startdate

    while (calendar.time.before(enddate)) {
        val result = calendar.time
        val formatter = SimpleDateFormat("dd/MM/yyyy")
        val today = formatter.format(result)
        today.split("|")
        dates.add(today)
        calendar.add(Calendar.DATE, 1)
    }
    return dates
}

Well, thanks for helping to OleV.V. and asm0dey.

Arsen Saruhanyan
  • 173
  • 1
  • 1
  • 11
  • Please search. This has been asked and answered before. – Ole V.V. Feb 24 '18 at 13:22
  • It’s also been asked and answered more than once. [Here, for example: how to get a list of dates between two dates in java](https://stackoverflow.com/questions/2689379/how-to-get-a-list-of-dates-between-two-dates-in-java). By all means go search for more, it’s unlikely I’ve found the very best answers yet. – Ole V.V. Feb 24 '18 at 14:35
  • @OleV.V. ouch. Sorry, I was not online. So, I searched, but for some reason I did not find it. (Apparently Google did not produce normal results, but here I always forget that I can search directly). Well, now I'll look at what you offer. – Arsen Saruhanyan Feb 24 '18 at 14:56
  • @OleV.V. well, if it's gonna be helpful, check out my edit. Again, if it's gonna be helpful.... – Arsen Saruhanyan Feb 24 '18 at 15:13
  • Thx for the edit. “…ThreeTenABP, but, how I understood, it's very inefficient to use.” Beg to differ. `LocalDate` from ThreeTenABP, I expect it to be efficient both in terms of efficient code writing, efficient maintenance and efficient runtime performance. I got no idea from where you got that other statement. – Ole V.V. Feb 24 '18 at 15:20
  • To create a date object from three integers: [see here](https://stackoverflow.com/a/33892250/5772882). Then use the other answers. To format the dates into the format you want in your output string: [How to format LocalDate to string?](https://stackoverflow.com/questions/28177370/how-to-format-localdate-to-string) If it’s hard to find, it may be because you are asking too many questions in one. For best results try to separate into single tasks, both when you search and when you post a question. – Ole V.V. Feb 24 '18 at 15:27
  • @OleV.V. Well, it's written in the official github repository. Anyway, I'll try it. I'm just a little bit tired and want to get answer to all my questions, oh well. – Arsen Saruhanyan Feb 24 '18 at 15:32
  • Do you mean this sentence? “the threetenbp uses a JAR resource for loading timezone information. This is an extremely inefficient mechanism on Android.” It’s talking exactly about the inecciciency that ThreeTenABP *overcomes* (compared to pure ThreeTen Backport). – Ole V.V. Feb 24 '18 at 15:50
  • And, then it is understandable. Anyway, in one of the links, what you threw off (https://stackoverflow.com/questions/2689379/how-to-get-a-list-of-dates-between-two-dates-in-java) was a pretty good answer, which almost completely came up to me. The fact is that I need a short date, that is, without explaining the day, month and time. So, maybe you know how to do this? Anyway, thank you very much for helping to find the answer that is completely appropriate. – Arsen Saruhanyan Feb 24 '18 at 16:02
  • What do you mean by “without explaining the day, month and time”? – Ole V.V. Feb 24 '18 at 16:36
  • Another tip, depending on the comparison you’re to do, it may be easier to process a list of dates than a long string. So you may split the string from JSON at the pipe or vertical bar symbol `|` and parse each element into a `LocalDate` to obtain such a list. – Ole V.V. Feb 24 '18 at 16:49
  • 1. Why do you think `Date` class is deprecated? https://docs.oracle.com/javase/8/docs/api/java/util/Date.html it's not deprecated at least in Java 8. 2. In my example you can use any dates in any months. 3. Please, expose your exact problem — parse dates? find interim dates? You can build date string from digits and parse it if you need. You can construct Calendar from digits — it's perfectly affordable too… – asm0dey Feb 24 '18 at 20:09
  • I think the OP meant that the `Date` constructor taking three `int` arguments is deprecated, @asm0dey. I believe it has been deprecated since Java 1.1, that is, for more than 20 years. – Ole V.V. Feb 25 '18 at 07:28
  • @OleV.V. well... It's deprecated in favour of calendar API, so that's easy to create calendar and get date from it – asm0dey Feb 25 '18 at 07:30
  • Ok. I've fixed all I need. See my new edit. There'll be solution for my problem. – Arsen Saruhanyan Feb 25 '18 at 12:56
  • @ArsenSaruhanyan you can mark answer as correct — your solution is pretty close my :) – asm0dey Feb 25 '18 at 19:12
  • @asm0dey done :) – Arsen Saruhanyan Feb 26 '18 at 08:23

1 Answers1

2

It looks like you can achieve what you want with simple while loop — just add days one by one to initial date intil you'll reach end of the date range.

So it'll look like

val interimDates = arrayListOf<Date>()
var initial: Date = Date()// obtained earlier
val c = Calendar.getInstance()
c.time = initial
while (initial < finalDate /*also obtained earlier*/) {
    c.add(Calendar.DATE, 1)
    interimDates.add(c.time)
    initial = c.time
}

At the end interimDates will contain all interim dates

asm0dey
  • 2,841
  • 2
  • 20
  • 33
  • It should work. But the `Date` and `Calendar` classes are long outdated, and despite their names they only inaccurately match the requirements since they entail more than a date in the calendar. Better to add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project and use `org.threeten.bp.LocalDate`. – Ole V.V. Feb 24 '18 at 13:39
  • @OleV.V. if it would be Java - I would propose to use LocalDate, but task is that simple that you just don't need one more library in your project – asm0dey Feb 24 '18 at 15:41