Java8 新特性中推出了日期處理類LocalDate,實在是太方便了,主要用到是下面的3個類。
java.time.LocalDate ->只對年月日做出處理
java.time.LocalTime ->只對時分秒納秒做出處理
java.time.LocalDateTime ->同時可以處理年月日和時分秒
優點:
除了使用起來更加簡單和靈活,主要是傳統的時期處理類Date、Calendar不是多線程安全的,而LocalDate 線程安全的,所以不用擔心併發問題。下面是我的實戰代碼:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.collect.Lists;
/**
*
*
* Java 8 的時間工具類
*/
public class DateUtils {
/**
* 默認使用系統當前時區
*/
private static final ZoneId ZONE = ZoneId.systemDefault();
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_FORMAT_DS = "yyyyMMdd";
private static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
private static final String TIME_FORMAT = "yyyyMMddHHmmss";
private static final String REGEX = "\\:|\\-|\\s";
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 獲取當前時間
*
* @param format
* @return
*/
public static String getCurrentTime(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime now = LocalDateTime.now();
return now.format(dateTimeFormatter);
}
/**
* 獲取昨日時間
*
* @param format
* @return
*/
public static String getYesterday(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate nowDate = LocalDate.now();
LocalDate yesterday = nowDate.minusDays(1);
return yesterday.format(dateTimeFormatter);
}
/**
* 獲取上週的時間
*
* @param format
* @return
*/
public static String getLastWeek(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate nowDate = LocalDate.now();
LocalDate lastWeek = nowDate.minusWeeks(1);
return lastWeek.format(dateTimeFormatter);
}
/**
* 獲取上個月的時間
*
* @param format
* @return
*/
public static String getLastMonth(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate nowDate = LocalDate.now();
LocalDate lastMonth = nowDate.minusMonths(1);
return lastMonth.format(dateTimeFormatter);
}
/**
* 獲取去年的時間
*
* @param format
* @return
*/
public static String getLastYear(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate nowDate = LocalDate.now();
LocalDate lastYear = nowDate.minusYears(1);
return lastYear.format(dateTimeFormatter);
}
/**
* 獲取前多少天的日期
*
* @param format
* @param num
* @return
*/
public static String getBeforeSomeDay(String format, int num) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate nowDate = LocalDate.now();
LocalDate beforeDay = nowDate.minusDays(num);
return beforeDay.format(dateTimeFormatter);
}
/**
* 獲取指定時間的前多少天
*
* @param format
* @param date
* @param num
* @return
*/
public static String getBeforeDayOfDate(String format, String date, int num) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
LocalDate beforeDay = localDate.minusDays(num);
return beforeDay.format(dateTimeFormatter);
}
/**
* 獲取當天的開始時間 yyyy-MM-dd 00:00:00
*
* @param format
* @return
*/
public static String getDayStartTime(String format, String date) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
LocalDateTime toDayStart = LocalDateTime.of(localDate, LocalTime.MIN);
return toDayStart.format(FORMATTER);
}
/**
* 獲取當天的結束時間 yyyy-MM-dd 23:59:59
*
* @param format
* @return
*/
public static String getDayEndTime(String format, String date) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
LocalDateTime toDayStart = LocalDateTime.of(localDate, LocalTime.MAX);
return toDayStart.format(FORMATTER);
}
/**
* 獲取兩個時間之間的間隔天數
*
* @param startDate yyyyMMdd
* @param endDate yyyyMMdd
* @return
*/
public static long getRangeCountOfDate(String startDate, String endDate) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_DS);
LocalDate startLocalDate = LocalDate.parse(startDate, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(endDate, dateTimeFormatter);
long count = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
return count;
}
/**
* 後期兩個時間之間的所有日期 【包含開始時間和結束時間】
*
* @param startDate yyyyMMdd
* @param endDate yyyyMMdd
* @return
*/
public static List<String> getRangeOfDate(String startDate, String endDate) {
List<String> range = Lists.newArrayList();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_DS);
LocalDate startLocalDate = LocalDate.parse(startDate, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(endDate, dateTimeFormatter);
long count = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
if (count < 0) {
return range;
}
range = Stream.iterate(startLocalDate, d -> d.plusDays(1)).limit(count + 1).map(
s -> s.format(dateTimeFormatter)).collect(Collectors.toList());
return range;
}
public static void main(String[] args) {
System.out.println(getRangeOfDate("20191010", "20191020"));
}
}