Java 時刻のマイクロ秒精度を表現

JSR-310 java.time.LocalDateTime で、ナノ秒まで表現できる。となったものの
実際OS依存で環境によって、本当に1ナノ秒まで取得できるわけではない。
Windows PCであれば、100ナノ以下は結局、00 であった。
LocalDateTime から java.time.format.DateTimeFormatter 「S」でナノ秒までの文字列

LocalDateTime now = LocalDateTime.now();
String timestring = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS"));

LocalDateTime#getNano() を使う

LocalDateTime now = LocalDateTime.now();
String timestring = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss."))
                  + String.format("%09d", now.getNano());

TimeStamp ミリ秒とナノ秒で現在時刻を生成して

public static Timestamp getNowTimestamp(){
    long millis = System.currentTimeMillis();
    long nanos = System.nanoTime();
    Timestamp timestamp = new Timestamp(millis);
    timestamp.setNanos((int)(nanos % 1000000000));
    return timestamp;
}

TimeStamp → LocalDateTime 変換して、ナノ秒までの文字列

Timestamp timestamp = getNowTimestamp();
String timestring = timestamp.toLocalDateTime()
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss."))
                  + String.format("%09d", now.getNano());

マイクロ秒の時刻文字列は、

LocalDateTime now = LocalDateTime.now();
String timestring = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));

LocalDateTime#getNano() を使う場合

LocalDateTime now = LocalDateTime.now();
String timestring = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss."))
                  + String.format("%09d", now.getNano()).substring(0, 6);

TimeStamp → LocalDateTime 変換して、ナノ秒までの文字列

Timestamp timestamp = getNowTimestamp();
String timestring = timestamp.toLocalDateTime()
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss."))
                  + String.format("%09d", now.getNano()).substring(0, 6);