PreciseTimestampFormatter implements TimestampFormatter { @Override public String format(final Timestamp timestamp) { Instant instant = timestamp.toInstant(); return truncationUnit == null ? formatter.format(instant) : format(instant.truncatedTo(truncationUnit)); } PreciseTimestampFormatter(final String pattern, final Locale locale); @Override boolean isValid(final String timestamp); @Override String format(final Timestamp timestamp);  }
@Test public void minutePrecision() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("yyyy-MM-dd HH:mm", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 0))).isEqualTo("2016-02-01 12:30"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_000_000))).isEqualTo("2016-02-01 12:30"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("2016-02-01 12:30"); }
@Test public void secondPrecision() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("yyyy-MM-dd HH:mm:ss", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 0))).isEqualTo("2016-02-01 12:30:55"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_000_000))).isEqualTo("2016-02-01 12:30:55"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("2016-02-01 12:30:56"); }
@Test public void millisecondPrecision() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("HH:mm:ss.SSS", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_000_000))).isEqualTo("12:30:55.999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_999))).isEqualTo("12:30:55.999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("12:30:56.000"); }
@Test public void microsecondPrecision() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("HH:mm:ss.SSSSSS", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_000))).isEqualTo("12:30:55.999999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_999))).isEqualTo("12:30:55.999999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("12:30:56.000000"); }
@Test public void nanosecondPrecision() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("HH:mm:ss.SSSSSSSSS", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 1))).isEqualTo("12:30:55.000000001"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_999))).isEqualTo("12:30:55.999999999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("12:30:56.000000000"); }
@Test public void nanosecondOfSecond() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("HH:mm:ss / n", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 1))).isEqualTo("12:30:55 / 1"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_999))).isEqualTo("12:30:55 / 999999999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("12:30:56 / 0"); }
@Test public void nanosecondOfDay() { PreciseTimestampFormatter formatter = new PreciseTimestampFormatter("yyyyMMdd N", Locale.US); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 0))).isEqualTo("20160201 45055000000000"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 55, 999_999_999))).isEqualTo("20160201 45055999999999"); assertThat(formatter.format(TimestampFactory.create(2016, 2, 1, 12, 30, 56, 0))).isEqualTo("20160201 45056000000000"); }