Android Date 日期工具
阿新 • • 發佈:2018-12-08
該工具可以根據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日