该工具可以根据date返回的毫秒数与特定毫秒数,获取两个时间戳之间的时间段
Record类
1 package timeUtil; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.UUID; 6 7 public class Record { 8 9 private Date date; 10 private UUID id; 11 12 public Record(){ 13 this.date=new Date(); 14 this.id=UUID.randomUUID(); 15 } 16 17 public Record(UUID id){ 18 this.id=id; 19 this.date=new Date(); 20 } 21 22 public Date getDate() { 23 return date; 24 } 25 26 public void setDate(Date date) { 27 this.date = date; 28 } 29 30 31 32 public UUID getId() { 33 return id; 34 } 35 36 37 public String getDateString(){ 38 return new SimpleDateFormat("yyyy年MM月dd日").format(date); 39 } 40 }
工具类
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; } } }
单元测试
1 public static void main(String[] args) { 2 final long currentSecends = System.currentTimeMillis(); 3 List<Record> records = new ArrayList<Record>(); 4 Record today =new Record(); 5 Record yesterday = new Record(); 6 yesterday.setDate(new Date(dateToStamp("2018-7-26"))); 7 Record lastMonth = new Record(); 8 lastMonth.setDate(new Date(dateToStamp("2018-6-26"))); 9 Record lastTwoYear = new Record(); 10 lastTwoYear.setDate(new Date(dateToStamp("2016-6-26"))); 11 records.add(today); 12 records.add(yesterday); 13 records.add(lastMonth); 14 records.add(lastTwoYear); 15 16 17 List<Record> results =getRecordByDate(DateType.lastOneYear, records); 18 19 for(Record record:results) { 20 System.out.println(record.getDateString()); 21 } 22 23 24 } 25
测试结果:
2018年07月27日
2018年07月26日
2018年06月26日