zoukankan      html  css  js  c++  java
  • 时间炸弹

    一:

    //如果超过此时间,则不能进入程序
    
    if (isExpiredLocal(2013, 7, 07)) {
    
    Toast.makeText(this, "EXPIRED!", Toast.LENGTH_SHORT).show();
    this.finish();(this为进入程序的Activity)
    return;
    }
    
     
    
    /**
    * 判断本机日期是否超过 指定的日期,
    * 
    * @param year
    * @param month
    * @param day
    * @return
    */
    public static boolean isExpiredLocal(int year, int month, int day) {
    Calendar endtime = Calendar.getInstance();
    endtime.set(year, month - 1, day);
    Calendar currenttime = Calendar.getInstance();
    return currenttime.after(endtime);
    }

    二:

    //正常
    private static final int TRIAL = 2;
    //即将过期
    private static final int EXPIRE_SOON= 1;
    //过期
    private static final int BOMB = 0;
    
    // 时间炸弹
            if (isExpiredLocal(2) == TRIAL) {
                // 正常时的操作
            } else if (isExpiredLocal(2) == EXPIRE_SOON) {
                // 即将过期时的操作
            } else if (isExpiredLocal(2) == BOMB) {
                // 过期时的操作
            }
    
    
    /**
         * 判断本机日期是否超过 指定的日期,
         * 
         * @return
         */
        public int isExpiredLocal(int months) {
            // 创建将要写入时间炸弹日期的文件路径
            String filePath = "/mnt/sdcard/Dreye";
            String dirPath = "/mnt/sdcard/Dreye/timeBomb.txt";
            // 如果文件不存在
            if (!new File(dirPath).exists()) {
                // 将时间炸弹的日期写入“timeBomb.txt”文件,参数2表示当前时期加上2个月之后的时间,即:两个月后
                String sdate = nDaysAftertoday(months);
                FileUtil.writeSd(filePath, dirPath, sdate);
            }
            // 从文件中读取时间炸弹的日期
            String timeBomb = FileUtil.readSd(dirPath);
            Date d = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println("今天的日期:" + df.format(d));
            String currenttime = df.format(d);
            int betweenDays = nDaysBetweenTwoDate(currenttime, timeBomb);
            //即将到期
            if ((betweenDays < 10) & (betweenDays > 0)) {
                return EXPIRE_SOON;
            //到期
            } else if ((betweenDays == 0)|| (betweenDays < 0)) {
                return BOMB;
            }

    //正常
            return TRIAL;
        }
    
        /**
         * 获取n个月后的日期
         * 
         * @param nMonths
         * @return yyyy-MM-dd
         */
        public String nDaysAftertoday(int nMonths) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", this
                    .getResources().getConfiguration().locale);
            Calendar rightNow = Calendar.getInstance();
            // rightNow.add(Calendar.DAY_OF_MONTH,-1);
            rightNow.add(Calendar.MONTH, +nMonths);
            return df.format(rightNow.getTime());
        }
    
        /**
         * 计算两个日期相隔的天数
         * @param firstString
         * @param secondString
         * @return 相隔的天数
         */
        public int nDaysBetweenTwoDate(String firstString, String secondString) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date firstDate = null;
            Date secondDate = null;
            try {
                firstDate = df.parse(firstString);
                secondDate = df.parse(secondString);
            } catch (Exception e) {
                // 日期型字符串格式错误
            }
    
            int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
            return nDay;
        }
  • 相关阅读:
    Spring MVC “404 Not Found”错误的解决
    oracle高级编程2
    oracl 处理中文问题
    oracle中调用存储过程
    【Java多线程】使用多线程计算阶乘累加 1!+2!+3!+...+19!+20!。其中一个线程计算阶乘,另一线程实现累加并输出结果
    年轻人不讲武德,竟然还搞不懂JVM?求你们来看阿里Java开发岗的招聘要求吧!
    想拿到10k40k的offer,这些技能必不可少!作为程序员的你了解吗?
    一位面试官询问我:Java中的JVM内存溢出和内存泄露是什么?我这么回答成功拿到了offer。
    (四) Spring整合ActiveMQ超详细教程
    基于Java的Socket类Tcp网络编程实现实时聊天互动程序(一):QQ聊天界面的搭建
  • 原文地址:https://www.cnblogs.com/Cherry-B/p/3844939.html
Copyright © 2011-2022 走看看