Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I'm using SimpleDateFormat to parse a string containing a date/time, but the result has a different date, hour, minute, second and millisecond: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM...
#1: Initial revision
How to parse a date with more than 3 decimal digits in the fractions of second?
I'm using `SimpleDateFormat` to parse a string containing a date/time, but the result has a different date, hour, minute, second and millisecond: ```java SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS"); Date date = sdf.parse("2021-10-01T10:30:45.123456789"); System.out.println(date); // Sat Oct 02 20:48:21 BRT 2021 // get the millisecond value Calendar cal = Calendar.getInstance(); cal.setTime(date); System.out.println(cal.get(Calendar.MILLISECOND)); // 789 ``` As you can see, the input date is October 1<sup>st</sup> 2021, at 10:30:45 AM, but the output date is October **2<sup>nd</sup>** 2021, at **8:48:21 PM**. As printing a `Date` uses its `toString()` method, and it doesn't show the milliseconds, I've used a `Calendar` to get this value: I expected it to be `123`, but it was `789`. I noticed that it happens only when the fraction of seconds has more than 3 decimal digits (with 3 or less digits, it works). But I need to parse dates with up to 9 decimal digits, so how can I do it? And why is `SimpleDateFormat` changing the date/time values?