Issue
My database has times in UTC format. When I fetch from the database it comes in a long datatype format. For eg
In DB - 2023-03-08 15:01:05.234081
Corresponding Long - 1678258865
I am having a cron expression and trying to determine the next time to be run for the particular data. For eg, the next run should be at 2023-03-09 15:00:00.234081 UTC for the above data.
long prevRun = 1678258865L;
final CronExpression cronExpression = new CronExpression("0 1 0 * * ?");
cronExpression.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
Date date = Date.from(Instant.ofEpochSecond(prevRun)); => Outputs Tue Mar 07 23:01:05 PST 2023
Date nextRun = cronExpression.getNextValidTimeAfter(prevRun); => Outputs Wed Mar 08 16:01:00 PST 2023
The fourth line always gives in the local system time zone which is why the cron gives this output. Any advice on how I can get a new run in UTC time format for the above cron expression? I am using a cron quartz expression parser for my current use case.
Solution
Good night friend
long prevRun = 1678298465L; // is Wed Mar 08 15:01:05 BRT
final CronExpression cronExpression = new CronExpression("0 1 18 * * ?");
cronExpression.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
Date date = Date.from(Instant.ofEpochSecond(prevRun));
Date nextRun = cronExpression.getNextValidTimeAfter(date);
System.out.println(date); // Wed Mar 08 15:01:05 BRT 2023
System.out.println(nextRun); // Thu Mar 09 15:01:00 BRT 2023
Answered By - Oscar Oliveira Answer Checked By - Dawn Plyler (WPSolving Volunteer)