mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
add methods
This commit is contained in:
parent
3ac71b0a6f
commit
f7ab6b0ccf
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.21 (2022-01-26)
|
||||
# 5.7.21 (2022-01-27)
|
||||
|
||||
### 🐣新特性
|
||||
* 【extra 】 增加jetbrick模板支持
|
||||
@ -12,6 +12,8 @@
|
||||
* 【core 】 新增LocalDateTimeUtil.weekOfYear(issue#I4RWXC@Gitee)
|
||||
* 【core 】 Month增加toJdkMonth、getValueBaseOne
|
||||
* 【core 】 CsvWriter修改规则,去除末尾多余换行符(issue#I4RSQY@Gitee)
|
||||
* 【core 】 DateUtil增加rangeFunc和rangeConsume(issue#I4RSQY@Gitee)
|
||||
* 【core 】 DateTime增加setUseJdkToStringStyle方法
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUG(issue#2112@Github)
|
||||
|
@ -24,13 +24,30 @@ import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 包装java.util.Date
|
||||
* 包装{@link Date}<br>
|
||||
* 此类继承了{@link Date},并提供扩展方法,如时区等。<br>
|
||||
* 此类重写了父类的{@code toString()}方法,返回值为"yyyy-MM-dd HH:mm:ss"格式
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
public class DateTime extends Date {
|
||||
private static final long serialVersionUID = -5395712593979185936L;
|
||||
|
||||
private static boolean useJdkToStringStyle = false;
|
||||
|
||||
/**
|
||||
* 设置全局的,是否使用{@link Date}默认的toString()格式<br>
|
||||
* 如果为{@code true},则调用toString()时返回"EEE MMM dd HH:mm:ss zzz yyyy"格式,<br>
|
||||
* 如果为{@code false},则返回"yyyy-MM-dd HH:mm:ss",<br>
|
||||
* 默认为{@code false}
|
||||
*
|
||||
* @param customUseJdkToStringStyle 是否使用{@link Date}默认的toString()格式
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public static void setUseJdkToStringStyle(boolean customUseJdkToStringStyle){
|
||||
useJdkToStringStyle = customUseJdkToStringStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可变对象
|
||||
*/
|
||||
@ -932,13 +949,18 @@ public class DateTime extends Date {
|
||||
// -------------------------------------------------------------------- toString start
|
||||
|
||||
/**
|
||||
* 转为"yyyy-MM-dd HH:mm:ss" 格式字符串<br>
|
||||
* 如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区
|
||||
* 转为字符串,如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区<br>
|
||||
* 可以调用{@link DateTime#setUseJdkToStringStyle(boolean)} 方法自定义默认的风格<br>
|
||||
* 如果{@link #useJdkToStringStyle}为{@code true},返回"EEE MMM dd HH:mm:ss zzz yyyy"格式,<br>
|
||||
* 如果为{@code false},则返回"yyyy-MM-dd HH:mm:ss"
|
||||
*
|
||||
* @return "yyyy-MM-dd HH:mm:ss" 格式字符串
|
||||
* @return 格式字符串
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
if(useJdkToStringStyle){
|
||||
return super.toString();
|
||||
}
|
||||
return toString(this.timeZone);
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,15 @@ import java.time.LocalDateTime;
|
||||
import java.time.Year;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
@ -1888,24 +1896,20 @@ public class DateUtil extends CalendarUtil {
|
||||
* @param end 结束日期时间
|
||||
* @param unit 步进单位
|
||||
* @param func 每次遍历要执行的 function
|
||||
* @param <T>
|
||||
* @return
|
||||
* @param <T> Date经过函数处理结果类型
|
||||
* @return 结果列表
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public static <T> List<T> rangeFunc(Date start, Date end, final DateField unit, Function<Date, T> func) {
|
||||
if (start == null || end == null || start.after(end)) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
}
|
||||
ArrayList<T> list = new ArrayList<>();
|
||||
DateRange dateRange = range(start, end, unit);
|
||||
while (dateRange.hasNext()) {
|
||||
DateTime next = dateRange.next();
|
||||
Date date = next.toJdkDate();
|
||||
T result = func.apply(date);
|
||||
list.add(result);
|
||||
for(DateTime date : range(start, end, unit)){
|
||||
list.add(func.apply(date));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按日期范围遍历,执行 consumer
|
||||
@ -1914,19 +1918,13 @@ public class DateUtil extends CalendarUtil {
|
||||
* @param end 结束日期时间
|
||||
* @param unit 步进单位
|
||||
* @param consumer 每次遍历要执行的 consumer
|
||||
* @return
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public static void rangeConsume(Date start, Date end, final DateField unit, Consumer<Date> consumer) {
|
||||
if (start == null || end == null || start.after(end)) {
|
||||
return;
|
||||
} else {
|
||||
DateRange dateRange = range(start, end, unit);
|
||||
while (dateRange.hasNext()) {
|
||||
DateTime next = dateRange.next();
|
||||
Date date = next.toJdkDate();
|
||||
consumer.accept(date);
|
||||
}
|
||||
}
|
||||
range(start, end, unit).forEach(consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1937,7 +1935,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @param unit 步进单位
|
||||
* @return {@link DateRange}
|
||||
*/
|
||||
public static List<DateTime> rangeToList(Date start, Date end, final DateField unit) {
|
||||
public static List<DateTime> rangeToList(Date start, Date end, DateField unit) {
|
||||
return CollUtil.newArrayList((Iterable<DateTime>) range(start, end, unit));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user