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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You could use
Timestamp.valueOf(String)
. The documentation states that it understands timestamps in the formatyyyy-mm-dd hh:mm:ss[.f...]
, so you might need to change the field separators in your incoming string.Then again, if you're going to do that then you could just parse it yourself and use the
setNanos
method to store the microseconds.I believe you need to do this:
Option 2 Convert to the date format pointed out in Jon Skeet's answer and use that.
Here's the intended way to do it:
Admittedly, it only has millisecond resolution, but in all services slower than Twitter, that's all you'll need, especially since most machines don't even track down to the actual nanoseconds.
If you get time as string in format such as 1441963946053 you simply could do something as following:
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:Assuming you've already validated the string length, this will convert to the right format:
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 withInteger.parseInt
. You can then combine the values pretty easily: