java时间类型相关方法合集


前言

在工作中不可避免会遇到时间类型的使用,自己完成功能之后有可能还会在下一个开发任务中出现想类似的需求,如此重复造轮子即浪费精力又拖慢进度,所以特意将此类的方法集中记录了下来,之后还会继续增加。


一、传入时间参数,返回计算数值

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;
    }

总结

时间类型需求不少,总归不出两个方向,需要用到返回的时间或者是需要在遍历时间中做出一些操作,掌握一些方法只是方便自己开发,我们应该更深入的理解方法的实现才能设计出更加适合业务的代码。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荒野漫步者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值