java按月拆分两个时间段以及常用日期工具


/**
* 获取开始时间结束时间按照 日期单位 形成多个日期区间
* 第一个区间开始时间为传入开始时间
* 最后一个区间结束时间为传入结束时间
* @param startDate
* @param endDate
* @param unit 支持 ChronoUnit.DAYS,ChronoUnit.WEEKS,ChronoUnit.MONTHS
* @param zoneId 时区
* @return 每一个数组第一个为开始时间,第二个为结束时间;开始时间为当天0.0.0,结束时间为当天23.59.59
* 解决了正月最后时间段为下月1号的问题
*/
public static List<Date[]> rangeDate(Date startDate, Date endDate, ChronoUnit unit,ZoneId zoneId){
List<Date[]> returnList=new ArrayList<>();
ZonedDateTime zdt1= ZonedDateTime.ofInstant(startDate.toInstant(),zoneId);
ZonedDateTime zdt2= ZonedDateTime.ofInstant(endDate.toInstant(),zoneId);
switch (unit){
case DAYS:{
ZonedDateTime start= zdt1.with(ChronoField.SECOND_OF_DAY,0);
ZonedDateTime end= zdt1.with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds()-1);
while(true){
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
start=start.plusDays(1);
end=end.plusDays(1);
}
}

break;
}
case WEEKS:{
int dayOfWeek=zdt1.get(ChronoField.DAY_OF_WEEK);
ZonedDateTime start= zdt1.plusDays(1-dayOfWeek).with(ChronoField.SECOND_OF_DAY,0);
ZonedDateTime end= zdt1.plusDays(7-dayOfWeek).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds()-1);
while(true){
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
start=start.plusWeeks(1);
end=end.plusWeeks(1);
}
}
if(!returnList.isEmpty()){
Date[] firstEle=returnList.get(0);
Date[] lastEle=returnList.get(returnList.size()-1);
firstEle[0]=Date.from(zdt1.with(ChronoField.SECOND_OF_DAY,0).toInstant());
lastEle[1]=Date.from(zdt2.with(ChronoField.SECOND_OF_DAY,0).toInstant());
}
break;
}
case MONTHS:{
ZonedDateTime temp=zdt1;
while(true) {
int dayOfMonth = temp.get(ChronoField.DAY_OF_MONTH);
int max = temp.getMonth().maxLength();
ZonedDateTime start = temp.plusDays(1 - dayOfMonth).with(ChronoField.SECOND_OF_DAY, 0);
ZonedDateTime end = temp.plusDays(max - dayOfMonth).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds() - 1);
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
temp=temp.plusMonths(1);
}
}
if(!returnList.isEmpty()){
for (rl in returnList) {
if (rl != returnList.first() && rl != returnList.last()) {
Calendar calendar = Calendar.getInstance()
calendar.setTime(rl[1])
calendar.add(Calendar.DATE, -1) //当前时间减去一天
rl[1] = calendar.time
}
})
Date[] firstEle=returnList.get(0);
Date[] lastEle=returnList.get(returnList.size()-1);
firstEle[0]=Date.from(zdt1.with(ChronoField.SECOND_OF_DAY,0).toInstant());
lastEle[1]=Date.from(zdt2.with(ChronoField.SECOND_OF_DAY,0).toInstant());
}
break;
}
default:{
throw BaseRuntimeException.getException("[DateUtil.rangeDate],ChronoUnit["+unit.toString()+"] Not Support!");
}
}
return returnList;
}

import com.huixin.appserver2010.exception.ServiceException
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.temporal.ChronoField
import java.time.temporal.ChronoUnit
import java.util.*


