Search in sources :

Example 1 with DateTime

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

the class IdcardUtil method isvalidCard15.

/**
 * 验证15位身份编码是否合法
 *
 * @param idCard 身份编码
 * @return 是否合法
 */
public static boolean isvalidCard15(String idCard) {
    if (CHINA_ID_MIN_LENGTH != idCard.length()) {
        return false;
    }
    if (Validator.isNumber(idCard)) {
        // 省份
        String proCode = idCard.substring(0, 2);
        if (null == cityCodes.get(proCode)) {
            return false;
        }
        // 生日
        DateTime birthDate = DateUtil.parse(idCard.substring(6, 12), "yyMMdd");
        if (false == Validator.isBirthday(birthDate.year(), birthDate.month(), birthDate.dayOfMonth())) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}
Also used : DateTime(cn.hutool.core.date.DateTime)

Example 2 with DateTime

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

the class DateTimeTest method mutableTest.

@Test
public void mutableTest() {
    DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 默认情况下DateTime为可变对象
    DateTime offsite = dateTime.offset(DateField.YEAR, 0);
    Assert.assertTrue(offsite == dateTime);
    // 设置为不可变对象后变动将返回新对象
    dateTime.setMutable(false);
    offsite = dateTime.offset(DateField.YEAR, 0);
    Assert.assertFalse(offsite == dateTime);
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 3 with DateTime

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

the class DateTimeTest method datetimeTest.

@Test
public void datetimeTest() {
    DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 年
    int year = dateTime.year();
    Assert.assertEquals(2017, year);
    // 季度(非季节)
    Season season = dateTime.seasonEnum();
    Assert.assertEquals(Season.SPRING, season);
    // 月份
    Month month = dateTime.monthEnum();
    Assert.assertEquals(Month.JANUARY, month);
    // 日
    int day = dateTime.dayOfMonth();
    Assert.assertEquals(5, day);
}
Also used : Month(cn.hutool.core.date.Month) Season(cn.hutool.core.date.Season) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 4 with DateTime

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

the class TemporalAccessorConverter method parseFromCharSequence.

/**
 * 通过反射从字符串转java.time中的对象
 *
 * @param value 字符串值
 * @return 日期对象
 */
private TemporalAccessor parseFromCharSequence(CharSequence value) {
    if (StrUtil.isBlank(value)) {
        return null;
    }
    final Instant instant;
    ZoneId zoneId;
    if (null != this.format) {
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(this.format);
        instant = formatter.parse(value, Instant::from);
        zoneId = formatter.getZone();
    } else {
        final DateTime dateTime = DateUtil.parse(value);
        instant = Objects.requireNonNull(dateTime).toInstant();
        zoneId = dateTime.getZoneId();
    }
    return parseFromInstant(instant, zoneId);
}
Also used : ZoneId(java.time.ZoneId) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) OffsetDateTime(java.time.OffsetDateTime) DateTime(cn.hutool.core.date.DateTime)

Example 5 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)

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