Java8 LocalDateTime 常见使用_获取当天、本周、本月、本季度、本年度起止时间
一、返回 LocalDateTime 格式的起止时间
/**
* @Description:返回 LocalDateTime 格式的日、周、月、季度、年起止时间
* @Author: lsiyan
* @Date: 2020/8/21 22:47
*/
import java.time.*;
import java.time.temporal.TemporalAdjusters;
/**
* jdk8 获取当天,本周,本月,本季度,本年起始时间工具类 LocalDateTime
*/
public class LocalDateTimeUtils {
public static final String MinTime = "T00:00:00";
public static final String MaxTime = "T23:59:59.999999999";
/**
* @Description:当天的开始时间
* @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
*/
public static LocalDateTime getStartOrEndDayOfDay(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if(isFirst){
return LocalDateTime.of(today, LocalTime.MIN);
}else{
return LocalDateTime.of(today, LocalTime.MAX);
}
}
/**
* @Description:本周的开始时间
* @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
*/
public static LocalDateTime getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
DayOfWeek week = today.getDayOfWeek();
int value = week.getValue();
if (isFirst) {
resDate = today.minusDays(value - 1);
} else {
resDate = today.plusDays(7 - value);
time = MaxTime;
}
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
return localDateTime;
}
/**
* @Description:本月的开始时间
* @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
*/
public static LocalDateTime getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
int length = month.length(today.isLeapYear());
if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
} else {
resDate = LocalDate.of(today.getYear(), month, length);
time = MinTime;
}
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
return localDateTime;
}
/**
* @Description:本季度的开始时间
* @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
*/
public static LocalDateTime getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
time = MaxTime;
}
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
return localDateTime;
}
/**
* @Description:本年度的开始时间
* @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
*/
public static LocalDateTime getStartOrEndDayOfYear(LocalDate today, Boolean isFirst){
String time = MinTime;
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
} else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
time = MaxTime;
}
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + time);
return localDateTime;
}
二、返回 String 格式的起止时间
/**
* @Description:返回 String 格式的周、月、季度、年起止时间
* @Author: lsiyan
* @Date: 2020/8/21 22:47
*/
import java.time.*;
import java.time.temporal.TemporalAdjusters;
/**
* jdk8 获取本周,本月,本季度,本年起始时间工具类 String
*/
public class LocalDateTimeUtils {
//本周的开始时间
public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
DayOfWeek week = today.getDayOfWeek();
int value = week.getValue();
if (isFirst) {
resDate = today.minusDays(value - 1);
} else {
resDate = today.plusDays(7 - value);
}
return resDate.toString();
}
//本月的开始时间
public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
int length = month.length(today.isLeapYear());
if (isFirst) {
resDate = LocalDate.of(today.getYear(), month, 1);
} else {
resDate = LocalDate.of(today.getYear(), month, length);
}
return resDate.toString();
}
//本季度开始时间
public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
}
return resDate.toString();
}
//本年度开始时间
public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
if (isFirst) {
resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
} else {
resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
}
return resDate.toString();
}
public static void main(String[] args) {
System.out.println("本周开始时间>>>" + getStartOrEndDayOfWeek(null, true));
System.out.println("本周结束时间>>>" + getStartOrEndDayOfWeek(null, false));
System.out.println("本月开始时间>>>" + getStartOrEndDayOfMonth(null, true));
System.out.println("本月结束时间>>>" + getStartOrEndDayOfMonth(null, false));
System.out.println("本季度开始时间>>>" + getStartOrEndDayOfQuarter(null, true));
System.out.println("本季度结束时间>>>" + getStartOrEndDayOfQuarter(null, false));
System.out.println("本年开始时间>>>" + getStartOrEndDayOfYear(null, true));
System.out.println("本年结束时间>>>" + getStartOrEndDayOfYear(null, false));
}
}
版权声明:本文为Athena072213原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。