class SplitDateUtils {
companion object {

/**
* 获取开始时间结束时间按照 日期单位 形成多个日期区间
* 第一个区间开始时间为传入开始时间
* 最后一个区间结束时间为传入结束时间
* @param startDate
* @param endDate
* @param unit 支持 ChronoUnit.DAYS,ChronoUnit.WEEKS,ChronoUnit.MONTHS
* @param zoneId 时区
* @return 每一个数组第一个为开始时间,第二个为结束时间;开始时间为当天0.0.0,结束时间为当天23.59.59
* 解决了正月最后时间段为下月1号的问题
*/
fun rangeDate(startDate: Date, endDate: Date, unit: ChronoUnit, zoneId: ZoneId?): List<Array<Date>>? {
val returnList: MutableList<Array<Date>> = ArrayList<Array<Date>>()
val zdt1 = ZonedDateTime.ofInstant(startDate.toInstant(), zoneId)
val zdt2 = ZonedDateTime.ofInstant(endDate.toInstant(), zoneId)
when (unit) {
ChronoUnit.DAYS -> {
var start = zdt1.with(ChronoField.SECOND_OF_DAY, 0)
var end = zdt1.with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.duration.seconds - 1)
while (true) {
returnList.add(arrayOf<Date>(Date.from(start.toInstant()), Date.from(end.toInstant())))
if (!zdt2.isBefore(start) && !zdt2.isAfter(end)) {
break
} else {
start = start.plusDays(1)
end = end.plusDays(1)
}
}
}
ChronoUnit.WEEKS -> {
val dayOfWeek = zdt1[ChronoField.DAY_OF_WEEK]
var start = zdt1.plusDays((1 - dayOfWeek).toLong()).with(ChronoField.SECOND_OF_DAY, 0)
var end = zdt1.plusDays((7 - dayOfWeek).toLong()).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.duration.seconds - 1)
while (true) {
returnList.add(arrayOf<Date>(Date.from(start.toInstant()), Date.from(end.toInstant())))
if (!zdt2.isBefore(start) && !zdt2.isAfter(end)) {
break
} else {
start = start.plusWeeks(1)
end = end.plusWeeks(1)
}
}
if (!returnList.isEmpty()) {
val firstEle: Array<Date> = returnList[0]
val lastEle: Array<Date> = returnList[returnList.size - 1]
firstEle[0] = Date.from(zdt1.with(ChronoField.SECOND_OF_DAY, 0).toInstant())
lastEle[1] = Date.from(zdt2.with(ChronoField.SECOND_OF_DAY, 0).toInstant())
}
}
ChronoUnit.MONTHS -> {
var temp = zdt1
while (true) {
val dayOfMonth = temp[ChronoField.DAY_OF_MONTH]
val max = temp.month.maxLength()
val start = temp.plusDays((1 - dayOfMonth).toLong()).with(ChronoField.SECOND_OF_DAY, 0)
val end = temp.plusDays((max - dayOfMonth).toLong()).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.duration.seconds - 1)
returnList.add(arrayOf<Date>(Date.from(start.toInstant()), Date.from(end.toInstant())))
temp = if (!zdt2.isBefore(start) && !zdt2.isAfter(end)) {
break
} else {
temp.plusMonths(1)
}
}
if (!returnList.isEmpty()) {
for (rl in returnList) {
if (rl != returnList.first() && rl != returnList.last()) {
val calendar = Calendar.getInstance()
calendar.time = rl[1]
calendar.add(Calendar.DATE, -1) //当前时间减去一天
rl[1] = calendar.time
}
}
val firstEle: Array<Date> = returnList[0]
val lastEle: Array<Date> = returnList[returnList.size - 1]
firstEle[0] = Date.from(zdt1.with(ChronoField.SECOND_OF_DAY, 0).toInstant())
lastEle[1] = Date.from(zdt2.with(ChronoField.SECOND_OF_DAY, 0).toInstant())
}
}
else -> {
throw ServiceException("[DateUtil.rangeDate],ChronoUnit[$unit] Not Support!")
}
}
return returnList
}
}
}

class DateUtils {
companion object {
const val FORMAT_DEFAULT = "yyyy-MM-dd"
const val FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss"
const val FORMAT_DAY = "dd"

fun format(date: Date, format: String = FORMAT_DEFAULT): String {
return SimpleDateFormat(format).format(date)
}

fun parseDate(dateStr: String, format: String = FORMAT_DEFAULT): Date {
return SimpleDateFormat(format).parse(dateStr)
}
/*
* 获取两个日期相差天数
*/
fun getDiffDays(date1: Date,date2: Date):Float{
return SimpleDateFormat(FORMAT_DAY).format(date2).toString().toFloat()-SimpleDateFormat(FORMAT_DAY).format(date1).toString().toFloat()+1
}

/*
* 将时间戳转换为时间
*/
@Throws(ParseException::class)
fun stampToDate(s: String): String? {
val res: String
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val lt: Long = s.toLong()
val date = Date(lt)
res = simpleDateFormat.format(date)
return res
}

/*
* 将时间转换为时间戳
*/
@Throws(ParseException::class)
fun dateToStamp(s: String?): String? {
val res: String
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date = simpleDateFormat.parse(s)
val ts = date.time
res = ts.toString()
return res
}
}
}
原文地址:https://www.cnblogs.com/zyb2016/p/14504271.html