I have a value in column which is of type timestamp. Lets say I have a value 2007-05-04 08:48:40.969774
Now, when trying to fetch the value from the database and return this timestamp value to a function, what SimpleDateFormatter pattern should I use so that the fraction part beside the seconds also gets returned.
I have used yyyy-MM-dd hh:mm:ss, but that only returns till the seconds and ignores the fraction present beside the seconds(.969774). I need help in returning this fraction part also, with a 6-digits precision.
The default ways of formatting a java.util.Date
(or java.sql.Timestamp
) has only millisecond precision. You can use yyyy-MM-dd hh:mm:ss.SSS
to get that millisecond precision.
A java.sql.Timestamp
actually does have (up to) nanosecond precision (assuming the database server and the driver actually support it). The easiest way to format it in Java 8 is to convert the timestamp to a java.time.LocalDateTime
(using Timestamp.toLocalDateTime()
) and use the java.time
formatting options in java.time.format.DateTimeFormatter
which support up to nanoseconds.
If you use Java 7 or earlier it will take some extra effort, as the normal date formatters don't support it. For example you could use a dateformatter with pattern yyyy-MM-dd hh:mm:ss
(to only format up to seconds) and append the sub-second nano seconds (with appropriate zero-padding) of Timestamp.getNanos()
yourself.
You have to have a way of obtaining a micro-second timestamp. I use System.currentTimeMillis() with System.nanoTime() in combination. Then you need a way to display it. You can divide it by 1000 and display milliseconds as normal, then display the last 3 digits of the time. i.e. Have a time which is like
long timeUS = System.currentTimeMillis() * 1000 + micros;
here is a more detailed example
HiresTimer.java and HiresTimerTest.java
The test prints
2012/04/09T14:22:13.656008
2012/04/09T14:22:13.656840
2012/04/09T14:22:13.656958
2012/04/09T14:22:13.657066
....
2012/04/09T14:22:13.665249
2012/04/09T14:22:13.665392
2012/04/09T14:22:13.665473
2012/04/09T14:22:13.665581
EDIT: The relevant code is
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss.SSS");
private static final DecimalFormat DF = new DecimalFormat("000");
public static String toString(long timeUS) {
return SDF.format(timeUS / 1000) + DF.format(timeUS % 1000);
}
In Java 8 and later, the java.time package has support for parsing and manipulating date/times to nanosecond precision. That means up to 9 digits in the fraction of a second.
Dealing with nanoseconds in Java is not trivial. I cannot see any elegant solution other than dealing with them in a separate way (format date and time with SimpleDateFormat
and nanoseconds with DecimalFormat
), for example, like in the following example:
package test;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.start();
}
private void start() {
long time = System.currentTimeMillis();
Date d = new Date(time);
Timestamp t = new Timestamp(time);
t.setNanos(123456789);
System.out.println(d);
System.out.println(t);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
NumberFormat nf = new DecimalFormat("000000000");
System.out.println(df.format(t.getTime()) + nf.format(t.getNanos()));
}
}
The output produced is (in my country, my locale):
Mon Apr 09 16:27:27 CEST 2012
2012-04-09 16:27:27.123456789
2012-04-09 16:27:27.123456789