From 202e4f8ad1c4312491c6a5bd954c51f5cddd9c89 Mon Sep 17 00:00:00 2001 From: Kirill Nayduik Date: Fri, 15 Apr 2022 17:53:43 +0300 Subject: [PATCH 1/2] Create DateFormatUtils class for handling cases with DateTime format --- utils/build.gradle | 14 ++++++++ .../roboswag/core/utils/DateFormatUtils.kt | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 utils/src/main/java/ru/touchin/roboswag/core/utils/DateFormatUtils.kt diff --git a/utils/build.gradle b/utils/build.gradle index fe6fd6a..954f81e 100644 --- a/utils/build.gradle +++ b/utils/build.gradle @@ -5,6 +5,8 @@ dependencies { implementation "androidx.core:core" implementation "androidx.annotation:annotation" implementation "com.google.android.material:material" + implementation "net.danlew:android.joda" + implementation "junit:junit" constraints { implementation("androidx.core:core") { @@ -24,5 +26,17 @@ dependencies { require '1.2.0-rc01' } } + + implementation("net.danlew:android.joda") { + version { + require '2.10.2' + } + } + + implementation("junit:junit") { + version { + require '4.13.2' + } + } } } diff --git a/utils/src/main/java/ru/touchin/roboswag/core/utils/DateFormatUtils.kt b/utils/src/main/java/ru/touchin/roboswag/core/utils/DateFormatUtils.kt new file mode 100644 index 0000000..9444416 --- /dev/null +++ b/utils/src/main/java/ru/touchin/roboswag/core/utils/DateFormatUtils.kt @@ -0,0 +1,32 @@ +package ru.touchin.roboswag.core.utils + +import org.joda.time.DateTime +import org.joda.time.format.DateTimeFormat + +/** + * Util object for handling some cases with DateTime e.g. parsing string to DateTime object + */ +object DateFormatUtils { + + enum class Format(val formatValue: String) { + DATE_TIME_FORMAT("yyyy-MM-dd'T'HH:mm:ss.SSSZZ"), + DATE_FORMAT("yyyy-MM-dd"), + TIME_FORMAT("HH:mm:ssZ") + } + + /** + * @return the result of parsed string value + * @param value is string value of date time in right format + * @param format is date time format for parsing string value. + * Default value is [Format.DATE_TIME_FORMAT] + * @param defaultValue is value returned in case of exception + */ + fun fromString( + value: String, + format: Format = Format.DATE_TIME_FORMAT, + defaultValue: DateTime? = null + ): DateTime? = runCatching { value.parse(format.formatValue) }.getOrDefault(defaultValue) + + private fun String.parse(format: String) = DateTimeFormat.forPattern(format).parseDateTime(this) + +} From a3241002c51800d60adb464119ef7de38f04335c Mon Sep 17 00:00:00 2001 From: Kirill Nayduik Date: Fri, 15 Apr 2022 18:22:23 +0300 Subject: [PATCH 2/2] Create DateFormatUtilsTest for testing DateFormatUtils --- utils/src/test/java/DateFormatUtilsTest.kt | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 utils/src/test/java/DateFormatUtilsTest.kt diff --git a/utils/src/test/java/DateFormatUtilsTest.kt b/utils/src/test/java/DateFormatUtilsTest.kt new file mode 100644 index 0000000..62b4290 --- /dev/null +++ b/utils/src/test/java/DateFormatUtilsTest.kt @@ -0,0 +1,34 @@ +import org.joda.time.DateTime +import org.joda.time.DateTimeZone +import org.joda.time.tz.UTCProvider +import org.junit.Assert +import org.junit.Test +import ru.touchin.roboswag.core.utils.DateFormatUtils + +class DateFormatUtilsTest { + + init { + DateTimeZone.setProvider(UTCProvider()) + } + + @Test + fun `Assert Date format parsing`() { + val dateTime = DateFormatUtils.fromString( + value = "2015-04-29", + format = DateFormatUtils.Format.DATE_FORMAT + ) + Assert.assertEquals(DateTime(2015, 4, 29, 0, 0, 0), dateTime) + } + + @Test + fun `Assert Date format parsing with default value`() { + val currentDateTime = DateTime.now() + + val dateTime = DateFormatUtils.fromString( + value = "2015-04-29", + format = DateFormatUtils.Format.DATE_TIME_FORMAT, + defaultValue = currentDateTime + ) + Assert.assertEquals(currentDateTime, dateTime) + } +}