kiran kumar
kiran kumar

Reputation: 153

Java String to Date object of the format “yyyy-mm-dd HH:mm:ss.SSSS”

  public static void main(String[] args) throws ParseException {
        java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
                .parse("2015-03-24 20:12:20.135000");
        System.out.println(temp);

For the same code when I use different seconds I am getting output as Tue Mar 24 20:14:35 CET 2015. I am getting everything correct except minutes and seconds. My tasks is to print the data in column which is 2015-03-24 01:55:23.999000 to round off the seconds and show it as 2015-03-24 01:55:24. Can you please tell me how it is possible or how to truncate

Upvotes: 1

Views: 824

Answers (1)

rgettman
rgettman

Reputation: 178333

The S format character for SimpleDateFormat means milliseconds, not microseconds.

It's interpreting 135000 as 135,000 milliseconds, or 135 seconds. Then it adds 2 minutes and 15 seconds, yielding 20:14:35 instead of 20:12:20.135.

You'll need to truncate the string to read "2015-03-24 20:12:20.135" so 135 milliseconds is interpreted correctly.

Upvotes: 7

Related Questions