Skip to content

Commit

Permalink
Fix argless EnrichedDate.format
Browse files Browse the repository at this point in the history
  • Loading branch information
ddworak committed Aug 14, 2024
1 parent 0ab53c1 commit 6a661fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.avsystem.scex.util

import java.text.{DateFormat, SimpleDateFormat}
import java.text.SimpleDateFormat

object CommonDateFormat {
private val dateFormatTL = new ThreadLocal[DateFormat] {
override def initialValue(): DateFormat = {
private val dateFormatTL = new ThreadLocal[SimpleDateFormat] {
override def initialValue(): SimpleDateFormat = {
val df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss")
df.setLenient(false)
df
}
}

def get: DateFormat = dateFormatTL.get
def get: SimpleDateFormat = dateFormatTL.get
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import java.time.temporal.ChronoField
import java.time.{Clock, Instant, ZoneId, ZonedDateTime}
import java.util.{Calendar, Date}

final class EnrichedDate(wrapped: Date, zone: ZoneId = Clock.systemDefaultZone.getZone) {
final class EnrichedDate(wrapped: Date, zone: ZoneId = Clock.systemDefaultZone().getZone) {
private def zonedDateTime: ZonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(wrapped.getTime), zone)

@Documentation("Formats the date using the default date format: <tt>yyyy.MM.dd HH:mm:ss</tt>.")
def format: String = CommonDateFormat.get.format(wrapped)
def format: String =
if (zone == Clock.systemDefaultZone().getZone) {
CommonDateFormat.get.format(wrapped)
} else
format(CommonDateFormat.get.toPattern)

@Documentation("Formats the date according to provided date format. An example of correct date format is <tt>yyyy.MM.dd HH:mm:ss</tt>.")
def format(dateFormat: String): String = DateTimeFormatter.ofPattern(dateFormat).format(zonedDateTime)
Expand Down

0 comments on commit 6a661fe

Please sign in to comment.