Search in sources :

Example 16 with DateTime

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());
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 17 with DateTime

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());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 18 with DateTime

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, "内蒙古");
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 19 with DateTime

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[] {});
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 20 with DateTime

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