Java Quartz 默认时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19545181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Quartz default timezone
提问by Fandic
I run Tomcat with -Duser.timezone=UTC
. However Quartz scheduler 2.2.1 seems to run in Europe/Prague which is my OS timezone.
我用-Duser.timezone=UTC
. 然而 Quartz 调度程序 2.2.1 似乎在欧洲/布拉格运行,这是我的操作系统时区。
Is there a way to run Quartz in custom timezone or determine which timezone Quartz is using? If not, is there a way to determine OS timezone programatically?
有没有办法在自定义时区运行 Quartz 或确定 Quartz 正在使用哪个时区?如果没有,有没有办法以编程方式确定操作系统时区?
回答by Robert Gannon
You can call setTimeZone()to set the time zone of your choosing for anything in Quartz that inherits BaseCalendar.
您可以调用setTimeZone()为 Quartz 中继承BaseCalendar 的任何内容设置您选择的时区。
Java's TimeZoneclass has a getDefault()which should aid in determining OS timezone programmatically.
Java 的TimeZone类有一个getDefault(),它应该有助于以编程方式确定操作系统时区。
回答by Paul Vargas
If you are using the XML configuration file, e.g. the quartz-config.xml
from Example To Run Multiple Jobs In Quartzof mkyong, you can configure the timezone in the element time-zone
:
如果您使用的是 XML 配置文件,例如quartz-config.xml
来自mkyong 的在 Quartz中运行多个作业的示例,您可以在元素中配置时区time-zone
:
<schedule>
<job>
<name>JobA</name>
<group>GroupDummy</group>
<description>This is Job A</description>
<job-class>com.mkyong.quartz.JobA</job-class>
</job>
<trigger>
<cron>
<name>dummyTriggerNameA</name>
<job-name>JobA</job-name>
<job-group>GroupDummy</job-group>
<!-- It will run every 5 seconds -->
<cron-expression>0/5 * * * * ?</cron-expression>
<time-zone>UTC</time-zone>
</cron>
</trigger>
</schedule>
See also Java's java.util.TimeZonefor to see the ID for several timezones.
另请参阅Java 的 java.util.TimeZone以查看多个时区的 ID。
回答by eadjei
Quartz by default will use the default system locale and timezone, and it is not programed to pick up the property user.timezoneyou are providing your app. Remember also that this is only applies to a CronTrigger and not a SimpleTrigger.
默认情况下,Quartz 将使用默认的系统区域设置和时区,并且它没有被编程为获取您为应用程序提供的属性user.timezone。还要记住,这仅适用于 CronTrigger 而不适用于 SimpleTrigger。
If you are using Spring for example:
例如,如果您使用的是 Spring:
<bean id="timeZone" class="java.util.TimeZone" factory-method="getTimeZone">
<constructor-arg value="GMT" />
</bean>
<bean id="yourTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="yourJob" />
<property name="cronExpression" value="0 0 0/1 * * ?" />
<property name="timeZone" ref="timeZone" />
</bean>
If you are using plain java:
如果您使用的是普通 Java:
Trigger yourTrigger = TriggerBuilder
.newTrigger()
.withIdentity("TRIGGER-ID", "TRIGGER-GROUP")
.withSchedule(CronScheduleBuilder
.cronSchedule("0 0 0/1 * * ?")
.inTimeZone(TimeZone.getTimeZone("GMT")))
).build();