-1

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.

Harish
  • 565
  • 1
  • 12
  • 34
  • 2
    Also, you want `ofEpochMilli` not `ofEpochSecond` – g00se Mar 08 '23 at 22:26
  • I just want to point out that `Date.from(Instant.ofEpochSecond(prevRun))` produces `Wed Nov 28 21:03:20 AEDT 55151` ... pretty sure you don't want to wait that long – MadProgrammer Mar 08 '23 at 22:28
  • 3
    Stop worrying about what things like `Date` (you should stop using this) prints by default, it's `toString` method will make use of the current local/time zone, this is what date formatters are for. Care only that the value, if converted to UTC, is correct. – MadProgrammer Mar 08 '23 at 22:29
  • @g00se - Adding ofEpochMilli gives `Tue Jan 20 02:10:58 PST 1970` – Harish Mar 08 '23 at 22:34
  • @MadProgrammer - i ad put correct prevRun Value – Harish Mar 08 '23 at 22:36
  • 2023-03-08T07:01:05Z is what it gives me from prevRun. If you want to operate in UTC, you probably want something like `long prevRun = 1678258865000L; Instant i = Instant.ofEpochMilli(prevRun); ZonedDateTime prevRunZdt = ZonedDateTime.ofInstant(i, ZoneId.of("UTC")); // Now add to prevRunZdt` – g00se Mar 08 '23 at 22:37
  • How do I determine the next cron run? The cron expression using quartz scheduler operates only on Date ? My main problem is determining next run in UTC format. – Harish Mar 08 '23 at 22:41
  • Yes you might have to convert back into to `Date` to use that API – g00se Mar 08 '23 at 22:51
  • `Date.from(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1678258865000L), ZoneId.of("UTC")).toInstant());`. Note `ZonedDateTime.ofInstant` will result in `2023-03-08T07:01:05Z[UTC]` and `Date.from` will result in `Wed Mar 08 18:01:05 AEDT 2023` (for me) which is correct, as AU is +11 hours ahead of UTC - stop worrying about what `Date#toString` prints – MadProgrammer Mar 08 '23 at 23:10
  • In your first example, I don’t see how that long number corresponds to that date-time mathematically; the fractional second does not match. Furthermore, your date-time lacks an indicator of time zone or offset. Is that meant to be in UTC, an offset of zero? – Basil Bourque Mar 09 '23 at 00:33
  • Why don’t you fetch your date-time from the database using `OffsetDateTime` with `ResultSet::getObject` as prescribed by JDBC 4.2+, and as described on many existing Answers on Stack Overflow? – Basil Bourque Mar 09 '23 at 00:37
  • I just took a sample of a long number that was returned from the database. In the database, it's stored in UTC time format. – Harish Mar 09 '23 at 01:12

1 Answers1

-1

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