Java 8设置全局时间格式化程序 [英] Java 8 Setting global time formatters
问题描述
我想将自己的DateTimeFormatter设置为全局格式化程序。当我执行以下行:
ZonedDateTime.now();
我得到:
2016-03-30T08:58:54.180-06:00 [美国/芝加哥]
如果我这样做:
ZonedDateTime.now()。format(DateTimeFormatter.RFC_1123_DATE_TIME)
我得到:
周三,2016年3月30日9:00:06 -0600
我想要上面打印的内容, pm,所以我做了我的自定义格式化和打印出来的时间如下:
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(EEE,dd MMM yyyy HH:mm:ss a Z);
ZonedDateTime.now()。format(FORMATTER);
哪个给了我:
Wed,30 Mar 2016 9:00:06 AM -0600
但是我使用这个 .now()
方法进行日志记录,我不想在代码中的任何地方定义格式化程序。有没有办法将格式化程序配置为在调用 .now()
方法时使用的默认格式?我正在想Spring Spring配置方法或者某些东西.....
你可以简单地在类中声明一个常量:
class UtilsOrWhatever {
public static final DateTimeFormater RFC_1123_DATE_TIME_AM_PM = DateTimeFormatter.ofPattern(EEE,dd MMM yyyy hh: mm:ss a Z);
}
只需在代码中使用:
ZonedDateTime.now()。format(RFC_1123_DATE_TIME_AM_PM); //需要静态导入
或者,使用纯Java EE 7,您可以创建一个DateTimeFormatter生产者,其中 @Produces
然后简单地 @Inject
它。
import javax .enterprise.context.ApplicationScoped;
导入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);
}
在您的代码中:
@Inject DateTimeFormatter rfc;
如果你有几个格式化程序,你也可以给它一个名字,如上面的链接。 >
I want to set my own DateTimeFormatter as the global formatter. 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 what's printed above but with am/pm so I made my custom formatter and printed out the time like so:
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss a Z");
ZonedDateTime.now().format(FORMATTER);
Which gave me:
Wed, 30 Mar 2016 9:00:06 AM -0600
But I use this .now()
method everywhere for logging purposes and I dont want to define the 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'm thinking like spring bean configuration method or something.....
You could simply declare a constant in a 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 simply use in your code:
ZonedDateTime.now().format(RFC_1123_DATE_TIME_AM_PM); //needs a static import
Alternatively, with pure Java EE 7, you could create a DateTimeFormatter Producer with @Produces
and then simply @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 could also give it a name like in the link above if you have several formatters.
这篇关于Java 8设置全局时间格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!