importjava.time.LocalDateTime;
importjava.time.format.DateTimeFormatter;
importjava.time.temporal.TemporalAdjusters;
public classLocalDateTimeUtil {
/*** 比较 localDateTime2 是否在localDateTime1之前(比较大小)*@paramlocalDateTime1*@paramlocalDateTime2*@return*/public staticBoolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){
returnlocalDateTime1.isBefore(localDateTime2);
}
/*** 获取当前月份前/后的月份的第一天*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString firstDay(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,0,i);
//获取该月份的第一天String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// System.out.println("第一天为:"+firstDay);returnfirstDay;
}
/*** 获取当前月份前/后的月份的最后一天*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString lastDay(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,0,i);
//获取该月份的最后一天String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// System.out.println("最后一天为:"+lastDay);returnlastDay;
}
/*** 获取当时间前/后的时间(天)*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString obtainDay(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,1,i);
//获取天String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// System.out.println("获取的时间为(天):"+day);returnday;
}
/*** 获取当时间前/后的时间(小时)*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString obtainHours(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,2,i);
//获取该月份的最后一天String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
// System.out.println("获取的时间为(小时):"+hours);returnhours;
}
/*** 获取当时间前/后的时间(小时)*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString obtainMinutes(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,3,i);
//获取该月份的最后一天String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
// System.out.println("获取的时间为(分钟):"+minutes);returnminutes;
}
/*** 获取当时间前/后的时间(小时)*@parami指定距离当前月份的时间*@paramstate状态 0.当月 1.前 2.后*@return*/public staticString obtainSeconds(Integer state,Integer i){
LocalDateTime date = null;
//type 类型 0.月 1.天 2.小时 3.分钟 4.秒date = getLocalDateTime(state,4,i);
//获取该月份的最后一天String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
// System.out.println("获取的时间为(秒):"+seconds);returnseconds;
}
public static voidmain(String[] args) {
System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1));
System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1));
System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1));
System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1));
System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1));
System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1));
System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1));
System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1));
System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1));
System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1));
}
private staticLocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) {
LocalDateTime date;
if(state == 0){
date = LocalDateTime.now();
}else if(state == 1){
if(type == 0) { //获取月date = LocalDateTime.now().minusMonths(i);
}else if(type == 1){ //获取天date = LocalDateTime.now().minusDays(i);
}else if(type == 2){ //获取小时date = LocalDateTime.now().minusHours(i);
}else if(type == 3){ //获取分钟date = LocalDateTime.now().minusMinutes(i);
}else{ //获取秒date = LocalDateTime.now().minusSeconds(i);
}
}else{
if(type == 0) { //获取月date = LocalDateTime.now().plusMonths(i);
}else if(type == 1){ //获取天date = LocalDateTime.now().plusDays(i);
}else if(type == 2){ //获取小时date = LocalDateTime.now().plusHours(i);
}else if(type == 3){ //获取分钟date = LocalDateTime.now().plusMinutes(i);
}else{ //获取秒date = LocalDateTime.now().plusSeconds(i);
}
}
returndate;
}
}