Created
November 16, 2019 13:53
-
-
Save AndSky90/0a5267ce96e2008c6434ed00d002f35a to your computer and use it in GitHub Desktop.
Форматтеры дат и имен
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object StringFormatter { | |
private const val ISO_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss" | |
fun formatPastTimeDate(dateString: String): String { | |
val isoString = dateString.substringBefore(".") | |
val outputTimePattern = SimpleDateFormat("HH:mm", Locale.getDefault()) | |
val outputDateTimePattern = SimpleDateFormat("dd MMM' в 'HH:mm", Locale.getDefault()) | |
val inputPattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
val date: Date | |
try { | |
date = inputPattern.parse(isoString) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
return isoString | |
} | |
val eventTimeDate = GregorianCalendar().apply { | |
time = date | |
timeZone = TimeZone.getDefault() | |
} | |
val currentTimeDate = GregorianCalendar() | |
val outputDate = eventTimeDate.timeInMillis + eventTimeDate.timeZone.rawOffset | |
return if (currentTimeDate.get(Calendar.MONTH) == eventTimeDate.get(Calendar.MONTH)) { | |
if (currentTimeDate.get(Calendar.DAY_OF_MONTH) == eventTimeDate.get(Calendar.DAY_OF_MONTH)) { | |
"Сегодня в " + outputTimePattern.format(outputDate) //если сегодня был коммент | |
} else { | |
if (currentTimeDate.get(Calendar.DAY_OF_MONTH) - 1 == eventTimeDate.get(Calendar.DAY_OF_MONTH)) { | |
"Вчера в " + outputTimePattern.format(outputDate) | |
} else outputDateTimePattern.format(outputDate) | |
} | |
} else { | |
val yesterdayDate = GregorianCalendar().apply { add(Calendar.DAY_OF_YEAR, -1) } | |
if (eventTimeDate.get(Calendar.DAY_OF_MONTH) == yesterdayDate.get(Calendar.DAY_OF_MONTH)) { | |
"Вчера в " + outputTimePattern.format(outputDate) | |
} else | |
outputDateTimePattern.format(outputDate) | |
} | |
} | |
fun formatOnlyDate(dateString: String, fourDigitYear: Boolean = false): String { | |
val isoString = dateString.substringBefore(".") | |
val inputPattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
val outputPattern = if (fourDigitYear) | |
SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()) | |
else | |
SimpleDateFormat("dd.MM.yy", Locale.getDefault()) | |
val date: Date | |
try { | |
date = inputPattern.parse(isoString) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
return isoString | |
} | |
val eventTimeDate = GregorianCalendar().apply { | |
time = date | |
timeZone = TimeZone.getDefault() | |
} | |
val outputDate = eventTimeDate.timeInMillis + eventTimeDate.timeZone.rawOffset | |
return outputPattern.format(outputDate) | |
} | |
fun serialiseDateTime(year: Int, month: Int, dayOfMonth: Int): String { | |
val date = GregorianCalendar().apply { | |
timeZone = TimeZone.getDefault() | |
set(year, month, dayOfMonth) | |
} | |
return serialiseDateTime(date) | |
} | |
fun serialiseDateTime(calendarDate: GregorianCalendar) : String { | |
val pattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
var ret = "" | |
try { | |
ret = pattern.format(calendarDate.time) | |
}catch (ex: Exception) { | |
log(ex) | |
} | |
return ret | |
} | |
fun formatForSendToApi(dateIsoString: String) : String { | |
if (dateIsoString.isBlank()) return "" | |
val inputPattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
val outputPattern = SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.getDefault()) | |
return try { | |
val date = inputPattern.parse(dateIsoString) | |
outputPattern.format(date) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
"" | |
} | |
} | |
fun getDateFromIsoString(dateIsoString: String): GregorianCalendar { | |
val inputPattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
val date: Date | |
try { | |
date = inputPattern.parse(dateIsoString) | |
} catch (e: ParseException) { | |
e.printStackTrace() | |
return GregorianCalendar() | |
} | |
return GregorianCalendar().apply { | |
time = date | |
timeZone = TimeZone.getDefault() | |
} | |
} | |
fun isOutdated(dateString: String?): Boolean { | |
if (dateString.isNullOrBlank()) return false | |
val inputPattern = SimpleDateFormat(ISO_DATE_PATTERN, Locale.getDefault()) | |
val inputString = dateString.substringBefore(".") | |
val date: Date | |
try { | |
date = inputPattern.parse(inputString) | |
} catch (e: ParseException) { | |
log("date parsing error: $dateString") | |
return false | |
} | |
val currentTimeDate = GregorianCalendar() | |
val offset = currentTimeDate.timeZone.rawOffset | |
val eventTimeDate = GregorianCalendar().apply { | |
time = date | |
timeZone = TimeZone.getDefault() | |
timeInMillis += offset | |
} | |
return (currentTimeDate > eventTimeDate) | |
} | |
fun formatName(user: User?, defaultValue: String = "") = | |
formatName( | |
surname = user?.surname, | |
name = user?.name, | |
patronymic = user?.patronymic, | |
fullName = user?.fullName, | |
defaultValue = defaultValue | |
) | |
fun formatName( | |
surname: String?, | |
name: String?, | |
patronymic: String?, | |
fullName: String?, | |
defaultValue: String = "" //опционально можно указать значение, если ни одно поле не заполнено | |
): String { | |
var output: String | |
if (surname != null && surname.isNotBlank()) { | |
output = surname | |
if (name != null && name.isNotBlank()) { | |
output = "$output ${name.first()}." | |
if (patronymic != null && patronymic.isNotBlank()) { | |
output = "$output ${patronymic.first()}." | |
} | |
} | |
} else { | |
output = fullName ?: defaultValue | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment