import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Date;
/**
* 基于Java8的时间工具类
*
* @author keji
* @version $Id: DateUtil.java, v 0.1 2018-12-28 14:11 keji Exp $
*/
public class DateUtil {
/**
* 例如:2018-12-28
*/
public static final String DATE = "yyyy-MM-dd";
/**
* 例如:2018-12-28 10:00:00
*/
public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
/**
* 例如:10:00:00
*/
public static final String TIME = "HHmmss";
/**
* 例如:10:00
*/
public static final String TIME_WITHOUT_SECOND = "HH:mm";
/**
* 例如:2018-12-28 10:00
*/
public static final String DATE_TIME_WITHOUT_SECONDS = "yyyy-MM-dd HH:mm";
/**
* 获取年
*
* @return 年
*/
public static int getYear() {
LocalTime localTime = LocalTime.now();
return localTime.get(ChronoField.YEAR);
}
/**
* 获取月份
*
* @return 月份
*/
public static int getMonth() {
LocalTime localTime = LocalTime.now();
return localTime.get(ChronoField.MONTH_OF_YEAR);
}
/**
* 获取某月的第几天
*
* @return 几号
*/
public static int getMonthOfDay() {
LocalTime localTime = LocalTime.now();
return localTime.get(ChronoField.DAY_OF_MONTH);
}
/**
* 格式化日期为字符串
*
* @param date date
* @param pattern 格式
* @return 日期字符串
*/
public static String format(Date date,String pattern){
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 解析字符串日期为Date
*
* @param dateStr 日期字符串
* @param pattern 格式
* @return Date
*/
public static Date parse(String dateStr, String pattern) {
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* 为Date增加分钟,减传负数
*
* @param date 日期
* @param plusMinutes 要增加的分钟数
* @return 新的日期
*/
public static Date addMinutes(Date date, Long plusMinutes) {
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
LocalDateTime newDateTime = dateTime.plusMinutes(plusMinutes);
return Date.from(newDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 增加时间
*
* @param date date
* @param hour 要增加的小时数
* @return new date
*/
public static Date addHour(Date date, Long hour) {
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
LocalDateTime localDateTime = dateTime.plusHours(hour);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @return 返回当天的起始时间
*/
public static Date getStartTime() {
LocalDateTime now = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
return localDateTime2Date(now);
}
/**
* @return 返回当天的结束时间
*/
public static Date getEndTime() {
LocalDateTime now = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999);
return localDateTime2Date(now);
}
/**
* 减月份
*
* @param monthsToSubtract 月份
* @return Date
*/
public static Date minusMonths(long monthsToSubtract){
LocalDate localDate = LocalDate.now().minusMonths(monthsToSubtract);
return localDate2Date(localDate);
}
/**
* LocalDate类型转为Date
*
* @param localDate LocalDate object
* @return Date object
*/
public static Date localDate2Date(LocalDate localDate) {
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* LocalDateTime类型转为Date
*
* @param localDateTime LocalDateTime object
* @return Date object
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 查询当前年的第一天
*
* @param pattern 格式,默认格式yyyyMMdd
* @return 20190101
*/
public static String getFirstDayOfCurrentYear(String pattern) {
LocalDateTime localDateTime = LocalDateTime.now().withMonth(1).withDayOfMonth(1);
if (StringUtils.isEmpty(pattern)) {
pattern = "yyyyMMdd";
}
return format(localDateTime2Date(localDateTime), pattern);
}
/**
* 查询前一年最后一个月第一天
*
* @param pattern 格式,默认格式yyyyMMdd
* @return 20190101
*/
public static String getLastMonthFirstDayOfPreviousYear(String pattern) {
LocalDateTime localDateTime = LocalDateTime.now().minusYears(1L).withMonth(12).withDayOfMonth(1);
if (StringUtils.isEmpty(pattern)) {
pattern = "yyyyMMdd";
}
return format(localDateTime2Date(localDateTime), pattern);
}
/**
* 查询前一年最后一个月第一天
*
* @param pattern 格式,默认格式yyyyMMdd
* @return 20190101
*/
public static String getLastMonthLastDayOfPreviousYear(String pattern) {
LocalDateTime localDateTime = LocalDateTime.now().minusYears(1L).with(TemporalAdjusters.lastDayOfYear());
if (StringUtils.isEmpty(pattern)) {
pattern = "yyyyMMdd";
}
return format(localDateTime2Date(localDateTime), pattern);
}
/**
* 获取当前日期
*
* @param pattern 格式,默认格式yyyyMMdd
* @return 20190101
*/
public static String getCurrentDay(String pattern) {
LocalDateTime localDateTime = LocalDateTime.now();
if (StringUtils.isEmpty(pattern)) {
pattern = "yyyyMMdd";
}
return format(localDateTime2Date(localDateTime), pattern);
}
}
基于Java8的时间工具类(持续更新)
最新推荐文章于 2022-07-20 13:52:55 发布