Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom variant of the ISO_INSTANT DateTimeFormatter that does not use colons?

I would like to convert instances of class java.time.Instant to and from Strings.

I would like to use a format exactly like java.time.format.DateTimeFormatter.ISO_INSTANT with the only difference that the colons in the format are omitted or replaced by dots so that they can be used without escaping in file names and URLs.

Example: 2011-12-03T10.15.30.001Z instead of 2011-12-03T10:15:30.001Z

See Javadoc for ISO_INSTANT: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT

like image 353
Gustave Avatar asked Sep 21 '15 12:09

Gustave


1 Answers

You could build your own formatter like this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH.mm.ss.SSSVV")

The DateTimeFormatter Javadoc lists all possible tokens with their signification.

like image 74
Tunaki Avatar answered Nov 15 '22 03:11

Tunaki