spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.63k stars 38.14k forks source link

I need a little help with use of CronExpression. #28707

Closed LGtaer closed 2 years ago

LGtaer commented 2 years ago

https://github.com/spring-projects/spring-framework/blob/730d6c95fcb8352d1d81079904deeb6518e90454/spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java#L155

I need to manually generate CronExpression,ex. "0 /10 ?" What I want is the correct answer for the time expression. But the time I got min was always "0,10,20,30..." because the baseline time is not included in the ValueRange construction.I have a headache and need help. ty

poutsma commented 2 years ago

I am not sure what the question is. The expression "0 */10 * * * ? means every 10 minutes starting at minute 00, i.e. 0,10,20,30,40,50.

Can you make clear what the desired behaviour is, by providing a complete minimal sample (something that we can unzip or git clone, build, and deploy). Just a JUnit test would be fine.

LGtaer commented 2 years ago

I am not sure what the question is. The expression "0 */10 * * * ? means every 10 minutes starting at minute 00, i.e. 0,10,20,30,40,50.

Can you make clear what the desired behaviour is, by providing a complete minimal sample (something that we can unzip or git clone, build, and deploy). Just a JUnit test would be fine.

ex.

@Test
public void testCron() {
    CronExpression expression = CronExpression.parse("0 */10 * * * ?");
    Instant timeFrom = Instant.ofEpochMilli(1656288300000L);
    System.out.println("begin time: " + ZonedDateTime.ofInstant(timeFrom, ZoneId.of("GMT")));
    for (int i = 1; i < 6; i++) {
        ZonedDateTime nextFirst = expression.next(ZonedDateTime.ofInstant(timeFrom, ZoneId.of("GMT")));
        timeFrom = nextFirst.toInstant();
        System.out.println(nextFirst.getMinute());
        Assertions.assertEquals(5 + 10 * i, nextFirst.getMinute());
    }
}

Expected :15,25,35,45,55 Actual :10,20,30,40,50

poutsma commented 2 years ago

Expected :15,25,35,45,55

I am afraid your expectations are incorrect. The expression 0 */10 * * * ? means every 10 minutes starting at minute 00. It does not mean every 10 minutes starting at the given time.

Feel free to verify this entering the expression into https://www.freeformatter.com/cron-expression-generator-quartz.html or https://beautifycode.net/cron-expression-descriptor.

LGtaer commented 2 years ago

Expected :15,25,35,45,55

I am afraid your expectations are incorrect. The expression 0 */10 * * * ? means every 10 minutes starting at minute 00. It does not mean every 10 minutes starting at the given time.

Feel free to verify this entering the expression into https://www.freeformatter.com/cron-expression-generator-quartz.html or https://beautifycode.net/cron-expression-descriptor.

but when i use @Scheduled(cron ="0 /10 ?) , the execution time was the same as I expected. system not starting at minute 00 I want to manually implement this function. Because I'm making a custom timer.