Current Date/Time custom formatted

I just like to double check:
In order to get in DSL Rules current date/time in custom format a monster code like this is required?

now().format(java.time.format.DateTimeFormatter.ofPattern("dd.MM.YYYY, HH:mm")

This code works but if I step back a little bit I think, this can’t be serious :slight_smile:

1 Like

It looks like you had a go at the Documentation.
If you have a better solution, post it.

This probably comes from a greatly underestimated assessment of the sheer complexity date times present.

That is one way to do it and there are others of course but ultimately it’s going to require the same three things:

  1. a DateTime
  2. a formatter
  3. a String that tells the formatter how you want the date time represented which is almost a whole programming language unto itself given how many different ways there are to represent a date time world wide.

Another example way to do it could be

String.format("%1td.%1tm.%1tY, %1tH:%1tM", now)

See Formatter (Java SE 11 & JDK 11 )

In that case we still have the three elements. We’ve just replaced the formatter with String.format instead of using DateTimeFormatter.

3 Likes

Hey Rick,
as always, I very much appreciate your answer and your profound knowledge as well as your willingness to share your knowledge!

The above example didn’t work for me…
but this did:

String.format("%1$td.%1$tm.%1$tY, %1$tH:%1$tM", now)
2 Likes