diff --git a/README.md b/README.md index 8271d1f..0e06e84 100644 --- a/README.md +++ b/README.md @@ -17,22 +17,22 @@ This library empowers Java Time & makes it a lot of **fun**! 😃 #### 1. Parsing ```kotlin // Provided time -val result = "01:30 AM".parseLocalTime() +val result = "01:30 AM".toLocalTime() // Provided local date -val result = "2021-06-07".parseLocalDate() +val result = "2021-06-07".toLocalDate() // Provided ambiguous date formats -val result = "06/07/2021".parseLocalDate(format = "MM/dd/yyyy") +val result = "06/07/2021".toLocalDate(format = "MM/dd/yyyy") // Automatic time zone conversions -val result = "2021-10-04T10:10:00+0000".parseZonedDateTime() +val result = "2021-10-04T10:10:00+0000".toZonedDateTime() // Maintain original time zone -val result = "2021-10-04T10:10:00+0000".parseZonedDateTime(useSystemTimeZone = false) +val result = "2021-10-04T10:10:00+0000".toZonedDateTime(useSystemTimeZone = false) // Parse LocalDate as ZonedDateTime -val result = "2021-06-07".parseZonedDateTime() +val result = "2021-06-07".toZonedDateTime() ``` #### 2. Creation ```kotlin @@ -69,7 +69,7 @@ val result = dateA.isAfterEqualTime(dateB) #### 4. Print ```kotlin -val date = "2021-07-06".parseZonedDateTime() +val date = "2021-07-06".toZonedDateTime() val result = date.print(format = "MM/dd/yyyy") ``` diff --git a/src/main/kotlin/javatimefun/localdate/extensions/LocalDateParsingExtensions.kt b/src/main/kotlin/javatimefun/localdate/extensions/LocalDateParsingExtensions.kt index 64a86ef..a91bd48 100644 --- a/src/main/kotlin/javatimefun/localdate/extensions/LocalDateParsingExtensions.kt +++ b/src/main/kotlin/javatimefun/localdate/extensions/LocalDateParsingExtensions.kt @@ -8,11 +8,11 @@ import java.time.format.DateTimeParseException * Works off of String representations of date, without time, nor time zone. * When a format is present, it'll try parsing using that format alone, & return null if it fails. * - * @param this String representation of LocalDate. - * @param format String representing format that should solely be used when parsing the date. - * @return LocalDate? Null means couldn't parse, else parsed LocalDate. + * @param this String representation of LocalDate. + * @param format String representing format that should solely be used when parsing the date. + * @return LocalDate? Null means couldn't parse, else parsed LocalDate. */ -fun String.parseLocalDate(format: String? = null): LocalDate? = +fun String.toLocalDate(format: String? = null): LocalDate? = if (format.isNullOrEmpty()) { try { LocalDate.parse(this) diff --git a/src/main/kotlin/javatimefun/localdatetime/extensions/LocalDateTimeParsingExtensions.kt b/src/main/kotlin/javatimefun/localdatetime/extensions/LocalDateTimeParsingExtensions.kt index 75d9337..ec3ae9a 100644 --- a/src/main/kotlin/javatimefun/localdatetime/extensions/LocalDateTimeParsingExtensions.kt +++ b/src/main/kotlin/javatimefun/localdatetime/extensions/LocalDateTimeParsingExtensions.kt @@ -1,6 +1,6 @@ package javatimefun.localdatetime.extensions -import javatimefun.localdate.extensions.parseLocalDate +import javatimefun.localdate.extensions.toLocalDate import java.time.LocalDateTime import java.time.LocalTime import java.time.format.DateTimeFormatter @@ -10,21 +10,21 @@ import java.time.format.DateTimeParseException * Works off of String representations of date(time) and parses through the following attempts in order when * no format is present: *

- * When a format is present, it'll try parsing using that format alone, & return null if it fails. + * When a format is present, tries parsing using that format alone, & return null if it fails. * - * @param this String representation of either LocalDate, or LocalDateTime. - * @param format String representing format that should solely be used when parsing the date. - * @return LocalDateTime? Null means couldn't parse, else parsed LocalDateTime. + * @param this String representation of either LocalDate, or LocalDateTime. + * @param format String representing format that should solely be used when parsing the date. + * @return LocalDateTime? Null means couldn't parse, else parsed LocalDateTime. */ -fun String.parseLocalDateTime(format: String? = null): LocalDateTime? { +fun String.toLocalDateTime(format: String? = null): LocalDateTime? { val localDateTime = parseLocalDateTimeHelper(this, format) if (localDateTime != null) { return localDateTime } - val localDate = this.parseLocalDate(format) + val localDate = this.toLocalDate(format) if (localDate != null) { return LocalDateTime.of(localDate, LocalTime.MIN) } diff --git a/src/main/kotlin/javatimefun/localtime/extensions/LocalTimeParsingExtensions.kt b/src/main/kotlin/javatimefun/localtime/extensions/LocalTimeParsingExtensions.kt index 6f731f8..2770772 100644 --- a/src/main/kotlin/javatimefun/localtime/extensions/LocalTimeParsingExtensions.kt +++ b/src/main/kotlin/javatimefun/localtime/extensions/LocalTimeParsingExtensions.kt @@ -4,7 +4,7 @@ import java.time.LocalTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeParseException -fun String.parseLocalTime(format: String? = null): LocalTime? = +fun String.toLocalTime(format: String? = null): LocalTime? = if (format.isNullOrEmpty()) { try { LocalTime.parse(this) diff --git a/src/main/kotlin/javatimefun/zoneddatetime/extensions/ZonedDateTimeParsingExtensions.kt b/src/main/kotlin/javatimefun/zoneddatetime/extensions/ZonedDateTimeParsingExtensions.kt index d7a08d8..e49bdc7 100644 --- a/src/main/kotlin/javatimefun/zoneddatetime/extensions/ZonedDateTimeParsingExtensions.kt +++ b/src/main/kotlin/javatimefun/zoneddatetime/extensions/ZonedDateTimeParsingExtensions.kt @@ -1,6 +1,6 @@ package javatimefun.zoneddatetime.extensions -import javatimefun.localdatetime.extensions.parseLocalDateTime +import javatimefun.localdatetime.extensions.toLocalDateTime import javatimefun.zoneddatetime.ZonedDateTimeUtil import java.time.ZoneId import java.time.ZonedDateTime @@ -12,18 +12,18 @@ import java.time.format.DateTimeParseException * no format is present: *

- * When a format is present, it'll try parsing using that format alone, & return null if it fails. + * When a format is present however, it'll try parsing using that format alone, & return null if it fails. * * @param this String representation of either MsftDate, LocalDate, LocalDateTime, or ZonedDateTime. * @param format String representing format that should solely be used when parsing the date. * @param useSystemTimeZone If true, converts parsed date to system default timezone, else keeps original time zone. - * @return ZonedDateTime? Null means couldn't parse, else parsed ZonedDateTime. + * @return ZonedDateTime? Null means couldn't parse, else parsed ZonedDateTime. */ -fun String.parseZonedDateTime( +fun String.toZonedDateTime( format: String? = null, useSystemTimeZone: Boolean = true ): ZonedDateTime? { @@ -34,7 +34,7 @@ fun String.parseZonedDateTime( } return zonedDateTime } - val localDateTime = this.parseLocalDateTime(format) + val localDateTime = this.toLocalDateTime(format) if (localDateTime != null) { return ZonedDateTime.of(localDateTime, ZoneId.systemDefault()) } diff --git a/src/test/kotlin/zoneddatetime/ZonedDateTimeMutatingExtensionsTest.kt b/src/test/kotlin/zoneddatetime/ZonedDateTimeMutatingExtensionsTest.kt index b6203fa..694fe1b 100644 --- a/src/test/kotlin/zoneddatetime/ZonedDateTimeMutatingExtensionsTest.kt +++ b/src/test/kotlin/zoneddatetime/ZonedDateTimeMutatingExtensionsTest.kt @@ -1,6 +1,6 @@ package zoneddatetime -import javatimefun.localtime.extensions.parseLocalTime +import javatimefun.localtime.extensions.toLocalTime import javatimefun.localtime.extensions.print import javatimefun.zoneddatetime.ZonedDateTimeUtil import org.junit.jupiter.api.Assertions @@ -74,7 +74,7 @@ class ZonedDateTimeMutatingExtensionsTest { // given var dateA = ZonedDateTimeUtil.new(2020, 3, 20) val timeText = "07:35:11 AM" - val time: LocalTime = timeText.parseLocalTime(HH_MM_SS_AM) ?: throw RuntimeException("Failed to parse") + val time: LocalTime = timeText.toLocalTime(HH_MM_SS_AM) ?: throw RuntimeException("Failed to parse") // when dateA = dateA.withLocalTime(time) diff --git a/src/test/kotlin/zoneddatetime/ZonedDateTimeParsingExtensionsTest.kt b/src/test/kotlin/zoneddatetime/ZonedDateTimeParsingExtensionsTest.kt index 3382f90..f07ea52 100644 --- a/src/test/kotlin/zoneddatetime/ZonedDateTimeParsingExtensionsTest.kt +++ b/src/test/kotlin/zoneddatetime/ZonedDateTimeParsingExtensionsTest.kt @@ -2,7 +2,7 @@ package zoneddatetime import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test -import javatimefun.zoneddatetime.extensions.parseZonedDateTime +import javatimefun.zoneddatetime.extensions.toZonedDateTime import javatimefun.zoneddatetime.extensions.print import java.lang.RuntimeException import java.time.ZonedDateTime @@ -20,7 +20,7 @@ class ZonedDateTimeParsingExtensionsTest { val dateInText = "2021-06-07" // when - val dateParsed: ZonedDateTime = dateInText.parseZonedDateTime() ?: throw RuntimeException("Failed to parse") + val dateParsed: ZonedDateTime = dateInText.toZonedDateTime() ?: throw RuntimeException("Failed to parse") // then assertEquals(dateInText, dateParsed.print(YYYY_MM_DD_DASH)) @@ -32,7 +32,7 @@ class ZonedDateTimeParsingExtensionsTest { val dateInText = "2021-06-07" // when - val dateParsed: ZonedDateTime = dateInText.parseZonedDateTime(format = YYYY_MM_DD_DASH) + val dateParsed: ZonedDateTime = dateInText.toZonedDateTime(format = YYYY_MM_DD_DASH) ?: throw RuntimeException("Failed to parse") // then @@ -45,7 +45,7 @@ class ZonedDateTimeParsingExtensionsTest { val dateInText = "06/07/2021" // when - val dateParsed: ZonedDateTime = dateInText.parseZonedDateTime(format = MM_DD_YYYY_SLASH) ?: throw RuntimeException("Failed to parse") + val dateParsed: ZonedDateTime = dateInText.toZonedDateTime(format = MM_DD_YYYY_SLASH) ?: throw RuntimeException("Failed to parse") // then assertEquals(dateInText, dateParsed.print(MM_DD_YYYY_SLASH))