该工具可以根据date返回的毫秒数与特定毫秒数,获取两个时间戳之间的时间段
Record类
package timeUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class Record {
private Date date;
private UUID id;
public Record(){
this.date=new Date();
this.id=UUID.randomUUID();
}
public Record(UUID id){
this.id=id;
this.date=new Date();
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public UUID getId() {
return id;
}
public String getDateString(){
return new SimpleDateFormat("yyyy年MM月dd日").format(date);
}
}
工具类
package timeUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class TimeUtil {
/**
* 时间类型枚举,用于查询不同时间段的记录
*/
public static enum DateType {
today,
lastWeek,
lastOneMonth,
lastThreeMonths,
lastOneYear
}
/**
* 计算时间间隔,单位为天数
* @param startTime
* @param endTime
* @return
*/
public static int equationOfDay(long startTime, long endTime) {
startTime = dateToStamp(stampToDate(startTime));
endTime = dateToStamp(stampToDate(endTime));
int newL = (int) ((endTime - startTime) / (1000 * 3600 * 24));
return newL;
}
/**
* 计算时间间隔,单位为自然月
* @param startTime
* @param endTime
* @return
*/
public static int equationOfMonth(long startTime,long endTime){
return equationOfMonth(stampToDate(startTime),stampToDate(endTime));
}
/**
* 计算时间间隔,单位为年
* @param startTime
* @param endTime
* @return
*/
public static int equationOfYear(long startTime,long endTime){
return equationOfMonth(startTime,endTime)/12;
}
private static int equationOfMonth(String startDate,String endDate){
int result=0;
try {
SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd");
Date start = sfd.parse(startDate);
Date end = sfd.parse(endDate);
int startYear=getYear(start);
int startMonth=getMonth(start);
int startDay=getDay(start);
int endYear=getYear(end);
int endMonth=getMonth(end);
int endDay=getDay(end);
if (startDay>endDay){ //1月17 大于 2月28
if (endDay==getDaysOfMonth(getYear(new Date()),2)){ //也满足一月
result=(endYear-startYear)*12+endMonth-startMonth;
}else{
result=(endYear-startYear)*12+endMonth-startMonth-1;
}
}else{
result=(endYear-startYear)*12+endMonth-startMonth;
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* 时间戳转换为时间
* @param l
* @return
*/
public static String stampToDate(long l) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
long lt = l;
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 时间转换为时间戳
* @param s
* @return
*/
public static long dateToStamp(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpleDateFormat.parse(s);
return date.getTime();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
/**
* 计算日期间相差天数、月份(相差的天数用1年2个月零3天格式展示)工具类
*/
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
/**
* 返回日期的月份,1-12,即yyyy-MM-dd中的MM
*
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 返回日期的年,即yyyy-MM-dd中的yyyy
*
* @param date
* @return int
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获取相应时间段的记录
* @param dateType
* @return
*/
public static List<Record> getRecordByDate(DateType dateType,List<Record> records){
//获取系统时间,返回为秒,乘1000转换为毫秒
final long currentSecends = System.currentTimeMillis();
switch (dateType){
case today:
return records.stream().
filter(record-> TimeUtil.equationOfDay(record.getDate().getTime(),currentSecends)==0)
.collect(Collectors.toList());
case lastWeek:
return records.stream().
filter(record-> TimeUtil.equationOfDay(record.getDate().getTime(),currentSecends)<=7)
.collect(Collectors.toList());
case lastOneMonth:
return records.stream().
filter(record-> TimeUtil.equationOfMonth(record.getDate().getTime(),currentSecends)<=1)
.collect(Collectors.toList());
case lastThreeMonths:
return records.stream().
filter(record-> TimeUtil.equationOfMonth(record.getDate().getTime(),currentSecends)<=3)
.collect(Collectors.toList());
case lastOneYear:
return records.stream().
filter(record-> TimeUtil.equationOfYear(record.getDate().getTime(),currentSecends)<=1)
.collect(Collectors.toList());
default:
return records;
}
}
}
单元测试
public static void main(String[] args) {
final long currentSecends = System.currentTimeMillis();
List<Record> records = new ArrayList<Record>();
Record today =new Record();
Record yesterday = new Record();
yesterday.setDate(new Date(dateToStamp("2018-7-26")));
Record lastMonth = new Record();
lastMonth.setDate(new Date(dateToStamp("2018-6-26")));
Record lastTwoYear = new Record();
lastTwoYear.setDate(new Date(dateToStamp("2016-6-26")));
records.add(today);
records.add(yesterday);
records.add(lastMonth);
records.add(lastTwoYear);
List<Record> results =getRecordByDate(DateType.lastOneYear, records);
for(Record record:results) {
System.out.println(record.getDateString());
}
}
测试结果:
2018年07月27日
2018年07月26日
2018年06月26日