Skip to content

Instantly share code, notes, and snippets.

@marlonlom
Last active April 15, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlonlom/cf477a85ea20adde8460821663334a3b to your computer and use it in GitHub Desktop.
Save marlonlom/cf477a85ea20adde8460821663334a3b to your computer and use it in GitHub Desktop.
Formats a dates (start,end) range in Kotlin, presenting the result in a collapsed date range format.
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale.ENGLISH
/**
* Creates a "collapsed" date range String representing the period of time
* between two Dates.
*
* @author marlonlom
*/
object CollapseDateRanges {
private val YEAR_FORMAT = SimpleDateFormat("yyyy", ENGLISH)
private val MONTH_FORMAT = SimpleDateFormat("MMM", ENGLISH)
private val DAY_FORMAT = SimpleDateFormat("dd", ENGLISH)
private val DAY_MONTH_FORMAT = SimpleDateFormat("dd MMM", ENGLISH)
private val DAY_MONTH_YEAR_FORMAT = SimpleDateFormat("dd MMM/yy", ENGLISH)
/**
* Returns a "collapsed" date range String representing the period of time
* between two Date parameters.
* <br/>
* Example: August 19 as a {@code startDate} and August 30 as an {@code endDate}
* will return "19 - 30 AUG", August 28 as a {@code startDate}
* and September 7 as an {@code endDate} will return "28 AUG - 07 SEP".
* <br/><br/>
* This means if you pass this two dates which cannot
* collapse into a shorter form, then the longer form is returned.
* <br/><br/>
* Years are not ignored, and the start and end dates are not checked to ensure we
* are not going backwards in time (Aug 10 - July 04 is not checked).
*
* @param startDate
* the start Date in the range.
* @param endDate
* the end Date in the range.
* @return a String representation of the range between the two dates given.
*/
@JvmStatic
fun collapseDate(startDate: Date, endDate: Date): String {
val formattedDateRange: String
// Get a comparison result to determine if the Years are the same
val startDateYear = YEAR_FORMAT.format(startDate)
val endDateYear = YEAR_FORMAT.format(endDate)
// Get a comparison result to determine if the Months are the same
val startDateMonth = MONTH_FORMAT.format(startDate)
val endDateMonth = MONTH_FORMAT.format(endDate)
formattedDateRange = if (startDateMonth == endDateMonth && startDateYear == endDateYear) {
"${DAY_FORMAT.format(startDate)} - ${DAY_MONTH_FORMAT.format(endDate).toUpperCase(ENGLISH)}"
} else if (startDateMonth == endDateMonth && startDateYear == endDateYear) {
// Months don't match, but, years match, split the string across both months
"${DAY_MONTH_FORMAT.format(startDate).toUpperCase(ENGLISH)} - ${DAY_MONTH_FORMAT.format(endDate).toUpperCase(ENGLISH)}"
}else {
// Months don't match, also, years don't match, split the string across both months and years
"${DAY_MONTH_YEAR_FORMAT.format(startDate).toUpperCase(ENGLISH)} - ${DAY_MONTH_YEAR_FORMAT.format(endDate).toUpperCase(ENGLISH)}"
}
return formattedDateRange
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment