Skip to content

Commit

Permalink
Rename parse methods to use to instead of parse (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
seljabali authored Feb 12, 2024
1 parent d5a0922 commit f4f26c1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
* <p><ul>
* <li>First, it'll try parsing as LocalDateTime, if successful, uses systemTimeZone.
* <li>Lastly, it'll try parsing as LocalDate, if successful, adds start of daytime, & systemTimeZone.
* <li>First, tries parsing as LocalDateTime, if successful, uses systemTimeZone.
* <li>Lastly, tries parsing as LocalDate, if successful, adds start of daytime, & systemTimeZone.
* </ul><p>
* 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,18 +12,18 @@ import java.time.format.DateTimeParseException
* no format is present:
* <p><ul>
* <li>First, if the date is identified as MsftDate, then it'll attempt to parse & return value.
* <li>Second, it'll try parsing as ZonedDateTime, if successful converts to systemTimeZone per param value.
* <li>Third, it'll try parsing as LocalDateTime, if successful, uses systemTimeZone.
* <li>Lastly, it'll try parsing as LocalDate, if successful, adds start of daytime, & systemTimeZone.
* <li>Second, try to parse as ZonedDateTime, if successful returns value & converts to systemTimeZone if param value is set.
* <li>Third, try to parse as LocalDateTime, if successful returns value & converts to systemTimeZone if param value is set.
* <li>Lastly, try to parse as LocalDate, if successful returns value & adds start of daytime, & converts to systemTimeZone if param value is set, else returns null.
* </ul><p>
* 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? {
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit f4f26c1

Please sign in to comment.