1,再当前日期上进行累加
/**
* 当前日期加减多少天
*/
public static String getNowDateString(int day){
return LocalDate.now().plusDays(day).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
/**
* 当前日期加减多少月
*/
public static Date getNowYmdString(int month) {
return Date.from(LocalDate.now().plusMonths(month).atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
}
/**
* 当前日期加减多少年后凌晨的日期+时间字符串
*/
public static String getBeginOfYear(int year) {
LocalDate localDate = LocalDate.now().plusYears(year);
LocalDateTime dateTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* 当前日期加减多少天后凌晨的日期+时间字符串
*/
public static String getBeginOfDay(int day) {
LocalDate localDate = LocalDate.now().plusDays(day);
LocalDateTime dateTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
2,获取月初周初的时间
/**
* 获取周初第一天的时间字符串
*/
public static String getFirstOfWeek() {
LocalDateTime localDate = LocalDateTime.now().with(DayOfWeek.MONDAY);
return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
}
/**
* 获取周末的时间字符串
*/
public static String getEndOfWeek() {
LocalDateTime localDate = LocalDateTime.now().with(DayOfWeek.SUNDAY);
return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59"));
}
/**
* 获取月初第一天的时间字符串
*/
public static String getFirstOfMonth() {
LocalDateTime localDate = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth());
return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));
}
/**
* 获取月末的时间字符串
*/
public static String getEndOfMonth() {
LocalDateTime localDate = LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59"));
}
/**
* 当前日期加减多少月后的月初日期时间戳
*/
public static long getFirstOfMonth(int lastMonth) {
return LocalDate.now()
.minusMonths(lastMonth)
.with(TemporalAdjusters.firstDayOfMonth())
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
}
/**
* 当前日期加减多少月后的月尾日期时间戳
*/
public static long getLastOfMonth(int lastMonth) {
return LocalDate.now()
.minusMonths(lastMonth)
.with(TemporalAdjusters.firstDayOfNextMonth())
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli() - 1;
}
3,获取日期区间内月份列表 ["2022-01","2022-02"]
/**
* 获取日期区间内月份列表 ["2022-01","2022-02"]
*/
public static List<String> getRankingNeedMonth(String beginTime, String endTime) {
List<String> ymList = Lists.newArrayList();
int intervalMonth = (int)LocalDate.parse(endTime).until(LocalDate.parse(beginTime), ChronoUnit.MONTHS);
if (0 == intervalMonth) {
return ymList;
}
for (int i = intervalMonth; i < 0; i++) {
ymList.add(LocalDate.now().plusMonths((i + 1)).format(DateTimeFormatter.ofPattern("yyyy-MM")));
}
return ymList;
}
4,计算两个日期间隔了日,时,分,秒
/**
* 时间转换
*/
public static Date str2Date(String time) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
} catch (ParseException e) {
return new Date();
}
}
/**
* 计算参数与当前时间间隔多少天多少小时多少分钟多少秒
*/
public static String calIntervalStr(String time) {
Date now = new Date();
Date before = str2Date(time);
long diff = now.getTime() - before.getTime();
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000* 60 * 60);
long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000* 60);
long second = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);
return days + "天" + hours + "小时" + minutes + "分钟" + second + "秒";
}