前言
在工作中不可避免会遇到时间类型的使用,自己完成功能之后有可能还会在下一个开发任务中出现想类似的需求,如此重复造轮子即浪费精力又拖慢进度,所以特意将此类的方法集中记录了下来,之后还会继续增加。
一、传入时间参数,返回计算数值
1.根据传入的的时间返回是当前年的第几天
public static int getDayOfYear(Date date){
Calendar cd = Calendar.getInstance();
cd.setTime(date);
return cd.get(Calendar.DAY_OF_YEAR);
}
2.根据传入的的时间返回是当前月的第几周
public static int getDayOfWeekInMonth(Date date){
Calendar cd = Calendar.getInstance();
cd.setTime(date);
return cd.get(Calendar.DAY_OF_WEEK_IN_MONTH);
}
3.根据传入的的时间返回是当前周的第几天(从周一开始)
public static int getDayOfWeek(Date date){
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int day = cd.get(Calendar.DAY_OF_WEEK);
day = day==1?7:day-1;
return day;
}
二、传入数值,返回时间
1.返回指定天数后的时间(正数向后,负数向前)
public static Date getDayOnNum(Date date,int day){
long time = date.getTime();
time = time+(day*(24*60*60*1000l));
return new Date(time);
}
2.返回指定周数的时间
public static Date getDayOnNumWeek(Date date,int week){
long time = date.getTime();
time = time+(week*(7*24*60*60*1000l));
return new Date(time);
}
3.返回指定月数的时间(正数向后,负数向前,超出本月最大天数则返回月末日期)
public static Date getMonthOnNum(Date date, int month) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, month);
return c.getTime();
}
4.返回本周第一天与最后一天
public static Date[] getWeekStartAndEnd(Date date){
Date[] dates = new Date[2];
Calendar cd=Calendar.getInstance();
cd.setFirstDayOfWeek(Calendar.MONDAY);
cd.setTime(date);
cd.set(Calendar.DAY_OF_WEEK, cd.getFirstDayOfWeek());
dates[0] = cd.getTime();
cd.set(Calendar.DAY_OF_WEEK, cd.getFirstDayOfWeek()+6);
dates[1] = cd.getTime();
return dates;
}
5.返回本月第一天与最后一天
public static Date[] getMonthStartAndEnd(Date date) {
Date[] dates = new Date[2];
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int maxDay = cd.getActualMaximum(Calendar.DATE);
cd.set(Calendar.DAY_OF_MONTH,1);
dates[0] = cd.getTime();
cd.set(Calendar.DAY_OF_MONTH,maxDay);
dates[1] = cd.getTime();
return dates;
}
三、其他时间类逻辑
1.计算两个时间差
public static String getDateGapTime(Date date,Date date1) {
long time = Math.abs(date.getTime()-date1.getTime());
long day = time/(24*60*60*1000);
int hourTime = (int)time%(24*60*60*1000);
int hour = hourTime/(60*60*1000);
int minuteTime = hourTime%(60*60*1000);
int minute = minuteTime/(60*1000);
int minuteTimeTime = minuteTime%(60*1000);
int second = minuteTimeTime/1000;
return "时间差:"+day+"天"+hour+"小时"+minute+"分钟"+second+"秒";
}
2.返回两个时间经历过的每一天(含头不含尾)
public static List<Date> getDateGapDays(Date date, Date date1) {
List<Date> list = new ArrayList<Date>();
long start = date.getTime();
long end = date1.getTime();
if(start>end){
end = start;
start = date1.getTime();
}
while (start<end){
list.add(new Date(start));
start+=(24*60*60*1000l);
}
return list;
}
3.返回两个时间所经历的每一周
public static List<String> getDateGapWeeks(Date date, Date date1) {
List<String> list = new ArrayList<String>();
long start = date.getTime();
long end = date1.getTime();
if(start>end){
end = start;
start = date1.getTime();
}
while (start<end){
Calendar cd = Calendar.getInstance();
cd.setTime(new Date(start));
cd.set(Calendar.DAY_OF_WEEK, cd.getFirstDayOfWeek());
start = cd.getTimeInMillis();
start+=(7*24*60*60*1000l);
list.add("第"+cd.get(Calendar.WEEK_OF_YEAR)+"周");
}
return list;
}
4.返回两个时间所经历的月数
public static List<String> getDateGapMonths(Date date, Date date1) {
List<String> list = new ArrayList<String>();
long start = date.getTime();
long end = date1.getTime();
if(start>end){
end = start;
start = date1.getTime();
}
while (start<end){
Calendar cd = Calendar.getInstance();
cd.setTime(new Date(start));
cd.set(Calendar.DAY_OF_WEEK, cd.getFirstDayOfWeek());
int maxDay = cd.getActualMaximum(Calendar.DATE);
start = cd.getTimeInMillis();
start+=(maxDay*24*60*60*1000l);
list.add(cd.get(Calendar.YEAR)+"年"+(cd.get(Calendar.MONTH)+1)+"月");
}
return list;
}
总结
时间类型需求不少,总归不出两个方向,需要用到返回的时间或者是需要在遍历时间中做出一些操作,掌握一些方法只是方便自己开发,我们应该更深入的理解方法的实现才能设计出更加适合业务的代码。
|