Convert cron expression from one timezone to another one
20:19 29 Jan 2015

I am looking for a way to convert cron expression from one timezone to another one timezone.

For example, my web-client user is GMT+1200 and my server-side is GMT+0800, while user setting 02:10 to execute task every Tuesday and Thursday, the cron expression will be 0 10 2 3,5 * ?, and I have used code as below, it can get current fire time for user's timezone

CronExpression expr = new CronExpression("0 10 2 3,5 * ?");
        
System.out.println(expr.getNextValidTimeAfter(new Date()));
System.out.println(expr.getTimeZone());
System.out.println(expr.getExpressionSummary());
System.out.println("=======");
TimeZone tz = TimeZone.getTimeZone("GMT+1200");
expr.setTimeZone(tz);
System.out.println(expr.getNextValidTimeAfter(new Date()));
System.out.println(expr.getTimeZone());
System.out.println(expr.getExpressionSummary());

The getNextValidTimeAfter will print Mon Feb 02 22:10:00 CST 2015, which after setTimeZone(tz);, however the getExpreesionSummary or even getCronExpression() will still be 0 10 2 3,5 * ?, where I want to get string will be 0 10 22 2,4 * ? and then I can save into DB for next time fire and also another time-zone user to query setting (of course this will need to convert 0 10 22 2,4 * ? to this user's timezone)

Any help is appreciated

java cron timezone timezone-offset