mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add method
This commit is contained in:
@@ -62,10 +62,19 @@ public class DateUtil extends CalendarUtil {
|
||||
*
|
||||
* @return 当前时间
|
||||
*/
|
||||
public static DateTime date() {
|
||||
public static DateTime now() {
|
||||
return new DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当天开始的时间,结果类似:2022-10-26 00:00:00
|
||||
*
|
||||
* @return 当天开始的时间
|
||||
*/
|
||||
public static DateTime today() {
|
||||
return new DateTime(beginOfDay(Calendar.getInstance()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前时间,转换为{@link DateTime}对象,忽略毫秒部分
|
||||
*
|
||||
@@ -73,7 +82,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static DateTime dateSecond() {
|
||||
return beginOfSecond(date());
|
||||
return beginOfSecond(now());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -393,56 +402,56 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return 今年
|
||||
*/
|
||||
public static int thisYear() {
|
||||
return year(date());
|
||||
return year(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前月份
|
||||
*/
|
||||
public static int thisMonth() {
|
||||
return month(date());
|
||||
return month(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前月份 {@link Month}
|
||||
*/
|
||||
public static Month thisMonthEnum() {
|
||||
return monthEnum(date());
|
||||
return monthEnum(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期所在年份的第几周
|
||||
*/
|
||||
public static int thisWeekOfYear() {
|
||||
return weekOfYear(date());
|
||||
return weekOfYear(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期所在月份的第几周
|
||||
*/
|
||||
public static int thisWeekOfMonth() {
|
||||
return weekOfMonth(date());
|
||||
return weekOfMonth(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期是这个日期所在月份的第几天
|
||||
*/
|
||||
public static int thisDayOfMonth() {
|
||||
return dayOfMonth(date());
|
||||
return dayOfMonth(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期是星期几
|
||||
*/
|
||||
public static int thisDayOfWeek() {
|
||||
return dayOfWeek(date());
|
||||
return dayOfWeek(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期是星期几 {@link Week}
|
||||
*/
|
||||
public static Week thisDayOfWeekEnum() {
|
||||
return dayOfWeekEnum(date());
|
||||
return dayOfWeekEnum(now());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -450,28 +459,28 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return 当前日期的小时数部分<br>
|
||||
*/
|
||||
public static int thisHour(final boolean is24HourClock) {
|
||||
return hour(date(), is24HourClock);
|
||||
return hour(now(), is24HourClock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期的分钟数部分<br>
|
||||
*/
|
||||
public static int thisMinute() {
|
||||
return minute(date());
|
||||
return minute(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期的秒数部分<br>
|
||||
*/
|
||||
public static int thisSecond() {
|
||||
return second(date());
|
||||
return second(now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 当前日期的毫秒数部分<br>
|
||||
*/
|
||||
public static int thisMillisecond() {
|
||||
return millisecond(date());
|
||||
return millisecond(now());
|
||||
}
|
||||
// -------------------------------------------------------------- Part of Date end
|
||||
|
||||
@@ -1408,7 +1417,7 @@ public class DateUtil extends CalendarUtil {
|
||||
|
||||
final long thisMills = date.getTime();
|
||||
final long beginMills = beginDate.getTime();
|
||||
final long endMills = endDate.getTime();
|
||||
final long endMills = endDate.getTime();
|
||||
final long rangeMin = Math.min(beginMills, endMills);
|
||||
final long rangeMax = Math.max(beginMills, endMills);
|
||||
|
||||
@@ -1584,7 +1593,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return 年龄
|
||||
*/
|
||||
public static int ageOfNow(final Date birthDay) {
|
||||
return age(birthDay, date());
|
||||
return age(birthDay, now());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1607,7 +1616,7 @@ public class DateUtil extends CalendarUtil {
|
||||
public static int age(final Date birthday, Date dateToCompare) {
|
||||
Assert.notNull(birthday, "Birthday can not be null !");
|
||||
if (null == dateToCompare) {
|
||||
dateToCompare = date();
|
||||
dateToCompare = now();
|
||||
}
|
||||
return age(birthday.getTime(), dateToCompare.getTime());
|
||||
}
|
||||
@@ -2006,21 +2015,23 @@ public class DateUtil extends CalendarUtil {
|
||||
|
||||
/**
|
||||
* 是否为本月最后一天
|
||||
*
|
||||
* @param date {@link Date}
|
||||
* @return 是否为本月最后一天
|
||||
* @since 5.8.8
|
||||
*/
|
||||
public static boolean isLastDayOfMonth(final Date date){
|
||||
public static boolean isLastDayOfMonth(final Date date) {
|
||||
return date(date).isLastDayOfMonth();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得本月的最后一天
|
||||
*
|
||||
* @param date {@link Date}
|
||||
* @return 天
|
||||
* @since 5.8.8
|
||||
*/
|
||||
public static int getLastDayOfMonth(final Date date){
|
||||
public static int getLastDayOfMonth(final Date date) {
|
||||
return date(date).getLastDayOfMonth();
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.regex.ReUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Duration;
|
||||
@@ -22,6 +22,7 @@ import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.Date;
|
||||
@@ -35,7 +36,7 @@ import java.util.TimeZone;
|
||||
* @see DatePattern 常用格式工具类
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class TimeUtil extends TemporalAccessorUtil{
|
||||
public class TimeUtil extends TemporalAccessorUtil {
|
||||
|
||||
/**
|
||||
* 当前时间,默认时区
|
||||
@@ -171,8 +172,8 @@ public class TimeUtil extends TemporalAccessorUtil{
|
||||
return ((LocalDate) temporalAccessor).atStartOfDay();
|
||||
} else if (temporalAccessor instanceof Instant) {
|
||||
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
||||
} else if(temporalAccessor instanceof ZonedDateTime){
|
||||
return ((ZonedDateTime)temporalAccessor).toLocalDateTime();
|
||||
} else if (temporalAccessor instanceof ZonedDateTime) {
|
||||
return ((ZonedDateTime) temporalAccessor).toLocalDateTime();
|
||||
}
|
||||
|
||||
return LocalDateTime.of(
|
||||
@@ -224,7 +225,7 @@ public class TimeUtil extends TemporalAccessorUtil{
|
||||
public static LocalDateTime parseByISO(final CharSequence text) {
|
||||
if (StrUtil.contains(text, 'T')) {
|
||||
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}else{
|
||||
} else {
|
||||
return parse(text, DatePattern.NORM_DATETIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
@@ -448,6 +449,16 @@ public class TimeUtil extends TemporalAccessorUtil{
|
||||
return time.with(LocalTime.MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取给定日期月底的时间
|
||||
*
|
||||
* @param time 日期时间
|
||||
* @return 月底
|
||||
*/
|
||||
public static LocalDateTime endOfMonth(final LocalDateTime time) {
|
||||
return time.with(TemporalAdjusters.lastDayOfMonth());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为周末(周六或周日)
|
||||
*
|
||||
|
@@ -475,7 +475,7 @@ public class IdcardUtil {
|
||||
* @return 年龄
|
||||
*/
|
||||
public static int getAge(final String idcard) {
|
||||
return getAge(idcard, DateUtil.date());
|
||||
return getAge(idcard, DateUtil.now());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -652,7 +652,7 @@ public class RandomUtil {
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static DateTime randomDay(final int min, final int max) {
|
||||
return randomDate(DateUtil.date(), DateField.DAY_OF_YEAR, min, max);
|
||||
return randomDate(DateUtil.now(), DateField.DAY_OF_YEAR, min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -667,7 +667,7 @@ public class RandomUtil {
|
||||
*/
|
||||
public static DateTime randomDate(Date baseDate, final DateField dateField, final int min, final int max) {
|
||||
if (null == baseDate) {
|
||||
baseDate = DateUtil.date();
|
||||
baseDate = DateUtil.now();
|
||||
}
|
||||
|
||||
return DateUtil.offset(baseDate, dateField, randomInt(min, max));
|
||||
|
@@ -17,7 +17,7 @@ public class DateConvertTest {
|
||||
final Date value = Convert.toDate(a);
|
||||
Assert.assertEquals(a, DateUtil.formatDate(value));
|
||||
|
||||
final long timeLong = DateUtil.date().getTime();
|
||||
final long timeLong = DateUtil.now().getTime();
|
||||
final Date value2 = Convert.toDate(timeLong);
|
||||
Assert.assertEquals(timeLong, value2.getTime());
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public class DateConvertTest {
|
||||
final java.sql.Date value = Convert.convert(java.sql.Date.class, a);
|
||||
Assert.assertEquals("2017-05-06", value.toString());
|
||||
|
||||
final long timeLong = DateUtil.date().getTime();
|
||||
final long timeLong = DateUtil.now().getTime();
|
||||
final java.sql.Date value2 = Convert.convert(java.sql.Date.class, timeLong);
|
||||
Assert.assertEquals(timeLong, value2.getTime());
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ public class DateUtilTest {
|
||||
@Test
|
||||
public void nowTest() {
|
||||
// 当前时间
|
||||
final Date date = DateUtil.date();
|
||||
final Date date = DateUtil.now();
|
||||
Assert.assertNotNull(date);
|
||||
// 当前时间
|
||||
final Date date2 = DateUtil.date(Calendar.getInstance());
|
||||
@@ -56,6 +56,12 @@ public class DateUtilTest {
|
||||
Assert.assertNotNull(today);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void todayTest() {
|
||||
final String s = DateUtil.today().toString();
|
||||
Assert.assertTrue(s.endsWith("00:00:00"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatAndParseTest() {
|
||||
final String dateStr = "2017-03-01";
|
||||
@@ -737,7 +743,7 @@ public class DateUtilTest {
|
||||
|
||||
@Test
|
||||
public void endOfYearTest() {
|
||||
final DateTime date = DateUtil.date();
|
||||
final DateTime date = DateUtil.now();
|
||||
date.setField(DateField.YEAR, 2019);
|
||||
final DateTime endOfYear = DateUtil.endOfYear(date);
|
||||
Assert.assertEquals("2019-12-31 23:59:59", endOfYear.toString());
|
||||
|
@@ -229,7 +229,7 @@ public class UrlBuilderTest {
|
||||
@Test
|
||||
public void endWithSlashTest() {
|
||||
// 原URL中以/结尾,则这个规则需保留,issue#I1G44J@Gitee
|
||||
final String today = DateUtil.date().toString("yyyyMMdd");
|
||||
final String today = DateUtil.now().toString("yyyyMMdd");
|
||||
final String getWorkDayUrl = "https://tool.bitefu.net/jiari/?info=1&d=" + today;
|
||||
final UrlBuilder builder = UrlBuilder.ofHttp(getWorkDayUrl, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals(getWorkDayUrl, builder.toString());
|
||||
|
Reference in New Issue
Block a user