This commit is contained in:
Looly 2020-05-30 07:51:45 +08:00
parent 7d97896c00
commit e7bc77aff5
3 changed files with 31 additions and 10 deletions

View File

@ -454,6 +454,16 @@ public class DateTime extends Date {
return getField(DateField.DAY_OF_MONTH); return getField(DateField.DAY_OF_MONTH);
} }
/**
* 获得指定日期是这个日期所在年份的第几天<br>
*
* @return
* @since 5.3.6
*/
public int dayOfYear() {
return getField(DateField.DAY_OF_YEAR);
}
/** /**
* 获得指定日期是星期几1表示周日2表示周一 * 获得指定日期是星期几1表示周日2表示周一
* *

View File

@ -20,6 +20,7 @@ import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.OffsetTime; import java.time.OffsetTime;
import java.time.Year;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -259,7 +260,7 @@ public class DateUtil extends CalendarUtil {
* @since 5.3.6 * @since 5.3.6
*/ */
public static int dayOfYear(Date date) { public static int dayOfYear(Date date) {
return DateTime.of(date).getField(DateField.DAY_OF_YEAR); return DateTime.of(date).dayOfYear();
} }
/** /**
@ -1863,6 +1864,17 @@ public class DateUtil extends CalendarUtil {
return LocalDateTime.ofInstant(dateTime.toInstant(), dateTime.getZoneId()); return LocalDateTime.ofInstant(dateTime.toInstant(), dateTime.getZoneId());
} }
/**
* 获得指定年份的总天数
*
* @param year 年份
* @return
* @since 5.3.6
*/
public static int lengthOfYear(int year) {
return Year.of(year).length();
}
// ------------------------------------------------------------------------ Private method start // ------------------------------------------------------------------------ Private method start
/** /**

View File

@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.BetweenFormater.Level; import cn.hutool.core.date.BetweenFormater.Level;
import cn.hutool.core.date.format.FastDateFormat; import cn.hutool.core.date.format.FastDateFormat;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -380,14 +379,6 @@ public class DateUtilTest {
Assert.assertEquals("2019-06-01 19:45:43", dateTime.toString()); Assert.assertEquals("2019-06-01 19:45:43", dateTime.toString());
} }
@Test
@Ignore
public void parseTest8() {
String str = "2020-04-24 9:00:00";
DateTime dateTime = DateUtil.parse(str);
Assert.assertEquals("2019-06-01 19:45:43", dateTime.toString());
}
@Test @Test
public void parseAndOffsetTest() { public void parseAndOffsetTest() {
// 检查UTC时间偏移是否准确 // 检查UTC时间偏移是否准确
@ -762,4 +753,12 @@ public class DateUtilTest {
final long weekCount = DateUtil.betweenWeek(start, end, true); final long weekCount = DateUtil.betweenWeek(start, end, true);
Assert.assertEquals(30L, weekCount); Assert.assertEquals(30L, weekCount);
} }
@Test
public void dayOfYearTest() {
int dayOfYear = DateUtil.dayOfYear(DateUtil.parse("2020-01-01"));
Assert.assertEquals(1, dayOfYear);
int lengthOfYear = DateUtil.lengthOfYear(2020);
Assert.assertEquals(366, lengthOfYear);
}
} }