Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

80%
+6 −0
Q&A 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: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM...

1 answer  ·  posted 2y ago by hkotsubo‭  ·  last activity 2y ago by hkotsubo‭

#1: Initial revision by user avatar hkotsubo‭ · 2021-08-20T16:22:44Z (over 2 years ago)
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?