|
| 1 | +/* |
| 2 | + * Copyright 2015 jmrozanec |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.cronutils; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +import java.text.ParseException; |
| 19 | +import java.time.ZoneId; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.time.format.DateTimeFormatter; |
| 22 | +import java.util.Date; |
| 23 | +import java.util.TimeZone; |
| 24 | + |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import com.cronutils.model.Cron; |
| 29 | +import com.cronutils.model.CronType; |
| 30 | +import com.cronutils.model.definition.CronDefinitionBuilder; |
| 31 | +import com.cronutils.parser.CronParser; |
| 32 | + |
| 33 | +/** |
| 34 | + * Provide an example on how convert a cron expression to ISO8601 |
| 35 | + */ |
| 36 | +public class Issue367Test { |
| 37 | + |
| 38 | + private CronParser parser; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setUp() { |
| 42 | + parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testCase1() throws ParseException { |
| 47 | + // set base data |
| 48 | + final ZoneId zone = ZoneId.of("Europe/Berlin"); |
| 49 | + final Date date = new Date(); |
| 50 | + final String cronExpression = "0 0 1 1 1 ?"; |
| 51 | + // build cron |
| 52 | + Cron cron = parser.parse(cronExpression); |
| 53 | + // convert to quartz |
| 54 | + final org.quartz.CronExpression quartzExpression = new org.quartz.CronExpression(cron.asString()); |
| 55 | + quartzExpression.setTimeZone(TimeZone.getTimeZone(zone)); |
| 56 | + // get date and convert to ISO8601 |
| 57 | + final Date quartzNextTime = quartzExpression.getNextValidTimeAfter(Date.from(date.toInstant()));// 2016-12-24T00:00:00Z |
| 58 | + ZonedDateTime d = ZonedDateTime.ofInstant(quartzNextTime.toInstant(), zone); |
| 59 | + String res = DateTimeFormatter.ISO_DATE_TIME.format(d); |
| 60 | + assertTrue(res.equals("2021-01-01T01:00:00+01:00[Europe/Berlin]")); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments