title: DateTime formatting to have trailing zeroes and snap to milli, micro or nano format
body: <p>I have some code like this</p>
  
  <pre><code>import java.time.Instant
  
  
  val tstamp = 1546344620.1877
  val nanoAdj = ((BigDecimal(tstamp) - tstamp.toLong) * 1000000000).toLong
  Instant.ofEpochSecond(tstamp.toLong, nanoAdj).toString
  // will print : 2019-01-01T12:10:20.187700Z
  </code></pre>
  
  <p>The <code>toString</code> from the Instant object created from <code>ofEpochSecond</code> is great with trailing zeroes to milli/micro/nano group, but I'm struggling to get the formatted to do the same. I need to change the format a little to something like <code>2019-01-01 12:10:20.187700 UTC</code></p>
  
  <p>Other examples:</p>
  
  <pre><code>2019-01-01 12:10:20 UTC // no fractions
  2019-01-01 12:10:20.180 UTC // milliseconds
  2019-01-01 12:10:20.187700 UTC // microseconds
  2019-01-01 12:10:20.187738200 UTC // nanoseconds
  </code></pre>
  
  <p>I'm using DateTimeFormatter like below, but I'm open for other suggestions.</p>
  
  <pre><code>def formatter: DateTimeFormatter = {
        val tz_format = new DateTimeFormatterBuilder()
          .optionalStart
          .parseCaseSensitive()
          .appendZoneRegionId()
          .toFormatter
  
        val datetime_format = new DateTimeFormatterBuilder()
          .appendValue(HOUR_OF_DAY, 2)
          .appendLiteral(':')
          .appendValue(MINUTE_OF_HOUR, 2)
          .optionalStart()
          .appendLiteral(':')
          .appendValue(SECOND_OF_MINUTE, 2)
          .optionalStart()
          .appendFraction(NANO_OF_SECOND, 0, 9, true)
          .toFormatter
  
        new DateTimeFormatterBuilder()
          .parseCaseInsensitive
          .append(ISO_LOCAL_DATE)
          .appendLiteral(' ')
          .append(datetime_format)
          .appendLiteral(' ')
          .append(tz_format)
          .toFormatter
          .withZone(ZoneId.of("UTC"))
      }
  </code></pre>