2

I have a job written in Java which runs on a specified time every day on that timezone. For example if I started the job for the first time at 0800 hrs it must alwas run the job on the same time everyday. This works fine if the timezone does not follow DST. But it breaks if it follows DST.

For example if I started my job for the first time in 0800 PST, it must always run in 0000 hrs in UTC as long as DST is not in effect but should move to 2300 hrs UTC the previous day if DST is in effect. How do I adjust my job start time programatically for these DST changes.

My input looks like this:

String startdate = "2012-06-16T08:00:00"
String timeZone = "PST";

These will be constants located in a config file, and once set cannot be modified. The input will PST regardless of whether DST is in effect or not.

So if the current timezone is in PST it will run on 0000 hrs UTC 2012-06-16 or else it will run on 2300 hrs UTC 2012-06-15.

How do I achieve this.

Note: This is not a homework question and I come from a region that does not follow DST, so it is a bit confusing for me.

Thanks in advance.

4

1 回答 1

0

我不知道你是如何安排你的工作的,但如果你手动计算下一个执行日期,你可以使用java.util.Calendar如下:

TimeZone tz = TimeZone.getTimeZone( timeZone );
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss" );
dateFormat.setTimeZone( tz );
Calendar cal = Calendar.getInstance();
cal.setTimeZone( tz );
cal.setTime( dateFormat.parse( startdate ) );

然后在每次执行作业后,您可以计算作业的下一次运行时间:

cal.add( Calendar.DAY_OF_YEAR, 1 );

这增加了一个日历日,而不是 24 小时。因此,如果开始日期是当地时间上午 8 点,那么即使在 DST 期间,它将继续在当地时间上午 8 点运行。日历将为您处理 DST 转换。

于 2012-06-17T11:27:00.383 回答