java 日期计算


    /**
     * 根据起期和期限计算止期
     * @param startDate 起期(格式为yyyy-MM-dd)
     * @param date 天数或者月数或者年数
     * @param dayOrMonth (传"day"或者"month"或者"year")
     * @return
     * @throws ParseException
     */
    public static String calculateDate(String startDate, int date, String dayOrMonth) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sf.parse(startDate));
        if ("year".equals(dayOrMonth))
            calendar.add(Calendar.YEAR, date);
        else if ("month".equals(dayOrMonth))
            calendar.add(Calendar.MONTH, date);
        else
            calendar.add(Calendar.DAY_OF_YEAR, date);
        return sf.format(calendar.getTime());
    }

执行:calculateDate("2017-08-20", 2, "month")
结果:2017-10-20


    /**
     * 日期比较
     * @param time1 日期1(格式为yyyy-MM-dd)
     * @param time2 日期2(格式为yyyy-MM-dd)
     * @return 日期1在日期2之前 true; 日期1在日期2之前 false 
     * @throws ParseException 
     */
    public static boolean compareDate(String time1, String time2) throws ParseException {
        boolean flag = false;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = df.parse(time1);
        Date date2 = df.parse(time2);
        if (date1.before(date2))
            flag = true;
        return flag;
    }

执行:compareDate("2017-01-15", "2017-02-20")
结果:true

方法1:

    /**
     * 判断当前系统时间是否在当天的6点之后
     * @return true-是  false-否
     */
    private static boolean compareDate(){
        boolean flag = false;
        //当前系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateStr = df.format(new Date());
        String yyyyMMdd = currentDateStr.split(" ")[0];
//      String hour = "";//配置成配置项
//      if (StringUtils.isEmpty(hour)) {
//          hour = "06";
//      } else {
//          hour = hour.replace(" ", "");
//      }
//      int hours;
//      try {
//          hours = Integer.parseInt(hour);
//      } catch (Exception e) {
//          hours = 6;
//      }
//      if(hours < 0 || hours >= 24) hour = "06";
//      String appointDateStr = yyyyMMdd + " " + hour + ":00:00";
//      String HHmmss = "06:00:00"; //配置成配置项
//      SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
//      try {
//          df2.parse(HHmmss);
//      } catch (ParseException e1) {
//          HHmmss = "06:00:00";
//      }
        String HHmmss = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            Calendar calendar  = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 6);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            HHmmss = sdf.format(calendar.getTime());
        } catch (Exception e) {
            HHmmss = "06:00:00";
        }
        String appointDateStr = yyyyMMdd + " " + HHmmss;
        try {
            Date currentDate = df.parse(currentDateStr);
            Date appointDate = df.parse(appointDateStr);
            if (currentDate.after(appointDate)) {
                flag = true;
            }
        } catch (ParseException e) {
            //打印日志:转换日期异常
        }
        return flag;
    }

方法2:(推荐这种方式)


    /**
     * 判断当前系统时间是否在当天的6点之后
     * @return true-是  false-否
     */
    private static boolean compareDate(){
        boolean flag = false;
        //当前系统时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateStr = sdf.format(new Date());//当前日期
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 6);//配置成配置项
            calendar.set(Calendar.SECOND, 0);//配置成配置项
            calendar.set(Calendar.MINUTE, 0);//配置成配置项
            String appointDateStr = sdf.format(calendar.getTime());//指定日期
            Date currentDate = sdf.parse(currentDateStr);
            Date appointDate = sdf.parse(appointDateStr);
            if (currentDate.after(appointDate)) {
                flag = true;
            }
        } catch (ParseException e) {
            //打印日志:转换日期异常
        } catch (Exception e) {
            //打印日志:转换日期异常
        }
        return flag;
    }
    /**
     * 当前日期与指定日期相差天数
     */
    private static int days(Date currentDate, Date appointDate) {
        int days = (int) ((currentDate.getTime() - appointDate.getTime()) / (1000*3600*24));
        return days;
    }
    //main方法验证
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String appointDateStr = "2012-06-17 00:00:00";
        String currentDateStr = sdf.format(new Date());
        currentDateStr = currentDateStr.split(" ")[0] + " 00:00:00";
        try {
            Date appointDate = sdf.parse(appointDateStr);
            Date currentDate = sdf.parse(currentDateStr);

            int days = days(currentDate, appointDate);
            System.out.println(days);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值