java 日期 date_JAVA Date 日期工具

packagetimeUtil;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Date;importjava.util.List;importjava.util.stream.Collectors;public classTimeUtil {/*** 时间类型枚举,用于查询不同时间段的记录*/

public static enumDateType {

today,

lastWeek,

lastOneMonth,

lastThreeMonths,

lastOneYear

}/*** 计算时间间隔,单位为天数

*@paramstartTime

*@paramendTime

*@return

*/

public static int equationOfDay(long startTime, longendTime) {

startTime=dateToStamp(stampToDate(startTime));

endTime=dateToStamp(stampToDate(endTime));int newL = (int) ((endTime - startTime) / (1000 * 3600 * 24));returnnewL;

}/*** 计算时间间隔,单位为自然月

*@paramstartTime

*@paramendTime

*@return

*/

public static int equationOfMonth(long startTime,longendTime){returnequationOfMonth(stampToDate(startTime),stampToDate(endTime));

}/*** 计算时间间隔,单位为年

*@paramstartTime

*@paramendTime

*@return

*/

public static int equationOfYear(long startTime,longendTime){return equationOfMonth(startTime,endTime)/12;

}private static intequationOfMonth(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();

}returnresult;

}/*** 时间戳转换为时间

*@paraml

*@return

*/

public static String stampToDate(longl) {

String res;

SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");long lt =l;

Date date= newDate(lt);

res=simpleDateFormat.format(date);returnres;

}/*** 时间转换为时间戳

*@params

*@return

*/

public static longdateToStamp(String s) {

SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");

Date date= null;try{

date=simpleDateFormat.parse(s);returndate.getTime();

}catch(java.text.ParseException e) {//TODO Auto-generated catch block

e.printStackTrace();return -1;

}

}/*** 计算日期间相差天数、月份(相差的天数用1年2个月零3天格式展示)工具类*/

public static intgetDay(Date date) {

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);returncalendar.get(Calendar.DATE);

}/*** 返回日期的月份,1-12,即yyyy-MM-dd中的MM

*

*@paramdate

*@return

*/

public static intgetMonth(Date date) {

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);return calendar.get(Calendar.MONTH) + 1;

}/*** 返回日期的年,即yyyy-MM-dd中的yyyy

*

*@paramdate

*@returnint*/

public static intgetYear(Date date) {

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);returncalendar.get(Calendar.YEAR);

}public static int getDaysOfMonth(int year, intmonth) {

Calendar calendar=Calendar.getInstance();

calendar.set(year, month- 1, 1);returncalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

}/*** 获取相应时间段的记录

*@paramdateType

*@return

*/

public static List getRecordByDate(DateType dateType,Listrecords){//获取系统时间,返回为秒,乘1000转换为毫秒

final long currentSecends =System.currentTimeMillis();switch(dateType){casetoday:returnrecords.stream().

filter(record-> TimeUtil.equationOfDay(record.getDate().getTime(),currentSecends)==0)

.collect(Collectors.toList());caselastWeek:returnrecords.stream().

filter(record-> TimeUtil.equationOfDay(record.getDate().getTime(),currentSecends)<=7)

.collect(Collectors.toList());caselastOneMonth:returnrecords.stream().

filter(record-> TimeUtil.equationOfMonth(record.getDate().getTime(),currentSecends)<=1)

.collect(Collectors.toList());caselastThreeMonths:returnrecords.stream().

filter(record-> TimeUtil.equationOfMonth(record.getDate().getTime(),currentSecends)<=3)

.collect(Collectors.toList());caselastOneYear:returnrecords.stream().

filter(record-> TimeUtil.equationOfYear(record.getDate().getTime(),currentSecends)<=1)

.collect(Collectors.toList());default:returnrecords;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值