• 日期操作


    final static long EIGHT_HOURS = 8 * 60 * 60 * 1000;
    public final static long ONE_HOUR = 60 * 60 * 1000;
    public final static long DAY = 24 * 60 * 60 * 1000;
    public final static long WEEK = 7 * 24 * 60 * 60 * 1000;
    public final static long ONE_MINUTE = 60 * 1000;
    
     
    
     
    
    /**
    
    * 计算两个日期之间相差的天数
    * 
    * @param startTime
    * 开始的时间
    * @param currentTime
    * 当前的时间
    * @return 相差天数
    * @throws ParseException
    */
    
    public static int daysBetween(Date startTime, Date currentTime) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      startTime = sdf.parse(sdf.format(startTime));
      currentTime = sdf.parse(sdf.format(currentTime));
      Calendar cal = Calendar.getInstance();
      cal.setTime(startTime);
      long time1 = cal.getTimeInMillis();
      cal.setTime(currentTime);
      long time2 = cal.getTimeInMillis();
      long between_days = (time2 - time1) / (1000 * 3600 * 24);
    
      return Integer.parseInt(String.valueOf(between_days));
    }
    
     
    
    /**
    * 对时间进行天加减运算
    * 
    * @param time    要被加减的时间
    * @param num   被加减的天数(正为加,负为减)
    * @return
    */
    public static long addDate(long time, int num) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(new Date(time));
      cal.add(Calendar.DATE, num);
      return cal.getTime().getTime();
    }
    
    /**
    * 对小时加减运算
    *
    * @param date
    * @param num
    * @return
    */
    public static long addHour(long time, int num) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(new Date(time));
      cal.add(Calendar.HOUR_OF_DAY, num);
      return cal.getTime().getTime();
    }
    
    /**
    * 对分钟加减运算
    *
    * @param date
    * @param num
    * @return
    */
    public static long addMinute(long time, int num) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(new Date(time));
      cal.add(Calendar.MINUTE, num);
      return cal.getTime().getTime();
    }
    
    /**
    * 获取当天0点的时间戳
    *
    * @param
    * @return
    */
    public static long getTodayZeroHour(long time) {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
      try {
          return formatter.parse(formatter.format(new Date(time))).getTime();
        } catch (ParseException e) {
          e.printStackTrace();
          return 0;
        }
    }
    
    /**
    * 根据时间获取当月多少天
    *
    * @param date
    * @param num
    * @return
    */
    public static int getDayByCurMon(long time) {
      int day = 0;
      try {
          Date date = new Date(time);
          Calendar cal = Calendar.getInstance();
          cal.setTime(date);
          day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
          return day;
        } catch (Exception e) {
          e.printStackTrace();
        }
         return day;
    }
    
    //获取当前时间所在周的开始日期
    public static Date getFirstDayOfWeek(Date date) {
      Calendar c = Calendar.getInstance();
      c.setFirstDayOfWeek(Calendar.MONDAY);
      c.setTime(date);
      c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
      return getDayStart(c).getTime();
    }
    
    // 获取当前时间所在周的结束日期
    public static Date getLastDayOfWeek(Date date) {
      Calendar c = Calendar.getInstance();
      c.setFirstDayOfWeek(Calendar.MONDAY);
      c.setTime(date);
      c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
      return getDayEnd(c).getTime();
    }
  • 相关阅读:
    设计模式外观模式
    SQL Server高级查询
    电脑双屏下如何设置程序在第二屏幕运行
    Vistual Studio自带的计划任务功能
    SQL Server定时创建动态表
    访问网站出现空白页面的原因和解决方法
    微软老将Philip Su的离职信:回首12年职场生涯的心得和随笔
    SQL Server更改字段名
    刷新本页与父页的两条代码
    色彩感情
  • 原文地址:https://www.cnblogs.com/lizhen-home/p/7692338.html
Copyright © 2020-2023  润新知