我的服务器使用欧洲/罗马时区 -这也是服务器上的默认时区-,我需要根据用户的时区安排任务。因此,如果生活在夏威夷时间(Pacific/Honolulu)时区的用户为他所在地球的区域安排每天22:00pm触发的CronTrigger,我已找到了以下解决方案:
CronTrigger trigger = newTrigger()
.withIdentity("name", "group")
.withSchedule(
cronSchedule("0 0 22 ? * *").inTimeZone(TimeZone.getTimeZone("Pacific/Honolulu"))
)
.startNow()
.build();
在我的服务器上,这个任务从“我的”下一天的上午09:00开始。
除了保持时区更新(即时区更新工具)之外,还需要考虑特定的问题吗?
如果我想为上一个任务定义.startAt()和.endAt(),这种日期格式可以吗?使用这种方法是否安全考虑到可能存在的夏令时?
Calendar calTZStarts = new GregorianCalendar(TimeZone.getTimeZone("Pacific/Honolulu"));
calTZStarts.set(2013, Calendar.JANUARY, 10);
Calendar calTZEnds = new GregorianCalendar(TimeZone.getTimeZone("Pacific/Honolulu"));
calTZEnds.set(2013, Calendar.JANUARY, 30);
Calendar calStarts = Calendar.getInstance();
calStarts.set(Calendar.YEAR, calTZStarts.get(Calendar.YEAR));
calStarts.set(Calendar.MONTH, calTZStarts.get(Calendar.MONTH));
calStarts.set(Calendar.DAY_OF_MONTH, calTZStarts.get(Calendar.DAY_OF_MONTH));
calStarts.set(Calendar.HOUR_OF_DAY, calTZStarts.get(Calendar.HOUR_OF_DAY));
calStarts.set(Calendar.MINUTE, calTZStarts.get(Calendar.MINUTE));
calStarts.set(Calendar.SECOND, calTZStarts.get(Calendar.SECOND));
calStarts.set(Calendar.MILLISECOND, calTZStarts.get(Calendar.MILLISECOND));
Calendar calEnds = Calendar.getInstance();
calEnds.set(Calendar.YEAR, calTZEnds.get(Calendar.YEAR));
calEnds.set(Calendar.MONTH, calTZEnds.get(Calendar.MONTH));
calEnds.set(Calendar.DAY_OF_MONTH, calTZEnds.get(Calendar.DAY_OF_MONTH));
calEnds.set(Calendar.HOUR_OF_DAY, calTZEnds.get(Calendar.HOUR_OF_DAY));
calEnds.set(Calendar.MINUTE, calTZEnds.get(Calendar.MINUTE));
calEnds.set(Calendar.SECOND, calTZEnds.get(Calendar.SECOND));
calEnds.set(Calendar.MILLISECOND, calTZEnds.get(Calendar.MILLISECOND));
CronTrigger trigger = newTrigger()
.withIdentity("name", "group")
.withSchedule(
cronSchedule("0 0 22 ? * *").inTimeZone(TimeZone.getTimeZone("Pacific/Honolulu"))
)
.startAt(calStarts.getTime())
.endAt(calEnds.getTime())
.build();
或者我只需要使用以下方式设置开始和结束:
Calendar calTZStarts = new GregorianCalendar();
calTZStarts.set(2013, Calendar.JANUARY, 10, 0, 0, 0);
Calendar calTZEnds = new GregorianCalendar();
calTZEnds.set(2013, Calendar.JANUARY, 30, 0, 0, 0);
CronTrigger trigger = newTrigger()
.withIdentity("name", "group")
.withSchedule(
cronSchedule("0 0 22 ? * *").inTimeZone(TimeZone.getTimeZone("Pacific/Honolulu"))
)
.startAt(calTZStarts.getTime())
.endAt(calTZEnds.getTime())
.build();
那么在“夏威夷时间”定义的日期中,工作是否正确开始/结束?
感谢您提前提供的每一条建议。