1. 簡介
Java8提供了全新的日期處理包(java.time.*),根據Java8日期新特性封裝日期時間工具類LocalDateTimeUtils。
2. 相關博客
3. 工具類方法目錄
說明 | 方法名稱 |
---|---|
當前時間 | LocalDateTimeUtils.now() |
Date 轉 LocalDateTime | LocalDateTimeUtils.convert(new Date())); |
LocalDateTime 轉 Date | LocalDateTimeUtils.convert(LocalDateTime.now())); |
今天開始時間 | LocalDateTimeUtils.todayStartTime() |
今天結束時間 | LocalDateTimeUtils.todayEndTime() |
昨天開始時間 | LocalDateTimeUtils.yesterdayStartTime() |
昨天結束時間 | LocalDateTimeUtils.yesterdayEndTime() |
最近7天開始時間 | LocalDateTimeUtils.last7DaysStartTime() |
最近7天結束時間 | LocalDateTimeUtils.last7DaysEndTime() |
最近30天開始時間 | LocalDateTimeUtils.last30DaysStartTime() |
最近30天天結束時間 | LocalDateTimeUtils.last30DaysEndTime() |
最近一年開始時間 | LocalDateTimeUtils.last1YearStartTime() |
最近一年結束時間 | LocalDateTimeUtils.last1YearEndTime() |
本周開始時間 | LocalDateTimeUtils.weekStartTime() |
本周結束時間 | LocalDateTimeUtils.weekEndTime() |
本月開始時間 | LocalDateTimeUtils.monthStartTime() |
本月結束時間 | LocalDateTimeUtils.monthEndTime() |
本季度開始時間 | LocalDateTimeUtils.quarterStartTime() |
本季度結束時間 | LocalDateTimeUtils.quarterEndTime() |
本半年開始時間 | LocalDateTimeUtils.halfYearStartTime() |
本半年結束時間 | LocalDateTimeUtils.halfYearEndTime() |
本年開始時間 | LocalDateTimeUtils.yearStartTime() |
本年結束時間 | LocalDateTimeUtils.yearEndTime() |
上周開始時間 | LocalDateTimeUtils.lastWeekStartTime() |
上周結束時間 | LocalDateTimeUtils.lastWeekEndTime() |
上月開始時間 | LocalDateTimeUtils.lastMonthStartTime() |
上月結束時間 | LocalDateTimeUtils.lastMonthEndTime() |
上季度開始時間 | LocalDateTimeUtils.lastQuarterStartTime() |
上季度結束時間 | LocalDateTimeUtils.lastQuarterEndTime() |
上半年開始時間 | LocalDateTimeUtils.lastHalfYearStartTime() |
上半年結束時間 | LocalDateTimeUtils.lastHalfYearEndTime() |
上一年開始時間 | LocalDateTimeUtils.lastYearStartTime() |
上一年結束時間 | LocalDateTimeUtils.lastYearEndTime() |
下周開始時間 | LocalDateTimeUtils.nextWeekStartTime() |
下周結束時間 | LocalDateTimeUtils.nextWeekEndTime() |
下月開始時間 | LocalDateTimeUtils.nextMonthStartTime() |
下月結束時間 | LocalDateTimeUtils.nextMonthEndTime() |
下季度開始時間 | LocalDateTimeUtils.nextQuarterStartTime() |
下季度結束時間 | LocalDateTimeUtils.nextQuarterEndTime() |
下半年開始時間 | LocalDateTimeUtils.nextHalfYearStartTime() |
下半年結束時間 | LocalDateTimeUtils.nextHalfYearEndTime() |
下一年開始時間 | LocalDateTimeUtils.nextYearStartTime() |
下一年結束時間 | LocalDateTimeUtils.nextYearEndTime() |
4. 示例代碼
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* LocalDateTime工具類
*
* @author CL
*/
public class LocalDateTimeUtils {
/**
* 當前時間
*
* @return
*/
public static LocalDateTime now() {
return LocalDateTime.now();
}
/**
* Date 轉 LocalDateTime
*
* @return
*/
public static LocalDateTime convert(Date date) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
}
/**
* LocalDateTime 轉 Date
*
* @return
*/
public static Date convert(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 今天開始時間
*
* @return
*/
public static LocalDateTime todayStartTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
}
/**
* 今天結束時間
*
* @return
*/
public static LocalDateTime todayEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 昨天開始時間
*
* @return
*/
public static LocalDateTime yesterdayStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 昨天結束時間
*
* @return
*/
public static LocalDateTime yesterdayEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MAX);
}
/**
* 最近7天開始時間
*
* @return
*/
public static LocalDateTime last7DaysStartTime() {
return LocalDateTime.of(LocalDate.now().minus(6L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近7天結束時間
*
* @return
*/
public static LocalDateTime last7DaysEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 最近30天開始時間
*
* @return
*/
public static LocalDateTime last30DaysStartTime() {
return LocalDateTime.of(LocalDate.now().minus(29L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近30天結束時間
*
* @return
*/
public static LocalDateTime last30DaysEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 最近一年開始時間
*
* @return
*/
public static LocalDateTime last1YearStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).plus(1L, ChronoUnit.DAYS), LocalTime.MIN);
}
/**
* 最近一年結束時間
*
* @return
*/
public static LocalDateTime last1YearEndTime() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 本周開始時間
*
* @return
*/
public static LocalDateTime weekStartTime() {
LocalDate now = LocalDate.now();
return LocalDateTime.of(now.minusDays(now.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 本周結束時間
*
* @return
*/
public static LocalDateTime weekEndTime() {
LocalDate now = LocalDate.now();
return LocalDateTime.of(now.plusDays(7 - now.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 本月開始時間
*
* @return
*/
public static LocalDateTime monthStartTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 本月結束時間
*
* @return
*/
public static LocalDateTime monthEndTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 本季度開始時間
*
* @return
*/
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);
}
/**
* 本季度結束時間
*
* @return
*/
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);
}
/**
* 本半年開始時間
*
* @return
*/
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);
}
/**
* 本半年結束時間
*
* @return
*/
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);
}
/**
* 本年開始時間
*
* @return
*/
public static LocalDateTime yearStartTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 本年結束時間
*
* @return
*/
public static LocalDateTime yearEndTime() {
return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
/**
* 上周開始時間
*
* @return
*/
public static LocalDateTime lastWeekStartTime() {
LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(lastWeek.minusDays(lastWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 上周結束時間
*
* @return
*/
public static LocalDateTime lastWeekEndTime() {
LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(lastWeek.plusDays(7 - lastWeek.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 上月開始時間
*
* @return
*/
public static LocalDateTime lastMonthStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 上月結束時間
*
* @return
*/
public static LocalDateTime lastMonthEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 上季度開始時間
*
* @return
*/
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);
}
/**
* 上季度結束時間
*
* @return
*/
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);
}
/**
* 上半年開始時間
*
* @return
*/
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);
}
/**
* 上半年結束時間
*
* @return
*/
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);
}
/**
* 上一年開始時間
*
* @return
*/
public static LocalDateTime lastYearStartTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 上一年結束時間
*
* @return
*/
public static LocalDateTime lastYearEndTime() {
return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
}
/**
* 下周開始時間
*
* @return
*/
public static LocalDateTime nextWeekStartTime() {
LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(nextWeek.minusDays(nextWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
}
/**
* 下周結束時間
*
* @return
*/
public static LocalDateTime nextWeekEndTime() {
LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
return LocalDateTime.of(nextWeek.plusDays(7 - nextWeek.getDayOfWeek().getValue()), LocalTime.MAX);
}
/**
* 下月開始時間
*
* @return
*/
public static LocalDateTime nextMonthStartTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
}
/**
* 下月結束時間
*
* @return
*/
public static LocalDateTime nextMonthEndTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
}
/**
* 下季度開始時間
*
* @return
*/
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);
}
/**
* 下季度結束時間
*
* @return
*/
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);
}
/**
* 上半年開始時間
*
* @return
*/
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);
}
/**
* 上半年結束時間
*
* @return
*/
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);
}
/**
* 下一年開始時間
*
* @return
*/
public static LocalDateTime nextYearStartTime() {
return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
}
/**
* 下一年結束時間
*
* @return
*/
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());
}
}