Search in sources :

Example 21 with DateTime

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

the class RangeTest method rangeToListTest.

@Test
public void rangeToListTest() {
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-31");
    List<DateTime> rangeToList = DateUtil.rangeToList(start, end, DateField.DAY_OF_YEAR);
    Assert.assertEquals(DateUtil.parse("2017-01-01"), rangeToList.get(0));
    Assert.assertEquals(DateUtil.parse("2017-01-02"), rangeToList.get(1));
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 22 with DateTime

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

the class DateConverter method convertInternal.

@Override
protected java.util.Date convertInternal(Object value) {
    if (value == null || (value instanceof CharSequence && StrUtil.isBlank(value.toString()))) {
        return null;
    }
    if (value instanceof TemporalAccessor) {
        return wrap(DateUtil.date((TemporalAccessor) value));
    } else if (value instanceof Calendar) {
        return wrap(DateUtil.date((Calendar) value));
    } else if (value instanceof Number) {
        return wrap(((Number) value).longValue());
    } else {
        // 统一按照字符串处理
        final String valueStr = convertToStr(value);
        final DateTime dateTime = // 
        StrUtil.isBlank(this.format) ? // 
        DateUtil.parse(valueStr) : DateUtil.parse(valueStr, this.format);
        if (null != dateTime) {
            return wrap(dateTime);
        }
    }
    throw new ConvertException("Can not convert {}:[{}] to {}", value.getClass().getName(), value, this.targetType.getName());
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ConvertException(cn.hutool.core.convert.ConvertException) Calendar(java.util.Calendar) DateTime(cn.hutool.core.date.DateTime)

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