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

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

I would like to use the 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

+5
source share
1 answer

You can create your own formatter as follows:

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

DateTimeFormatter Javadoc lists all possible markers with their value.

+1
source

Source: https://habr.com/ru/post/1231949/


All Articles