JAVA8常用时间工具值得收藏
可以解决各种常用的时间开发需求,包括日历表,获取周几,一个月开始和结束时间
根据时间区间获取当前时间区间内所有日期,获取一个月的天数,各种日期格式化之间
相互转换还有诸多讲解和功能,请参考一下代码:
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 操作时间工具类
*
* @author LiSanWei
*/
public class DataTimeUtils {
public static final DateTimeFormatter FORMATTER_FULL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("+8"));
public static final DateTimeFormatter DATE_STR = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.of("+8"));
public static void main(String[] args) {
// 1. java8获取当前时间
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("+8"));
// 2.将获取当前时间转化成特定格式
System.out.println("dateTimeFormatter = " + now.format(dateTimeFormatter));
// 3.字符串转成特定时间
String strDateTime = "2016-04-04 11:50:53";
LocalDateTime parse = LocalDateTime.parse(strDateTime, dateTimeFormatter);
// 4.一天的开始/一天的结束
LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
String startDay = start.format(dateTimeFormatter);
String endDay = end.format(dateTimeFormatter);
System.out.println("startDay = " + startDay + "endDay = " + endDay);
// 5.获取当前月的第一天
LocalDate currentMonthFirstDay = LocalDate.now().minusMonths(0).with(TemporalAdjusters.firstDayOfMonth());
System.out.println("with = " + currentMonthFirstDay);
// 6.获取周几
DayOfWeek dayOfWeek = currentMonthFirstDay.getDayOfWeek();
System.out.println("dayOfWeek = " + dayOfWeek);
int value = dayOfWeek.getValue();
System.out.println("value = " + value);
// 7.获取一个月多少 //当月的天数
int i = LocalDate.now().lengthOfMonth();
// 8.获取当前月的最后一天
LocalDate currentMonthEndDay = LocalDate.now().minusMonths(0).with(TemporalAdjusters.lastDayOfMonth());
System.out.println("withEnd = " + currentMonthEndDay);
// 9.一个月之前,一个月之后
LocalDate currentNow = LocalDate.now();
LocalDate minus = currentNow.minus(1, ChronoUnit.MONTHS);
LocalDate plus = currentNow.plus(1, ChronoUnit.MONTHS);
System.out.println("with = " + minus + plus);
// 10.获取几天/几天后的日期
LocalDate minus1 = currentNow.minus(1, ChronoUnit.DAYS);
LocalDate plus1 = currentNow.plus(1, ChronoUnit.DAYS);
System.out.println("minus1 = " + minus1);
System.out.println("plus1 = " + plus1);
// 11.java8指定年月日
LocalDate of = LocalDate.of(2021, 11, 18);
System.out.println("of = " + of);
}
/**
* 将字符串 转成 LocalDateTime 示例:"2016-04-04 11:50:53"
*
* @param dateTime 时间
* @return LocalDateTime
*/
public static LocalDateTime strTimeToLocalDateTime(String dateTime) {
return LocalDateTime.parse(dateTime, FORMATTER_FULL);
}
/**
* date 转成 LocalDateTime
*
* @param date 日期
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
/**
* date 转成 LocalDate
*
* @param date 日期
*/
public static LocalDate dateToLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalDate();
}
/**
* date 转成 LocalTime
*
* @param date 日期
*/
public static LocalTime dateToLocalTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalTime();
}
/**
* LocalDateTime 转成 Date
*
* @param localDateTime 时间
* @return 结果集
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
/**
* localDate 转成 Date
*
* @param localDate 结果集
*/
public static Date localDateToDate(LocalDate localDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
}
/**
* LocalTime 转成 Date
*
* @param localTime 本地时间
* @param localDate 时间
*/
public static Date localTimeToDate(LocalTime localTime, LocalDate localDate) {
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
/**
* 获取前一个月的第一天的时间
*
* @return 几号
*/
public Integer getLastMonthEndDay() {
LocalDate with = LocalDate.now().plusMonths(-1).with(TemporalAdjusters.firstDayOfMonth());
return with.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
}
/**
* 获取后一个月的第一天的时间
*
* @return 几号
*/
public static Integer getNextMonthEndDay() {
LocalDate with = LocalDate.now().plusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
return with.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
}
/**
* 获取日历表
*
* @param index -1表示上月 0表示当月 1表示下月
* @return 结果集
*/
public static List getCalendarTable(Integer index) {
// 根据输入确定月份
LocalDate months = LocalDate.now().plusMonths(index);
// 获取当前月的第一天
LocalDate currentMonthFirstDay = months.with(TemporalAdjusters.firstDayOfMonth());
// 获取当前月的最后一天
LocalDate currentMonthEndDay = months.with(TemporalAdjusters.lastDayOfMonth());
// 获取一个月多少 //当月的天数
int days = LocalDate.now().lengthOfMonth();
// 获取周几
int dayOfWeek = currentMonthFirstDay.getDayOfWeek().getValue();
// 获取日历第一天
LocalDate calendarFirstDay = months.minus(dayOfWeek - 1, ChronoUnit.DAYS);
// 获取日历最后一天
LocalDate calendarEndDay = currentMonthEndDay.plus(42 - days - (dayOfWeek - 1), ChronoUnit.DAYS);
List localDateList = new ArrayList<>();
long length = calendarEndDay.toEpochDay() - calendarFirstDay.toEpochDay();
DataCalendarDTO dataCalendarDTO;
for (long i = length; i >= 0; i--) {
dataCalendarDTO = new DataCalendarDTO();
// 当前日期
LocalDate localDate = calendarEndDay.minusDays(i);
dataCalendarDTO.setDate(localDate.format(DATE_STR));
// 周几
dataCalendarDTO.setWeek(localDate.getDayOfWeek().getValue());
localDateList.add(dataCalendarDTO);
}
return localDateList;
}
/**
* @param begin 开始日期
* @param end 结束日期
* @return 开始与结束之间的所以日期,包括起止
*/
public static List getMiddleAllDate(LocalDate begin, LocalDate end) {
List localDateList = new ArrayList<>();
long length = end.toEpochDay() - begin.toEpochDay();
for (long i = length; i >= 0; i--) {
localDateList.add(end.minusDays(i));
}
return localDateList;
}
/**
* 获取前几天或者后几天的时间
*
* @param offSet 负数代表前几天
* @param timeFormat 时间格式化类型
* @return 格式化后的结果
*/
public static String getYesterdayByFormat(Integer offSet, String timeFormat) {
return LocalDateTime.now().plusDays(offSet).format(DateTimeFormatter.ofPattern(timeFormat));
}
}
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* LocalDateTime工具类
*
* @author lisanwei
*/
public class LocalDateTimeUtils {
/**
* 当前时间
*/
public static LocalDateTime now() {
return LocalDateTime.now();
}
/**
* Date 转 LocalDateTime
*/
public static LocalDateTime convert(Date date) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
}
/**
* LocalDateTime 转 Date
*/
public static Date convert(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 今天开始时间
*/
public static LocalDateTime todayStartTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
}
/**
* 今天结束时间
*/
public static LocalDateTime todayEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 昨天开始时间
*/
public static LocalDateTime yesterdayStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 昨天结束时间
*/
public static LocalDateTime yesterdayEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MAX);
}
/**
* 最近7天开始时间
*/
public static LocalDateTime last7DaysStartTime() {
return LocalDateTime.of(LocalDate.now().minus(6L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近7天结束时间
*/
public static LocalDateTime last7DaysEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 最近30天开始时间
*/
public static LocalDateTime last30DaysStartTime() {
return LocalDateTime.of(LocalDate.now().minus(29L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近30天结束时间
*/
public static LocalDateTime last30DaysEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 最近一年开始时间
*/
public static LocalDateTime last1YearStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).plus(1L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近一年结束时间
*/
public static LocalDateTime last1YearEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 本周开始时间
*/
public static LocalDateTime weekStartTime() {
LocalDate now = LocalDate.now();
return LocalDateTime.of(now.minusDays(now.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 本周结束时间
*/
public static LocalDateTime weekEndTime() {
LocalDate now = LocalDate.now();
return LocalDateTime.of(now.plusDays(7 - now.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 本月开始时间
*/
public static LocalDateTime monthStartTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 本月结束时间
*/
public static LocalDateTime monthEndTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 本季度开始时间
*/
public static LocalDateTime quarterStartTime() {
LocalDate now = LocalDate.now();
Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
return LocalDateTime.of(LocalDate.of(now.getYear(), month, 1), LocalTime.MIN);
}
/**
* 本季度结束时间
*/
public static LocalDateTime quarterEndTime() {
LocalDate now = LocalDate.now();
Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue()).plus(2L);
return LocalDateTime.of(LocalDate.of(now.getYear(), month, month.length(now.isLeapYear())), LocalTime.MAX);
}
/**
* 本半年开始时间
*/
public static LocalDateTime halfYearStartTime() {
LocalDate now = LocalDate.now();
Month month = (now.getMonthValue() > 6) ? Month.JULY : Month.JANUARY;
return LocalDateTime.of(LocalDate.of(now.getYear(), month, 1), LocalTime.MIN);
}
/**
* 本半年结束时间
*/
public static LocalDateTime halfYearEndTime() {
LocalDate now = LocalDate.now();
Month month = (now.getMonthValue() > 6) ? Month.DECEMBER : Month.JUNE;
return LocalDateTime.of(LocalDate.of(now.getYear(), month, month.length(now.isLeapYear())), LocalTime.MAX);
}
/**
* 本年开始时间
*/
public static LocalDateTime yearStartTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 本年结束时间
*/
public static LocalDateTime yearEndTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
/**
* 上周开始时间
*/
public static LocalDateTime lastWeekStartTime() {
LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(lastWeek.minusDays(lastWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 上周结束时间
*/
public static LocalDateTime lastWeekEndTime() {
LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(lastWeek.plusDays(7 - lastWeek.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 上月开始时间
*/
public static LocalDateTime lastMonthStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 上月结束时间
*/
public static LocalDateTime lastMonthEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 上季度开始时间
*/
public static LocalDateTime lastQuarterStartTime() {
LocalDate now = LocalDate.now();
Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
Month firstMonthOfLastQuarter = firstMonthOfQuarter.minus(3L);
int yearOfLastQuarter = firstMonthOfQuarter.getValue() < 4 ? now.getYear() - 1 : now.getYear();
return LocalDateTime.of(LocalDate.of(yearOfLastQuarter, firstMonthOfLastQuarter, 1), LocalTime.MIN);
}
/**
* 上季度结束时间
*/
public static LocalDateTime lastQuarterEndTime() {
LocalDate now = LocalDate.now();
Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
Month firstMonthOfLastQuarter = firstMonthOfQuarter.minus(1L);
int yearOfLastQuarter = firstMonthOfQuarter.getValue() < 4 ? now.getYear() - 1 : now.getYear();
return LocalDateTime.of(LocalDate.of(yearOfLastQuarter, firstMonthOfLastQuarter, firstMonthOfLastQuarter.maxLength()), LocalTime.MAX);
}
/**
* 上半年开始时间
*/
public static LocalDateTime lastHalfYearStartTime() {
LocalDate now = LocalDate.now();
int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() : now.getYear() - 1;
Month firstMonthOfLastHalfYear = (now.getMonthValue() > 6) ? Month.JANUARY : Month.JULY;
return LocalDateTime.of(LocalDate.of(lastHalfYear, firstMonthOfLastHalfYear, 1), LocalTime.MIN);
}
/**
* 上半年结束时间
*/
public static LocalDateTime lastHalfYearEndTime() {
LocalDate now = LocalDate.now();
int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() : now.getYear() - 1;
Month lastMonthOfLastHalfYear = (now.getMonthValue() > 6) ? Month.JUNE : Month.DECEMBER;
return LocalDateTime.of(LocalDate.of(lastHalfYear, lastMonthOfLastHalfYear, lastMonthOfLastHalfYear.maxLength()), LocalTime.MAX);
}
/**
* 上一年开始时间
*/
public static LocalDateTime lastYearStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 上一年结束时间
*/
public static LocalDateTime lastYearEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
/**
* 下周开始时间
*/
public static LocalDateTime nextWeekStartTime() {
LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(nextWeek.minusDays(nextWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 下周结束时间
*/
public static LocalDateTime nextWeekEndTime() {
LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(nextWeek.plusDays(7 - nextWeek.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 下月开始时间
*/
public static LocalDateTime nextMonthStartTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 下月结束时间
*/
public static LocalDateTime nextMonthEndTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 下季度开始时间
*/
public static LocalDateTime nextQuarterStartTime() {
LocalDate now = LocalDate.now();
Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
Month firstMonthOfNextQuarter = firstMonthOfQuarter.plus(3L);
int yearOfNextQuarter = firstMonthOfQuarter.getValue() > 9 ? now.getYear() + 1 : now.getYear();
return LocalDateTime.of(LocalDate.of(yearOfNextQuarter, firstMonthOfNextQuarter, 1), LocalTime.MIN);
}
/**
* 下季度结束时间
*/
public static LocalDateTime nextQuarterEndTime() {
LocalDate now = LocalDate.now();
Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
Month firstMonthOfNextQuarter = firstMonthOfQuarter.plus(5L);
int yearOfNextQuarter = firstMonthOfQuarter.getValue() > 9 ? now.getYear() + 1 : now.getYear();
return LocalDateTime.of(LocalDate.of(yearOfNextQuarter, firstMonthOfNextQuarter, firstMonthOfNextQuarter.maxLength()), LocalTime.MAX);
}
/**
* 下半年开始时间
*/
public static LocalDateTime nextHalfYearStartTime() {
LocalDate now = LocalDate.now();
int nextHalfYear = (now.getMonthValue() > 6) ? now.getYear() + 1 : now.getYear();
Month firstMonthOfNextHalfYear = (now.getMonthValue() > 6) ? Month.JANUARY : Month.JULY;
return LocalDateTime.of(LocalDate.of(nextHalfYear, firstMonthOfNextHalfYear, 1), LocalTime.MIN);
}
/**
* 下半年结束时间
*/
public static LocalDateTime nextHalfYearEndTime() {
LocalDate now = LocalDate.now();
int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() + 1 : now.getYear();
Month lastMonthOfNextHalfYear = (now.getMonthValue() > 6) ? Month.JUNE : Month.DECEMBER;
return LocalDateTime.of(LocalDate.of(lastHalfYear, lastMonthOfNextHalfYear, lastMonthOfNextHalfYear.maxLength()), LocalTime.MAX);
}
/**
* 下一年开始时间
*/
public static LocalDateTime nextYearStartTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 下一年结束时间
*/
public static LocalDateTime nextYearEndTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
public static void main(String[] args) {
System.out.println("当前时间:" + now());
System.out.println("Date 转 LocalDateTime:" + convert(new Date()));
System.out.println("LocalDateTime 转 Date:" + convert(LocalDateTime.now()));
System.out.println("今天开始时间:" + todayStartTime());
System.out.println("今天结束时间:" + todayEndTime());
System.out.println("昨天开始时间:" + yesterdayStartTime());
System.out.println("昨天结束时间:" + yesterdayEndTime());
System.out.println("最近7天开始时间:" + last7DaysStartTime());
System.out.println("最近7天结束时间:" + last7DaysEndTime());
System.out.println("最近30天开始时间:" + last30DaysStartTime());
System.out.println("最近30天天结束时间:" + last30DaysEndTime());
System.out.println("最近一年开始时间:" + last1YearStartTime());
System.out.println("最近一年结束时间:" + last1YearEndTime());
System.out.println("本周开始时间:" + weekStartTime());
System.out.println("本周结束时间:" + weekEndTime());
System.out.println("本月开始时间:" + monthStartTime());
System.out.println("本月结束时间:" + monthEndTime());
System.out.println("本季度开始时间:" + quarterStartTime());
System.out.println("本季度结束时间:" + quarterEndTime());
System.out.println("本半年开始时间:" + halfYearStartTime());
System.out.println("本半年结束时间:" + halfYearEndTime());
System.out.println("本年开始时间:" + yearStartTime());
System.out.println("本年结束时间:" + yearEndTime());
System.out.println("上周开始时间:" + lastWeekStartTime());
System.out.println("上周结束时间:" + lastWeekEndTime());
System.out.println("上月开始时间:" + lastMonthStartTime());
System.out.println("上月结束时间:" + lastMonthEndTime());
System.out.println("上季度开始时间:" + lastQuarterStartTime());
System.out.println("上季度结束时间:" + lastQuarterEndTime());
System.out.println("上半年开始时间:" + lastHalfYearStartTime());
System.out.println("上半年结束时间:" + lastHalfYearEndTime());
System.out.println("上一年开始时间:" + lastYearStartTime());
System.out.println("上一年结束时间:" + lastYearEndTime());
System.out.println("下周开始时间:" + nextWeekStartTime());
System.out.println("下周结束时间:" + nextWeekEndTime());
System.out.println("下月开始时间:" + nextMonthStartTime());
System.out.println("下月结束时间:" + nextMonthEndTime());
System.out.println("下季度开始时间:" + nextQuarterStartTime());
System.out.println("下季度结束时间:" + nextQuarterEndTime());
System.out.println("下半年开始时间:" + nextHalfYearStartTime());
System.out.println("下半年结束时间:" + nextHalfYearEndTime());
System.out.println("下一年开始时间:" + nextYearStartTime());
System.out.println("下一年结束时间:" + nextYearEndTime());
}
}