I have a method to parse a String (yyyy-MM-dd HH: mm: ss.SSS) for a Date object using SimpleDateFormat.
public static Timestamp convertToTimestamp(String stringToFormat) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); try { Date date = dateFormat.parse(stringToFormat); Timestamp tstamp = new Timestamp(date.getTime()); return tstamp; } catch (ParseException e) { return null; } }
However, when the milliseconds end with 0, for example, "2013-07-07 19: 15: 00.000", when I do the following to assign it to a Timestamp object:
Timestamp tstamp = new Timestamp(date.getTime());
The conclusion is as follows: 2013-07-07 19: 15: 00.0
Is there a way to keep my precision of three decimal places in milliseconds? I understand that I could probably do some length checking and manually add to 0, but a more efficient, standard way to maintain this precision would be preferable
source share