Skip to content

Instantly share code, notes, and snippets.

@TJC
Last active March 18, 2022 04:53
Show Gist options
  • Select an option

  • Save TJC/2504b2d7d8be1065eae80486382469f0 to your computer and use it in GitHub Desktop.

Select an option

Save TJC/2504b2d7d8be1065eae80486382469f0 to your computer and use it in GitHub Desktop.

Revisions

  1. TJC revised this gist Mar 18, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mysql-formatter.scala
    Original file line number Diff line number Diff line change
    @@ -17,10 +17,10 @@ val mysqlFormatter = new DateTimeFormatterBuilder().appendValue(YEAR, 4)
    .appendValue(MINUTE_OF_HOUR, 2)
    .optionalStart.appendLiteral(':')
    .appendValue(SECOND_OF_MINUTE, 2)
    .optionalStart.appendFraction(NANO_OF_SECOND, 5, 6, true)
    .optionalStart.appendFraction(NANO_OF_SECOND, 1, 6, true)
    .toFormatter()

    val s = "2022-03-18 13:56:01.85734"
    val s = "2022-03-18 13:56:01.857"
    val x = LocalDateTime.parse(s, mysqlFormatter)
    println(x)
    println(f.format(x))
  2. TJC renamed this gist Mar 18, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. TJC created this gist Mar 18, 2022.
    26 changes: 26 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import java.time._
    import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
    import java.time.temporal.ChronoField._

    // This one doesn't work, because Mysql can return five-digits of precision, if the sixth digit was 0
    val badFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")


    val mysqlFormatter = new DateTimeFormatterBuilder().appendValue(YEAR, 4)
    .appendLiteral('-')
    .appendValue(MONTH_OF_YEAR, 2)
    .appendLiteral('-')
    .appendValue(DAY_OF_MONTH, 2)
    .appendLiteral(' ')
    .appendValue(HOUR_OF_DAY, 2)
    .appendLiteral(':')
    .appendValue(MINUTE_OF_HOUR, 2)
    .optionalStart.appendLiteral(':')
    .appendValue(SECOND_OF_MINUTE, 2)
    .optionalStart.appendFraction(NANO_OF_SECOND, 5, 6, true)
    .toFormatter()

    val s = "2022-03-18 13:56:01.85734"
    val x = LocalDateTime.parse(s, mysqlFormatter)
    println(x)
    println(f.format(x))