0

I have a list of dates in the form of ddMMMyyyy stored in one single string. The dates may or may not be consecutive I print those dates as individual dates. I want to remove that and use hyphen if the dates are consecutive.

For Example

13Aug2020
15Aug2020 - 18Aug2020
22Aug2020

Instead of

13Aug2020
15Aug2020
16Aug2020
17Aug2020
18Aug2020
22Aug2020

Code used to Print Dates :

mDateView.setText(mDateValue.replace(",", "\n"));

where mDateView is a Textview and mDateValue is the String containing all the dates seperated by Commas

Akash R
  • 175
  • 2
  • 11
  • 2
    Where exactly does XML come into it? – Michael Kay Jul 25 '20 at 23:00
  • Try converting the date strings into Date objects and then work with looping to find out the last date of the series. see: https://stackoverflow.com/q/15211132/5454852 – Tarik Weiss Jul 25 '20 at 23:47
  • Looks like a simple programming task, where exactly did you struggle? – Henry Jul 26 '20 at 05:35
  • By the way, the [standard way to represent](https://en.wikipedia.org/wiki/ISO_8601#Time_intervals) an interval of time textually is with a slash character separating your dates, rather than spaces and a hyphen. `2020-08-15/2020-08-18` – Basil Bourque Jul 26 '20 at 17:21

1 Answers1

2

Parse each string into a LocalDate object using a DateTimeFormatter. Search for how (if your search leads to a page using the old and troublesome SimpleDateFormat, avoid that). Create two variables for the start and end of the current interval. Store the first date into both. In a loop over the remaining dates:

  • If the current date is one day after the end, store it into the end, thus extending the interval by one day.
  • Otherwise print the current interval, see below for how. Then again store the current date into both start and end.

After the loop terminates, print the current interval.

How to print the current interval: if start and end are equal, print only one of them; otherwise print both with an en-dash between them. In either case format each printed date into the desired format, for example the original format using the same DateTimeFormatter.

To determine whether current date is one day after end, use the plusDays and the isEqual methods of LocalDate.

Question: Doesn’t LocalDate require Android API level 26?

LocalDate is part of java.time, the modern Java date and time API. 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.
  • 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 either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161