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;
}
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);
}
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);
}
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);
}
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());
}
Aggregations