Got a String coming in with this format: YYYY-MM-DD-HH.MM.SS.NNNNNN The Timestamp is coming from a DB2 database. I need to parse it into a java.sql.Timestamp and NOT lose any precison. So far I've been unable to find existing code to parse that far out to microseconds. SimpleDateFormat returns a Date and only parses to milliseconds. Looked at JodaTime briefly and didn't see that would work either.
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
A timestamp is mainly used in databases to represent the exact time of some event. The Timestamp class we will use in this tutorial is a part of the java.sql.Timestamp package. We will use the TimeStamp class’s own static function - valueOf (). It takes a string as an argument and then converts it to a timestamp.
first convert your date string to date then convert it to timestamp by using following set of line. Date date=new Date (); Timestamp timestamp = new Timestamp (date.getTime ());//instead of date put your converted date Timestamp myTimeStamp= timestamp; Share.
The simplest way to parse a String to a Timestamp is its valueOf method: Timestamp.valueOf("2018-11-12 01:02:03.123456789") And when our String is in JDBC timestamp format – yyyy-m[m]-d[d] hh:mm : ss [.f…] – then it's pretty simple.
A timestamp is mainly used in databases to represent the exact time of some event. The Timestamp class we will use in this tutorial is a part of the java.sql.Timestamp package. We will use the TimeStamp class’s own static function - valueOf ().
Have you tried using Timestamp.valueOf(String)
? It looks like it should do almost exactly what you want - you just need to change the separator between your date and time to a space, and the ones between hours and minutes, and minutes and hours, to colons:
import java.sql.*; public class Test { public static void main(String[] args) { String text = "2011-10-02 18:48:05.123456"; Timestamp ts = Timestamp.valueOf(text); System.out.println(ts.getNanos()); } }
Assuming you've already validated the string length, this will convert to the right format:
static String convertSeparators(String input) { char[] chars = input.toCharArray(); chars[10] = ' '; chars[13] = ':'; chars[16] = ':'; return new String(chars); }
Alternatively, parse down to milliseconds by taking a substring and using Joda Time or SimpleDateFormat
(I vastly prefer Joda Time, but your mileage may vary). Then take the remainder of the string as another string and parse it with Integer.parseInt
. You can then combine the values pretty easily:
Date date = parseDateFromFirstPart(); int micros = parseJustLastThreeDigits(); Timestamp ts = new Timestamp(date.getTime()); ts.setNanos(ts.getNanos() + micros * 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With