Peter Penzov
Peter Penzov

Reputation: 1658

Incorrect configuration for date

I use this code to execute Job in fixes time.

private static final ZoneId zone = ZoneId.systemDefault();
private static final ZonedDateTime zonedDateTime = ZonedDateTime.now(zone);

@Scheduled(cron = "0 0 0 * * *")
public void PaymentTransactionsDailyFactsScheduler() throws Exception { 
    .......

    DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy  hh:mm a");
    String time = zonedDateTime.format(format);

    System.out.printf("Job Scheduler executed (%s, (%s))\n", time, zone);
}

But every time I get Job Scheduler executed (Apr 13 2019 03:41 PM, (Europe/Germany))

Do you know why the time is every time exactly the same? Probably I need to remove final?

Upvotes: 0

Views: 51

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

You create a single ZonedDateTime object, once and only once, when your class is being initialized.

If you want the current time of the execution of the method, create it inside the method.

Upvotes: 1

Related Questions