Code Optimization

I saw this code, and I feel like there has to be a better way to do it. Any ideas?

    import java.time.ZoneId
    import java.time.format.DateTimeFormatter

    static final DateTimeFormatter timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX").withZone(ZoneId.of("UTC"))
    timestampFormatter.format(ec.user.nowTimestamp.toInstant())

Figured it out:

ec.user.nowTimestamp.toInstant().atZone(ZoneOffset.UTC.normalized()).format(DateTimeFormatter.ISO_INSTANT)

Another option:

ec.l10n.format(ec.user.nowTimestamp, "yyyy-MM-dd'T'HH:mm:ssX", null, TimeZone.getTimeZone("UTC"))

The 3rd parameter is the Locale (irrelevant in this case, but if null uses the current user’s locale or the system default locale). The 3rd and 4th parameters can be left off and each default to the user’s setting (including time zone) or the system default setting if no user or the user has no setting (on UserAccount).

1 Like