use of cn.hutool.core.date.DateTime in project hutool by looly.
the class DateUtilTest method parseTest.
@Test
public void parseTest() throws ParseException {
// 转换时间与SimpleDateFormat结果保持一致即可
String time = "12:11:39";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
DateTime parse = DateUtil.parse("12:11:39");
Assert.assertEquals(format.parse(time).getTime(), parse.getTime());
}
use of cn.hutool.core.date.DateTime in project hutool by looly.
the class ConvertToNumberTest method dateToLongTest.
@Test
public void dateToLongTest() {
final DateTime date = DateUtil.parse("2020-05-17 12:32:00");
final Long dateLong = Convert.toLong(date);
assert date != null;
Assert.assertEquals(date.getTime(), dateLong.longValue());
}
use of cn.hutool.core.date.DateTime in project benchmark by seelunzi.
the class HutoolTest method name.
@Test
public void name() throws Exception {
String ID_18 = "321083197812162119";
String ID_15 = "150102880730303";
// 是否有效
boolean valid = IdcardUtil.isValidCard(ID_18);
boolean valid15 = IdcardUtil.isValidCard(ID_15);
// 转换
String convert15To18 = IdcardUtil.convert15To18(ID_15);
Assert.assertEquals(convert15To18, "150102198807303035");
// 年龄
DateTime date = DateUtil.parse("2017-04-10");
int age = IdcardUtil.getAgeByIdCard(ID_18, date);
Assert.assertEquals(age, 38);
int age2 = IdcardUtil.getAgeByIdCard(ID_15, date);
Assert.assertEquals(age2, 28);
// 生日
String birth = IdcardUtil.getBirthByIdCard(ID_18);
Assert.assertEquals(birth, "19781216");
String birth2 = IdcardUtil.getBirthByIdCard(ID_15);
Assert.assertEquals(birth2, "19880730");
// 省份
String province = IdcardUtil.getProvinceByIdCard(ID_18);
Assert.assertEquals(province, "江苏");
String province2 = IdcardUtil.getProvinceByIdCard(ID_15);
Assert.assertEquals(province2, "内蒙古");
}
use of cn.hutool.core.date.DateTime in project hutool by looly.
the class RangeTest method dateRangeFuncTest.
@Test
public void dateRangeFuncTest() {
DateTime start = DateUtil.parse("2021-01-01");
DateTime end = DateUtil.parse("2021-01-03");
List<Integer> dayOfMonthList = DateUtil.rangeFunc(start, end, DateField.DAY_OF_YEAR, a -> DateTime.of(a).dayOfMonth());
Assert.assertArrayEquals(dayOfMonthList.toArray(new Integer[] {}), new Integer[] { 1, 2, 3 });
List<Integer> dayOfMonthList2 = DateUtil.rangeFunc(null, null, DateField.DAY_OF_YEAR, a -> DateTime.of(a).dayOfMonth());
Assert.assertArrayEquals(dayOfMonthList2.toArray(new Integer[] {}), new Integer[] {});
}
use of cn.hutool.core.date.DateTime in project hutool by looly.
the class RangeTest method dateRangeTest2.
@Test
public void dateRangeTest2() {
DateTime start = DateUtil.parse("2021-01-31");
DateTime end = DateUtil.parse("2021-03-31");
final DateRange range = DateUtil.range(start, end, DateField.MONTH);
Assert.assertTrue(range.hasNext());
Assert.assertEquals(DateUtil.parse("2021-01-31"), range.next());
Assert.assertTrue(range.hasNext());
Assert.assertEquals(DateUtil.parse("2021-02-28"), range.next());
Assert.assertTrue(range.hasNext());
Assert.assertEquals(DateUtil.parse("2021-03-31"), range.next());
Assert.assertFalse(range.hasNext());
}
Aggregations