Java 8 Setting Global Time Formats

I want to set my own DateTimeFormatter as global formatting. When I do the following line:

ZonedDateTime.now(); 

I get:

 2016-03-30T08:58:54.180-06:00[America/Chicago] 

If I do this:

 ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME) 

I get:

 Wed, 30 Mar 2016 9:00:06 -0600 

I want to print above, but with am / pm, so I created my own formatter and printed the time like this:

 DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss a Z"); ZonedDateTime.now().format(FORMATTER); 

Who gave me:

 Wed, 30 Mar 2016 9:00:06 AM -0600 

But I use this .now() method everywhere for logging purposes, and I don't want to define a formatter everywhere in the code. Is there a way to configure the formatter as the default format to use when calling the .now() method? I think like a spring bean setup method or something .....

+8
java spring datetime java-8 javabeans
source share
2 answers

You can simply declare a constant in the class:

 class UtilsOrWhatever { public static final DateTimeFormater RFC_1123_DATE_TIME_AM_PM = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy hh:mm:ss a Z"); } 

and just use in your code:

 ZonedDateTime.now().format(RFC_1123_DATE_TIME_AM_PM); //needs a static import 

Alternatively, with pure Java EE 7, you can create a DateTimeFormatter @Produces using @Produces and then just @Inject it.

 import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; @ApplicationScoped public class RfcFormatterProducer { @Produces private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy hh:mm:ss a Z"); } 

In your code:

 @Inject DateTimeFormatter rfc; 

You can also give it a name, as in the link above, if you have several formatters.

+5
source share

Let me first explain why you get what you get when you don't name the format. toString is called in ZonedDateTime, which in turn calls toString on on DateTime and Offset, which calls toString in LocalDate and LocalTime. These toStrings do not use formatters, so even if you can specify a default formatter, it will not be called when it implicitly converts ZonedDateTime to a string.

There are many ways to simplify the creation of this formatted string. One way is to use a utility class that you would replace in all of your log statements. I do not necessarily suggest the following, but it most closely matches what you ask for:

 public class MyAwesomeUtility { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss a Z"); public static String getFormattedZonedDateTime() { return ZonedDateTime.now().format(FORMATTER); } } 

The next parameter is not the same as ZonedDateTime is final, as pointed out by @assylias. Leaving it here anyway.

If you really want to accomplish what you asked in the question that overrides the now method to return an object that would give the specified format when calling toString , you would need to do something like the following (NOTE: THIS IS ANTI-PATTERN. DO NOT DO THIS ):

 public class DefaultFormattedZonedDateTime extends ZonedDateTime { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss a Z"); @Overwrite public String toString() { this.format(FORMATTER); } } 

Then, since now in ZonedDateTime is static and still returns ZonedDateTime, you will need to use AspectJ (you cannot use AOP in the static method only with spring) to return this new object instead.

0
source share

All Articles