Search in sources :

Example 11 with DateTime

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

the class RangeTest method rangeNotContains.

@Test
public void rangeNotContains() {
    // 开始区间
    DateTime start = DateUtil.parse("2017-01-01");
    DateTime end = DateUtil.parse("2017-01-30");
    DateRange startRange = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
    // 结束区间
    DateTime start1 = DateUtil.parse("2017-01-01");
    DateTime end1 = DateUtil.parse("2017-01-31");
    DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
    // 差集
    List<DateTime> dateTimes1 = DateUtil.rangeNotContains(startRange, endRange);
    Assert.assertEquals(1, dateTimes1.size());
    Assert.assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0));
}
Also used : DateRange(cn.hutool.core.date.DateRange) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 12 with DateTime

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

the class ConvertToNumberTest method dateToAtomicLongTest.

@Test
public void dateToAtomicLongTest() {
    final DateTime date = DateUtil.parse("2020-05-17 12:32:00");
    final AtomicLong dateLong = Convert.convert(AtomicLong.class, 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 13 with DateTime

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

the class ConvertToNumberTest method dateToIntTest.

@Test
public void dateToIntTest() {
    final DateTime date = DateUtil.parse("2020-05-17 12:32:00");
    final Integer dateInt = Convert.toInt(date);
    assert date != null;
    Assert.assertEquals((int) date.getTime(), dateInt.intValue());
}
Also used : DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 14 with DateTime

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

the class DateTimeTest method seasonTest.

@Test
public void seasonTest() {
    DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 季度(非季节)
    Season season = dateTime.seasonEnum();
    Assert.assertEquals(Season.SPRING, season);
    dateTime = new DateTime("2017-04-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 季度(非季节)
    season = dateTime.seasonEnum();
    Assert.assertEquals(Season.SUMMER, season);
    dateTime = new DateTime("2017-09-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 季度(非季节)
    season = dateTime.seasonEnum();
    Assert.assertEquals(Season.AUTUMN, season);
    dateTime = new DateTime("2017-12-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 季度(非季节)
    season = dateTime.seasonEnum();
    Assert.assertEquals(Season.WINTER, season);
}
Also used : Season(cn.hutool.core.date.Season) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 15 with DateTime

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

the class DateTimeTest method toStringTest.

@Test
public void toStringTest() {
    DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    Assert.assertEquals("2017-01-05 12:34:23", dateTime.toString());
    String dateStr = dateTime.toString("yyyy/MM/dd");
    Assert.assertEquals("2017/01/05", dateStr);
}
Also used : 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