DEV Community

Discussion on: Data Type Conversions in Java

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

Hi, thanks for the article. One important addition to your article.

The simple date format on does not support ISO standardized timestamps. You can parse one special variant of it and you can convert hey date object to something that looks like an ISO timestamp. But it will not support all possible versions. If you want to do that, one should use the new Java API, for example the Instant class has a of() and a toString() method that will parse a string or create an ISO UTC timestamp string.
In general, I think Date is deprecated.

Collapse
 
attacomsian profile image
Atta • Edited

I agree with you SimpleDateFormat is not as efficient as new Java 8+ Date & Time APIs are. Since this tutorial is for beginners, I tried to use only well-known keywords and APIs.

Perhaps, I will write another tutorial on new Java Date & Time APIs especially LocalDate, LocalTime, LocalDateTime, ZonedDateTime etc.

Collapse
 
stealthmusic profile image
Jan Wedel

Oh, and to be clear, I’m not talking about efficiency. I’m saying that it does not support all versions of the ISO format. You have to do some ugly hacks and preprocessing.

Thread Thread
 
attacomsian profile image
Atta

Yes it requires some ugly preprocessing but it does support popular ISO 8601 date and time formats:

yyyy-MM-dd'T'HH:mm:ssXXX -> 2019-03-30T14:22:15+05:00

//set the timezone to UTC

yyyy-MM-dd'T'HH:mm:ss'Z' -> 2019-03-30T09:22:15Z
yyyyMMdd'T'HHmmss'Z' -> 20190330T092215Z

I updated the tutorial to include these ISO conversions.

Thread Thread
 
stealthmusic profile image
Jan Wedel

Yeah, I know but there is no single formatter that supports parsing all ISO compliant timestamps. The biggest issue is the time zone options. There „Z“ as well as „+00:00“ and others as well.

Thread Thread
 
attacomsian profile image
Atta

Yeah, timezone options are a bit confusing for developers. Before Java 8, I even used to have a special DateUtils class for handling these ugly conversions at one place.

Collapse
 
stealthmusic profile image
Jan Wedel

I also agree with you, but since ISO time stamps are so common (I would say 95% of timestamp strings I had to process were ISO string) and I’ve seen a couple of bugs because of wrong parsing. So people should not start with SimpleDateFormatter at any circumstances IMO.