Search in sources :

Example 6 with DateTime

use of cn.hutool.core.date.DateTime in project hutool by looly.

the class RangeTest method rangeByStepTest.

@Test
public void rangeByStepTest() {
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-03");
    // 测试包含开始和结束情况下步进为1的情况
    DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
    Assert.assertEquals(range.next(), DateUtil.parse("2017-01-01"));
    Assert.assertEquals(range.next(), DateUtil.parse("2017-01-02"));
    Assert.assertEquals(range.next(), DateUtil.parse("2017-01-03"));
    try {
        range.next();
        Assert.fail("已超过边界,下一个元素不应该存在!");
    } catch (NoSuchElementException ignored) {
    }
    // 测试多步进的情况
    range = new DateRange(start, end, DateField.DAY_OF_YEAR, 2);
    Assert.assertEquals(DateUtil.parse("2017-01-01"), range.next());
    Assert.assertEquals(DateUtil.parse("2017-01-03"), range.next());
}
Also used : DateRange(cn.hutool.core.date.DateRange) DateTime(cn.hutool.core.date.DateTime) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 7 with DateTime

use of cn.hutool.core.date.DateTime in project hutool by looly.

the class RangeTest method dateRangeConsumeTest.

@Test
public void dateRangeConsumeTest() {
    DateTime start = DateUtil.parse("2021-01-01");
    DateTime end = DateUtil.parse("2021-01-03");
    StringBuilder sb = new StringBuilder();
    DateUtil.rangeConsume(start, end, DateField.DAY_OF_YEAR, a -> sb.append(DateTime.of(a).dayOfMonth()).append("#"));
    Assert.assertEquals(sb.toString(), "1#2#3#");
    StringBuilder sb2 = new StringBuilder();
    DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb2.append(DateTime.of(a).dayOfMonth()).append("#"));
    Assert.assertEquals(sb2.toString(), StrUtil.EMPTY);
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 8 with DateTime

use of cn.hutool.core.date.DateTime in project hutool by looly.

the class RangeTest method rangeDayOfYearTest.

@Test
public void rangeDayOfYearTest() {
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-05");
    // 测试不包含开始结束时间的情况
    DateRange range = new DateRange(start, end, DateField.DAY_OF_YEAR, 1, false, false);
    Assert.assertEquals(DateUtil.parse("2017-01-02"), range.next());
    Assert.assertEquals(DateUtil.parse("2017-01-03"), range.next());
    Assert.assertEquals(DateUtil.parse("2017-01-04"), range.next());
    try {
        range.next();
        Assert.fail("不包含结束时间情况下,下一个元素不应该存在!");
    } catch (NoSuchElementException ignored) {
    }
}
Also used : DateRange(cn.hutool.core.date.DateRange) DateTime(cn.hutool.core.date.DateTime) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 9 with DateTime

use of cn.hutool.core.date.DateTime in project hutool by looly.

the class RangeTest method dateRangeTest.

@Test
public void dateRangeTest() {
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-02");
    final Range<DateTime> range = new Range<>(start, end, (current, end1, index) -> {
        if (current.isAfterOrEquals(end1)) {
            return null;
        }
        return current.offsetNew(DateField.DAY_OF_YEAR, 1);
    });
    Assert.assertTrue(range.hasNext());
    Assert.assertEquals(DateUtil.parse("2017-01-01"), range.next());
    Assert.assertTrue(range.hasNext());
    Assert.assertEquals(DateUtil.parse("2017-01-02"), range.next());
    Assert.assertFalse(range.hasNext());
}
Also used : DateRange(cn.hutool.core.date.DateRange) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 10 with DateTime

use of cn.hutool.core.date.DateTime in project hutool by looly.

the class RangeTest method rangeContains.

@Test
public void rangeContains() {
    // 开始区间
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-31");
    DateRange startRange = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
    // 结束区间
    DateTime start1 = DateUtil.parse("2017-01-31");
    DateTime end1 = DateUtil.parse("2017-02-02");
    DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
    // 交集
    List<DateTime> dateTimes = DateUtil.rangeContains(startRange, endRange);
    Assert.assertEquals(1, dateTimes.size());
    Assert.assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0));
}
Also used : DateRange(cn.hutool.core.date.DateRange) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Aggregations

DateTime (cn.hutool.core.date.DateTime)22 Test (org.junit.Test)19 DateRange (cn.hutool.core.date.DateRange)6 Season (cn.hutool.core.date.Season)2 NoSuchElementException (java.util.NoSuchElementException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ConvertException (cn.hutool.core.convert.ConvertException)1 Month (cn.hutool.core.date.Month)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ZoneId (java.time.ZoneId)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 Calendar (java.util.Calendar)1