在 Java 中比较常见的时间表示方法有三种,时间戳、时间类(常用类 LocalDateTime)、格式化日期字符串(常用格式 yyyy-MM-dd HH:mm:ss),这三者可以说是从计算机底层至表层的演变
- 时间戳(timestamp):一般是指从 1970 年 1 月 1 日 00:00:00 UTC 至某个时间点所经过的秒数(10位)或毫秒数(13位)
- 时间类:在 Java 中,诸如比较传统的 Date、Calendar 类,还有 Java 8 后引入的较新的位于 java.time 包的 Instant、Duration、Period、LocalDate、LocalTime、LocalDateTime、ZonedDateTime 等类
- Instant:表示从 1970 年 1 月 1 日 00:00:00 UTC 至今的时间戳
- Duration:表示一段时间的持续时间,以秒和纳秒为单位
- Period:表示日期之间的时间段,以年、月和日为单位
- LocalDate:表示日期,不包含时间和时区信息,如用于表示 2021 年 4 月 26 日
- LocalTime:表示时间,不包含日期和时区信息,如用于表示上午10点30分
- LocalDateTime:表示日期和时间,不包含时区信息,如用于表示 2021 年 4 月 26 日上午 10 点 30 分
- ZonedDateTime:表示具有时区的日期和时间,如用于表示亚洲/上海时间的 2021 年 4 月 26 日上午 10 点 30 分
- 格式化日期字符串:就是日常符合人阅读的时间格式,常用 yyyy-MM-dd HH:mm:ss 表示年月日时分秒,其中 hh 表示十二小时制,HH 表示二十四小时制
以下则是 Java 中各个时间类的常用方法
📑来源
本文中时间类示例代码均来自
https://zhuanlan.zhihu.com/p/440573545
Instant 类
now()
:返回当前时间戳对应的 Instant 对象ofEpochMilli(long epochMilli)
:通过一个毫秒值创建一个 Instant 对象ofEpochSecond(long epochSecond, long nanoAdjustment)
:通过一个秒数和一个纳秒值创建一个 Instant 对象plus(Duration duration)
:在时间戳上加上一个持续时间minus(Duration duration)
:在时间戳上减去一个持续时间isBefore(Instant otherInstant)
:判断一个时间戳是否在另一个时间戳之前isAfter(Instant otherInstant)
:判断一个时间戳是否在另一个时间戳之后
// 时间戳
@Test
public void test01() {
Instant now = Instant.now();
System.out.println("当前时间戳 = " + now);
// 获取从1970年1月1日 00:00:00的秒
System.out.println(now.getNano());
System.out.println(now.getEpochSecond());
System.out.println(now.toEpochMilli());
System.out.println(System.currentTimeMillis());
Instant instant = Instant.ofEpochSecond(5);
System.out.println(instant);
}
Duration 类
between(Temporal startInclusive, Temporal endExclusive)
:计算两个时间点之间的持续时间ofDays(long days)
、ofHours(long hours)
、ofMinutes(long minutes)
、ofSeconds(long seconds)
、ofMillis(long millis)
、ofNanos(long nanos)
:创建一个持续时间对象plus(Duration duration)
:在持续时间上加上另一个持续时间minus(Duration duration)
:在持续时间上减去另一个持续时间toDays()
、toHours()
、toMinutes()
、toSeconds()
、toMillis()
、toNanos()
:获取持续时间对应的天数、小时数、分钟数、秒数、毫秒数、纳秒数
// Duration/Period类: 计算日期时间差
@Test
public void test02() {
// Duration计算时间的距离
LocalTime now = LocalTime.now();
LocalTime time = LocalTime.of(14, 15, 20);
Duration duration = Duration.between(time, now);
System.out.println("相差的天数:" + duration.toDays());
System.out.println("相差的小时数:" + duration.toHours());
System.out.println("相差的分钟数:" + duration.toMinutes());
System.out.println("相差的秒数:" + duration.toSeconds());
// Period计算日期的距离
LocalDate nowDate = LocalDate.now();
LocalDate date = LocalDate.of(1998, 8, 8);
// 让后面的时间减去前面的时间
Period period = Period.between(date, nowDate);
System.out.println("相差的年:" + period.getYears());
System.out.println("相差的月:" + period.getMonths());
System.out.println("相差的天:" + period.getDays());
}
Period 类
between(LocalDate startDateInclusive, LocalDate endDateExclusive)
:计算两个日期之间的时间间隔ofDays(int days)
、ofMonths(int months)
、ofYears(int years)
:创建一个时间间隔对象plus(Period period)
:在时间间隔上加上另一个时间间隔minus(Period period)
:在时间间隔上减去另一个时间间隔getYears()
、getMonths()
、getDays()
:获取时间间隔对应的年数、月数、天数
// Duration/Period类: 计算日期时间差
@Test
public void test02() {
// Duration计算时间的距离
LocalTime now = LocalTime.now();
LocalTime time = LocalTime.of(14, 15, 20);
Duration duration = Duration.between(time, now);
System.out.println("相差的天数:" + duration.toDays());
System.out.println("相差的小时数:" + duration.toHours());
System.out.println("相差的分钟数:" + duration.toMinutes());
System.out.println("相差的秒数:" + duration.toSeconds());
// Period计算日期的距离
LocalDate nowDate = LocalDate.now();
LocalDate date = LocalDate.of(1998, 8, 8);
// 让后面的时间减去前面的时间
Period period = Period.between(date, nowDate);
System.out.println("相差的年:" + period.getYears());
System.out.println("相差的月:" + period.getMonths());
System.out.println("相差的天:" + period.getDays());
}
LocalDate 类
now()
:返回当前日期对应的 LocalDate 对象of(int year, int month, int dayOfMonth)
:通过年、月、日创建一个 LocalDate 对象plusDays(long daysToAdd)
、plusWeeks(long weeksToAdd)
、plusMonths(long monthsToAdd)
、plusYears(long yearsToAdd)
:在日期上加上指定天数、周数、月数、年数minusDays(long daysToSubtract)
、minusWeeks(long weeksToSubtract)
、minusMonths(long monthsToSubtract)
、minusYears(long yearsToSubtract)
:在日期上减去指定天数、周数、月数、年数isBefore(LocalDate other)
、isAfter(LocalDate other)
:判断一个日期是否在另一个日期之前或之后getYear()
、getMonthValue()
、getDayOfMonth()
:获取日期对应的年、月、日
// LocalDate:获取日期的信息。格式为 2019-10-16
@Test
public void test03() {
// 创建指定日期
LocalDate fj = LocalDate.of(1985, 9, 23);
System.out.println("fj = " + fj); // 1985-09-23
// 得到当前日期
LocalDate nowDate = LocalDate.now();
System.out.println("nowDate = " + nowDate); // 2019-10-16
// 获取日期信息
System.out.println("年: " + nowDate.getYear());
System.out.println("月: " + nowDate.getMonthValue());
System.out.println("日: " + nowDate.getDayOfMonth());
System.out.println("星期: " + nowDate.getDayOfWeek());
}
LocalTime 类
now()
:返回当前时间对应的 LocalTime 对象of(int hour, int minute)
、of(int hour, int minute, int second)
、of(int hour, int minute, int second, int nanoOfSecond)
:通过时、分、秒、纳秒创建一个 LocalTime 对象plusHours(long hoursToAdd)
、plusMinutes(long minutesToAdd)
、plusSeconds(long secondsToAdd)
、plusNanos(long nanosToAdd)
:在时间上加上指定小时数、分钟数、秒数、纳秒数minusHours(long hoursToSubtract)
、minusMinutes(long minutesToSubtract)
、minusSeconds(long secondsToSubtract)
、minusNanos(long nanosToSubtract)
:在时间上减去指定小时数、分钟数、秒数、纳秒数isBefore(LocalTime other)
、isAfter(LocalTime other)
:判断一个时间是否在另一个时间之前或之后getHour()
、getMinute()
、getSecond()
、getNano()
:获取时间对应的小时数、分钟数、秒数、纳秒数
// LocalTime类: 获取时间信息。格式为 16:38:54.158549300
@Test
public void test04() {
// 得到指定的时间
LocalTime time = LocalTime.of(12,15, 28, 129_900_000);
System.out.println("time = " + time);
// 得到当前时间
LocalTime nowTime = LocalTime.now();
System.out.println("nowTime = " + nowTime);
// 获取时间信息
System.out.println("小时: " + nowTime.getHour());
System.out.println("分钟: " + nowTime.getMinute());
System.out.println("秒: " + nowTime.getSecond());
System.out.println("纳秒: " + nowTime.getNano());
}
LocalDateTime 类
now()
:返回当前日期时间对应的 LocalDateTime 对象of(int year, int month, int dayOfMonth, int hour, int minute, int second)
:根据指定的年、月、日、时、分、秒创建 LocalDateTime 对象parse(CharSequence text)
:将字符串解析为 LocalDateTime 对象toLocalDate()
、toLocalTime()
:将 LocalDateTime 转换为 LocalDate 对象、LocalTime 对象getYear()
、getMonthValue()
、getDayOfMonth()
:获取日期对应的年、月、日getHour()
、getMinute()
、getSecond()
、getNano()
:获取时间对应的小时数、分钟数、秒数、纳秒数plusDays(long daysToAdd)
、plusWeeks(long weeksToAdd)
、plusMonths(long monthsToAdd)
、plusYears(long yearsToAdd)
:在日期上加上指定天数、周数、月数、年数minusDays(long daysToSubtract)
、minusWeeks(long weeksToSubtract)
、minusMonths(long monthsToSubtract)
、minusYears(long yearsToSubtract)
:在日期上减去指定天数、周数、月数、年数plusHours(long hoursToAdd)
、plusMinutes(long minutesToAdd)
、plusSeconds(long secondsToAdd)
、plusNanos(long nanosToAdd)
:在时间上加上指定小时数、分钟数、秒数、纳秒数minusHours(long hoursToSubtract)
、minusMinutes(long minutesToSubtract)
、minusSeconds(long secondsToSubtract)
、minusNanos(long nanosToSubtract)
:在时间上减去指定小时数、分钟数、秒数、纳秒数
// LocalDateTime类: 获取日期时间信息。格式为 2018-09-06T15:33:56.750
@Test
public void test05() {
LocalDateTime fj = LocalDateTime.of(1985, 9, 23, 9, 10, 20);
System.out.println("fj = " + fj); // 1985-09-23T09:10:20
// 得到当前日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now); // 2019-10-16T16:42:24.497896800
System.out.println(now.getYear());
System.out.println(now.getMonthValue());
System.out.println(now.getDayOfMonth());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
System.out.println(now.getNano());
}
ZonedDateTime 类
now()
:返回当前时区日期时间对应的 ZonedDateTime 对象of(LocalDateTime localDateTime, ZoneId zone)
:根据指定的本地日期时间和时区创建 ZonedDateTime 对象parse(CharSequence text)
:将字符串解析为 ZonedDateTime 对象toLocalDate()
、toLocalTime()
、toLocalDateTime()
:将 ZonedDateTime 转换为 LocalDate 对象、 LocalTime 对象、 LocalDateTime 对象getYear()
、getMonthValue()
、getDayOfMonth()
:获取日期对应的年、月、日getHour()
、getMinute()
、getSecond()
、getNano()
:获取时间对应的小时数、分钟数、秒数、纳秒数plusDays(long daysToAdd)
、plusWeeks(long weeksToAdd)
、plusMonths(long monthsToAdd)
、plusYears(long yearsToAdd)
:在日期上加上指定天数、周数、月数、年数minusDays(long daysToSubtract)
、minusWeeks(long weeksToSubtract)
、minusMonths(long monthsToSubtract)
、minusYears(long yearsToSubtract)
:在日期上减去指定天数、周数、月数、年数plusHours(long hoursToAdd)
、plusMinutes(long minutesToAdd)
、plusSeconds(long secondsToAdd)
、plusNanos(long nanosToAdd)
:在时间上加上指定小时数、分钟数、秒数、纳秒数minusHours(long hoursToSubtract)
、minusMinutes(long minutesToSubtract)
、minusSeconds(long secondsToSubtract)
、minusNanos(long nanosToSubtract)
:在时间上减去指定小时数、分钟数、秒数、纳秒数withZoneSameInstant(ZoneId zone)
:将当前时刻的 ZonedDateTime 转换为指定时区的 ZonedDateTime,时间值不会改变withZoneSameLocal(ZoneId zone)
:将当前日期时间的时区信息改为指定时区,并返回新的 ZonedDateTime 对象
// 设置日期时间的时区
@Test
public void test06() {
// 1.获取所有的时区ID
// ZoneId.getAvailableZoneIds().forEach(System.out::println);
// 不带时间,获取计算机的当前时间
LocalDateTime now = LocalDateTime.now(); // 中国使用的东八区的时区.比标准时间早8个小时
System.out.println("now = " + now);
// 2.操作带时区的类
// now(Clock.systemUTC()): 创建世界标准时间
ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
System.out.println("bz = " + bz);
// now(): 使用计算机的默认的时区,创建日期时间
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println("now1 = " + now1); // 2019-10-
19T16:19:44.007153500+08:00[Asia/Shanghai]
// 使用指定的时区创建日期时间
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Vancouver"));
System.out.println("now2 = " + now2); // 2019-10-19T01:21:44.248794200-
07:00[America/Vancouver]
}
TimeUtils 自定义类
以上方法笔者觉得没有必要再封装进工具类了,直接调用就行
而其他的处理可以参考笔者的时间处理工具类 TimeUtils 的以下方法
- 时间格式转化
toTimestamp()
:转化成时间戳toLocalDateTime()
:转化成 LocalDateTimeformat()
:转化成格式化日期字符串
- 时间获取
getCurrentTimestamp()
:获取当前时间戳(精确到毫秒)getCurrentYearValue()
:获取当前的年(仅数值)getCurrentMonthValue()
:获取当前的月(仅数值)getCurrentDayValue()
:获取当前的日(仅数值)getCurrentYear()
:获取当前的年(格式为 yyyy 的字符串)getCurrentMonth()
:获取当前的年月(格式为 yyyy-MM 的字符串)getCurrentDate()
:获取当前的年月日(格式为 yyyy-MM-dd 的字符串)getCurrentDateTime()
:获取当前的年月日时分秒(格式为 yyyy-MM-dd HH:mm:ss 的字符串)getFirstDayOfMonth
:获取指定年月的当月第一天(格式为 yyyy-MM-dd 的字符串)getLastDayOfMonth
:获取指定年月的当月最后一天(格式为 yyyy-MM-dd 的字符串)getYearsUntilNow
:获取起始日期至今的所有年(格式为 yyyy 的字符串列表)getMonthsUntilNow
:获取起始日期至今的所有年月(格式为 yyyy-MM 的字符串列表)getDaysUntilNow
:获取起始日期至今的所有年月日(格式为 yyyy-MM-dd 的字符串列表)getUnitsUntilNow
:获取起始日期至今的所有年/月/日(字符串列表)getTimestampSlicesUntilNow
:获取起始日期至今,按固定天数进行切片的起止时间戳getLocalDateTimeSlicesUntilNow
: 获取起始日期至今,按固定天数进行切片的起止 LocalDateTimegetDateStringSlicesUntilNow
: 获取起始日期至今,按固定天数进行切片的起止格式化日期字符串getTimeSlicesUntilNow
:获取起始日期至今,按固定天数进行切片的起止日期,起止日期的类型通过transformFunction
转化
- 时间判断
isLeapYear()
:是否是闰年
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.function.Function;
public class TimeUtils {
// private static final ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault();
public static final ZoneId DEFAULT_ZONE_ID = ZoneId.of("Asia/Shanghai");
public static final DateTimeFormatter DEFAULT_YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy");
public static final DateTimeFormatter DEFAULT_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
public static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DEFAULT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// -----时间格式转化-----
// 年月日转化成时间戳
public static long toTimestamp(int year, int month, int day) {
return toTimestamp(LocalDate.of(year, month, day));
}
// 年月日时分秒转化成时间戳
public static long toTimestamp(int year, int month, int day, int hour, int minute, int second) {
return toTimestamp(LocalDateTime.of(year, month, day, hour, minute, second));
}
// Date 转化成时间戳
public static long toTimestamp(Date date) {
return date.toInstant().getEpochSecond();
}
// LocalDate 转化成时间戳
public static long toTimestamp(LocalDate localDate) {
return toTimestamp(localDate.atStartOfDay());
}
// LocalDateTime 转化成时间戳
public static long toTimestamp(LocalDateTime localDateTime) {
ZonedDateTime zonedDateTime = localDateTime.atZone(DEFAULT_ZONE_ID);
Instant instant = zonedDateTime.toInstant();
return instant.getEpochSecond();
}
// 格式化日期字符串转化成时间戳
public static long toTimestamp(String dateString) {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, DEFAULT_TIME_FORMATTER);
ZonedDateTime zonedDateTime = localDateTime.atZone(DEFAULT_ZONE_ID);
Instant instant = zonedDateTime.toInstant();
return instant.getEpochSecond();
}
// 时间戳转化成 LocalDateTime
public static LocalDateTime toLocalDateTime(long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), DEFAULT_ZONE_ID);
}
// 时间戳转化成 LocalDateTime
public static LocalDateTime toLocalDateTime(LocalDateTime localdatetime) {
return localdatetime;
}
// 格式化日期字符串转化成 LocalDateTime
public static LocalDateTime toLocalDateTime(String dateString) {
return LocalDateTime.parse(dateString, DEFAULT_TIME_FORMATTER);
}
public static LocalDateTime toLocalDateTime(String dateString, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(dateString, formatter);
}
// Date 转化成格式化日期字符串
public static String format(Date date) {
return DEFAULT_TIME_FORMATTER.format(date.toInstant());
}
// Date 转化成格式化日期字符串
public static String format(Date date, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.getDefault());
return formatter.format(date.toInstant());
}
// 时间戳转化成格式化日期字符串
public static String format(long timestamp) {
return format(LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), DEFAULT_ZONE_ID));
}
public static String format(long timestamp, String pattern) {
return format(LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), DEFAULT_ZONE_ID), pattern);
}
// TemporalAccessor 转化成格式化日期字符串
// TemporalAccessor 接口表示任何可以被解析为日期和时间的对象,比如 LocalDate、LocalDateTime、ZonedDateTime 等
public static String format(TemporalAccessor dateTime) {
return DEFAULT_TIME_FORMATTER.format(dateTime);
}
public static String format(TemporalAccessor dateTime, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return formatter.format(dateTime);
}
// -----时间获取-----
// 获取当前时间戳(精确至毫秒,13位)
public static long getCurrentTimestamp() {
return System.currentTimeMillis();
}
// 获取当前的年(仅数值)
public static int getCurrentYearValue() {
return LocalDate.now().getYear();
}
// 获取当前的月(仅数值)
public static int getCurrentMonthValue() {
return LocalDate.now().getMonthValue();
}
// 获取当前的日(仅数值)
public static int getCurrentDayValue() {
return LocalDate.now().getDayOfMonth();
}
// 获取当前的年(格式为 yyyy 的字符串)
public static String getCurrentYear() {
return String.valueOf(LocalDate.now().getYear());
}
// 获取当前的年月(格式为 yyyy-MM 的字符串)
public static String getCurrentMonth() {
return YearMonth.now().toString();
}
// 获取当前的年月日(格式为 yyyy-MM-dd 的字符串)
public static String getCurrentDate() {
return LocalDate.now().toString();
}
// 获取当前的年月日时分秒(格式为 yyyy-MM-dd HH:mm:ss 的字符串)
public static String getCurrentDateTime() {
return LocalDateTime.now().format(DEFAULT_TIME_FORMATTER);
}
// 获取指定年月的当月第一天(格式为 yyyy-MM-dd 的字符串)
public static String getFirstDayOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
return yearMonth.atDay(1).format(DEFAULT_DATE_FORMATTER);
}
// 获取指定年月的当月最后一天(格式为 yyyy-MM-dd 的字符串)
public static String getLastDayOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
return yearMonth.atEndOfMonth().format(DEFAULT_DATE_FORMATTER);
}
// 获取起始日期至今的所有年(格式为 yyyy 的字符串列表)
public static List<String> getYearsUntilNow(int year, int month, int day) {
return getUnitsUntilNow(year, month, day, ChronoUnit.YEARS);
}
// 获取起始日期至今的所有年月(格式为 yyyy-MM 的字符串列表)
public static List<String> getMonthsUntilNow(int year, int month, int day) {
return getUnitsUntilNow(year, month, day, ChronoUnit.MONTHS);
}
// 获取起始日期至今的所有年月日(格式为 yyyy-MM-dd 的字符串列表)
public static List<String> getDaysUntilNow(int year, int month, int day) {
return getUnitsUntilNow(year, month, day, ChronoUnit.DAYS);
}
// 获取起始日期至今的所有年/月/日(字符串列表)
public static List<String> getUnitsUntilNow(int year, int month, int day, ChronoUnit unit) {
DateTimeFormatter formatter = null;
switch (unit) {
case YEARS:
formatter = DEFAULT_YEAR_FORMATTER;
break;
case MONTHS:
formatter = DEFAULT_MONTH_FORMATTER;
break;
case DAYS:
formatter = DEFAULT_DATE_FORMATTER;
break;
default:
formatter = DEFAULT_DATE_FORMATTER;
break;
}
List<String> results = new ArrayList<>();
LocalDate startDate = LocalDate.of(year, month, day);
LocalDate currentDate = LocalDate.now();
while (startDate.isBefore(currentDate) || startDate.isEqual(currentDate)) {
results.add(startDate.format(formatter));
startDate = startDate.plus(1, unit);
}
return results;
}
// 获取起始日期至今,按固定天数进行切片的起止时间戳
public static List<Map<String, Long>> getTimestampSlicesUntilNow(int year, int month, int day, int interval) {
return getTimeSlicesUntilNow(year, month, day, interval, TimeUtils::toTimestamp);
}
// 获取起始日期至今,按固定天数进行切片的起止 LocalDateTime
public static List<Map<String, LocalDateTime>> getLocalDateTimeSlicesUntilNow(int year, int month, int day, int interval) {
return getTimeSlicesUntilNow(year, month, day, interval, TimeUtils::toLocalDateTime);
}
// 获取起始日期至今,按固定天数进行切片的起止格式化日期字符串
public static List<Map<String, String>> getDateStringSlicesUntilNow(int year, int month, int day, int interval) {
return getTimeSlicesUntilNow(year, month, day, interval, TimeUtils::format);
}
// 获取起始日期至今,按固定天数进行切片的起止日期,起止日期的类型通过 transformFunction 转化
public static <R> List<Map<String, R>> getTimeSlicesUntilNow(int year, int month, int day, int interval, Function<LocalDateTime, R> transformFunction) {
List<Map<String, R>> slices = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
LocalDateTime startTime = LocalDateTime.of(year, month, day, 0, 0,0);
LocalDateTime endTime = startTime.plusDays(interval);
while (startTime.isBefore(endTime)) {
R transformedStartTime = transformFunction.apply(startTime);
R transformedEndTime = transformFunction.apply(endTime);
Map<String, R> params = new HashMap<String, R>(){{
put("startTime", transformedStartTime);
put("endTime", transformedEndTime);
}};
slices.add(params);
startTime = startTime.plusDays(interval);
endTime = endTime.plusDays(interval);
if(now.isBefore(endTime)){
endTime = now;
}
}
return slices;
}
// -----时间判断或基本运算-----
// 是否是闰年
public boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}